1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2023 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Factory for creating a query analyzer for the configured tokenizer.
10 from typing import List, cast, TYPE_CHECKING
11 from abc import ABC, abstractmethod
12 from pathlib import Path
15 from nominatim.api.logging import log
16 from nominatim.api.connection import SearchConnection
19 from nominatim.api.search.query import Phrase, QueryStruct
21 class AbstractQueryAnalyzer(ABC):
22 """ Class for analysing incoming queries.
24 Query analyzers are tied to the tokenizer used on import.
28 async def analyze_query(self, phrases: List['Phrase']) -> 'QueryStruct':
29 """ Analyze the given phrases and return the tokenized query.
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.
42 async def make_query_analyzer(conn: SearchConnection) -> AbstractQueryAnalyzer:
43 """ Create a query analyzer for the tokenizer used by the database.
45 name = await conn.get_property('tokenizer')
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')
52 module = importlib.import_module(f'nominatim.api.search.{name}_tokenizer')
54 return cast(AbstractQueryAnalyzer, await module.create_query_analyzer(conn))