]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/tokenizer/token_analysis/base.py
add type annotations for database import functions
[nominatim.git] / nominatim / tokenizer / token_analysis / base.py
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Common data types and protocols for analysers.
9 """
10 from typing import TypeVar, Mapping, List, Any
11
12 from typing_extensions import Protocol
13
14
15 T_config = TypeVar('T_config') # pylint: disable=invalid-name
16
17 class Analyser(Protocol):
18     """ Instance of the token analyser.
19     """
20
21     def normalize(self, name: str) -> str:
22         """ Return the normalized form of the name. This is the standard form
23             from which possible variants for the name can be derived.
24         """
25
26     def get_variants_ascii(self, norm_name: str) -> List[str]:
27         """ Compute the spelling variants for the given normalized name
28             and transliterate the result.
29         """
30
31 class AnalysisModule(Protocol[T_config]):
32     """ Protocol for analysis modules.
33     """
34
35     def configure(self, rules: Mapping[str, Any], normalization_rules: str) -> T_config:
36         """ Prepare the configuration of the analysis module.
37             This function should prepare all data that can be shared
38             between instances of this analyser.
39         """
40
41     def create(self, normalizer: Any, transliterator: Any, config: T_config) -> Analyser:
42         """ Create a new instance of the analyser.
43             A separate instance of the analyser is created for each thread
44             when used in multi-threading context.
45         """