1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Sanitizer that preprocesses address tags for house numbers. The sanitizer
11 * define which tags are to be considered house numbers (see 'filter-kind')
12 * split house number lists into individual numbers (see 'delimiters')
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.
29 class _HousenumberSanitizer:
31 def __init__(self, config):
32 self.filter_kind = config.get_filter_kind('housenumber')
33 self.split_regexp = config.get_delimiter()
35 nameregexps = config.get_string_list('convert-to-name', [])
36 self.is_name_regexp = [re.compile(r) for r in nameregexps]
40 def __call__(self, obj):
45 for item in obj.address:
46 if self.filter_kind(item):
47 if self._treat_as_name(item.name):
48 obj.names.append(item.clone(kind='housenumber'))
50 new_address.extend(item.clone(kind='housenumber', name=n)
51 for n in self.sanitize(item.name))
53 # Don't touch other address items.
54 new_address.append(item)
56 obj.address = new_address
59 def sanitize(self, value):
60 """ Extract housenumbers in a regularized format from an OSM value.
62 The function works as a generator that yields all valid housenumbers
63 that can be created from the value.
65 for hnr in self.split_regexp.split(value):
67 yield from self._regularize(hnr)
75 def _treat_as_name(self, housenumber):
76 return any(r.fullmatch(housenumber) is not None for r in self.is_name_regexp)
80 """ Create a housenumber processing function.
83 return _HousenumberSanitizer(config)