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 incomming 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.
33 async def make_query_analyzer(conn: SearchConnection) -> AbstractQueryAnalyzer:
34 """ Create a query analyzer for the tokenizer used by the database.
36 name = await conn.get_property('tokenizer')
38 src_file = Path(__file__).parent / f'{name}_tokenizer.py'
39 if not src_file.is_file():
40 log().comment(f"No tokenizer named '{name}' available. Database not set up properly.")
41 raise RuntimeError('Tokenizer not found')
43 module = importlib.import_module(f'nominatim.api.search.{name}_tokenizer')
45 return cast(AbstractQueryAnalyzer, await module.create_query_analyzer(conn))