]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/tokenizer/sanitizers/clean_housenumbers.py
Merge pull request #3373 from lonvia/restrict-man-made
[nominatim.git] / nominatim / tokenizer / sanitizers / clean_housenumbers.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 Sanitizer that preprocesses address tags for house numbers. The sanitizer
9 allows to
10
11 * define which tags are to be considered house numbers (see 'filter-kind')
12 * split house number lists into individual numbers (see 'delimiters')
13
14 Arguments:
15     delimiters: Define the set of characters to be used for
16                 splitting a list of house numbers into parts. (default: ',;')
17     filter-kind: Define the address tags that are considered to be a
18                  house number. Either takes a single string or a list of strings,
19                  where each string is a regular expression. An address item
20                  is considered a house number if the 'kind' fully matches any
21                  of the given regular expressions. (default: 'housenumber')
22     convert-to-name: Define house numbers that should be treated as a name
23                      instead of a house number. Either takes a single string
24                      or a list of strings, where each string is a regular
25                      expression that must match the full house number value.
26 """
27 from typing import Callable, Iterator, List
28
29 from nominatim.tokenizer.sanitizers.base import ProcessInfo
30 from nominatim.data.place_name import PlaceName
31 from nominatim.tokenizer.sanitizers.config import SanitizerConfig
32
33 class _HousenumberSanitizer:
34
35     def __init__(self, config: SanitizerConfig) -> None:
36         self.filter_kind = config.get_filter('filter-kind', ['housenumber'])
37         self.split_regexp = config.get_delimiter()
38
39         self.filter_name = config.get_filter('convert-to-name', 'FAIL_ALL')
40
41
42     def __call__(self, obj: ProcessInfo) -> None:
43         if not obj.address:
44             return
45
46         new_address: List[PlaceName] = []
47         for item in obj.address:
48             if self.filter_kind(item.kind):
49                 if self.filter_name(item.name):
50                     obj.names.append(item.clone(kind='housenumber'))
51                 else:
52                     new_address.extend(item.clone(kind='housenumber', name=n)
53                                        for n in self.sanitize(item.name))
54             else:
55                 # Don't touch other address items.
56                 new_address.append(item)
57
58         obj.address = new_address
59
60
61     def sanitize(self, value: str) -> Iterator[str]:
62         """ Extract housenumbers in a regularized format from an OSM value.
63
64             The function works as a generator that yields all valid housenumbers
65             that can be created from the value.
66         """
67         for hnr in self.split_regexp.split(value):
68             if hnr:
69                 yield from self._regularize(hnr)
70
71
72     def _regularize(self, hnr: str) -> Iterator[str]:
73         yield hnr
74
75
76 def create(config: SanitizerConfig) -> Callable[[ProcessInfo], None]:
77     """ Create a housenumber processing function.
78     """
79
80     return _HousenumberSanitizer(config)