]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/tokenizer/place_sanitizer.py
add penalty for single words that look like stop words
[nominatim.git] / nominatim / tokenizer / place_sanitizer.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 Handler for cleaning name and address tags in place information before it
9 is handed to the token analysis.
10 """
11 from typing import Optional, List, Mapping, Sequence, Callable, Any, Tuple
12
13 from nominatim.errors import UsageError
14 from nominatim.config import Configuration
15 from nominatim.tokenizer.sanitizers.config import SanitizerConfig
16 from nominatim.tokenizer.sanitizers.base import SanitizerHandler, ProcessInfo
17 from nominatim.data.place_name import PlaceName
18 from nominatim.data.place_info import PlaceInfo
19
20
21 class PlaceSanitizer:
22     """ Controller class which applies sanitizer functions on the place
23         names and address before they are used by the token analysers.
24     """
25
26     def __init__(self, rules: Optional[Sequence[Mapping[str, Any]]],
27                  config: Configuration) -> None:
28         self.handlers: List[Callable[[ProcessInfo], None]] = []
29
30         if rules:
31             for func in rules:
32                 if 'step' not in func:
33                     raise UsageError("Sanitizer rule is missing the 'step' attribute.")
34                 if not isinstance(func['step'], str):
35                     raise UsageError("'step' attribute must be a simple string.")
36
37                 module: SanitizerHandler = \
38                     config.load_plugin_module(func['step'], 'nominatim.tokenizer.sanitizers')
39
40                 self.handlers.append(module.create(SanitizerConfig(func)))
41
42
43     def process_names(self, place: PlaceInfo) -> Tuple[List[PlaceName], List[PlaceName]]:
44         """ Extract a sanitized list of names and address parts from the
45             given place. The function returns a tuple
46             (list of names, list of address names)
47         """
48         obj = ProcessInfo(place)
49
50         for func in self.handlers:
51             func(obj)
52
53         return obj.names, obj.address