]> git.openstreetmap.org Git - nominatim.git/blob - docs/develop/ICU-Tokenizer-Modules.md
51b189f137f0ddc7a14c996ec87214122d62da89
[nominatim.git] / docs / develop / ICU-Tokenizer-Modules.md
1 # Writing custom sanitizer and token analysis modules for the ICU tokenizer
2
3 The [ICU tokenizer](../customize/Tokenizers.md#icu-tokenizer) provides a
4 highly customizable method to pre-process and normalize the name information
5 of the input data before it is added to the search index. It comes with a
6 selection of sanitizers and token analyzers which you can use to adapt your
7 installation to your needs. If the provided modules are not enough, you can
8 also provide your own implementations. This section describes how to do that.
9
10 ## Using non-standard sanitizers and token analyzers
11
12 Sanitizer names (in the `step` property) and token analysis names (in the
13 `analyzer`) may refer to externally supplied modules. There are two ways
14 to include external modules: through a library or from the project directory.
15
16 To include a module from a library, use the absolute import path as name and
17 make sure the library can be found in your PYTHONPATH.
18
19 To use a custom module without creating a library, you can put the module
20 somewhere in your project directory and then use the relative path to the
21 file. Include the whole name of the file including the `.py` ending.
22
23 ## Custom sanitizer modules
24
25 A sanitizer module must export a single factory function `create` with the
26 following signature:
27
28 ``` python
29 def create(config: SanitizerConfig) -> Callable[[ProcessInfo], None]
30 ```
31
32 The function receives the custom configuration for the sanitizer and must
33 return a callable (function or class) that transforms the name and address
34 terms of a place. When a place is processed, then a `ProcessInfo` object
35 is created from the information that was queried from the database. This
36 object is sequentially handed to each configured sanitizer, so that each
37 sanitizer receives the result of processing from the previous sanitizer.
38 After the last sanitizer is finished, the resulting name and address lists
39 are forwarded to the token analysis module.
40
41 Sanitizer functions are instantiated once and then called for each place
42 that is imported or updated. They don't need to be thread-safe.
43 If multi-threading is used, each thread creates their own instance of
44 the function.
45
46 ### Sanitizer configuration
47
48 ::: nominatim.tokenizer.sanitizers.config.SanitizerConfig
49     rendering:
50         show_source: no
51         heading_level: 6
52
53 ### The sanitation function
54
55 The sanitation function receives a single object of type `ProcessInfo`
56 which has with three members:
57
58  * `place`: read-only information about the place being processed.
59    See PlaceInfo below.
60  * `names`: The current list of names for the place. Each name is a
61    PlaceName object.
62  * `address`: The current list of address names for the place. Each name
63    is a PlaceName object.
64
65 While the `place` member is provided for information only, the `names` and
66 `address` lists are meant to be manipulated by the sanitizer. It may add and
67 remove entries, change information within a single entry (for example by
68 adding extra attributes) or completely replace the list with a different one.
69
70 #### PlaceInfo - information about the place
71
72 ::: nominatim.data.place_info.PlaceInfo
73     rendering:
74         show_source: no
75         heading_level: 6
76
77
78 #### PlaceName - extended naming information
79
80 ::: nominatim.data.place_name.PlaceName
81     rendering:
82         show_source: no
83         heading_level: 6
84
85 ## Custom token analysis module
86
87 Setup of a token analyser is split into two parts: configuration and
88 analyser factory. A token analysis module must therefore implement two
89 functions:
90
91 ::: nominatim.tokenizer.token_analysis.base.AnalysisModule
92     rendering:
93         show_source: no
94         heading_level: 6
95
96
97 ::: nominatim.tokenizer.token_analysis.base.Analyzer
98     rendering:
99         show_source: no
100         heading_level: 6