]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/api/search/query_analyzer_factory.py
Correct some typos
[nominatim.git] / nominatim / api / search / query_analyzer_factory.py
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2023 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Factory for creating a query analyzer for the configured tokenizer.
9 """
10 from typing import List, cast, TYPE_CHECKING
11 from abc import ABC, abstractmethod
12 from pathlib import Path
13 import importlib
14
15 from nominatim.api.logging import log
16 from nominatim.api.connection import SearchConnection
17
18 if TYPE_CHECKING:
19     from nominatim.api.search.query import Phrase, QueryStruct
20
21 class AbstractQueryAnalyzer(ABC):
22     """ Class for analysing incoming queries.
23
24         Query analyzers are tied to the tokenizer used on import.
25     """
26
27     @abstractmethod
28     async def analyze_query(self, phrases: List['Phrase']) -> 'QueryStruct':
29         """ Analyze the given phrases and return the tokenized query.
30         """
31
32
33     @abstractmethod
34     def normalize_text(self, text: str) -> str:
35         """ Bring the given text into a normalized form. That is the
36             standardized form search will work with. All information removed
37             at this stage is inevitably lost.
38         """
39
40
41
42 async def make_query_analyzer(conn: SearchConnection) -> AbstractQueryAnalyzer:
43     """ Create a query analyzer for the tokenizer used by the database.
44     """
45     name = await conn.get_property('tokenizer')
46
47     src_file = Path(__file__).parent / f'{name}_tokenizer.py'
48     if not src_file.is_file():
49         log().comment(f"No tokenizer named '{name}' available. Database not set up properly.")
50         raise RuntimeError('Tokenizer not found')
51
52     module = importlib.import_module(f'nominatim.api.search.{name}_tokenizer')
53
54     return cast(AbstractQueryAnalyzer, await module.create_query_analyzer(conn))