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 cleans and normalizes house numbers.
11 delimiters: Define the set of characters to be used for
12 splitting a list of house numbers into parts. (default: ',;')
13 filter-kind: Define the address tags that are considered to be a
14 house number. Either takes a single string or a list of strings,
15 where each string is a regular expression. An address item
16 is considered a house number if the 'kind' fully matches any
17 of the given regular expressions. (default: 'housenumber')
20 from nominatim.tokenizer.sanitizers.helpers import create_split_regex, create_kind_filter
22 class _HousenumberSanitizer:
24 def __init__(self, config):
25 self.filter_kind = create_kind_filter(config, 'housenumber')
26 self.split_regexp = create_split_regex(config)
29 def __call__(self, obj):
34 for item in obj.address:
35 if self.filter_kind(item):
36 new_address.extend(item.clone(kind='housenumber', name=n) for n in self.sanitize(item.name))
38 # Don't touch other address items.
39 new_address.append(item)
41 obj.address = new_address
44 def sanitize(self, value):
45 """ Extract housenumbers in a regularized format from an OSM value.
47 The function works as a generator that yields all valid housenumbers
48 that can be created from the value.
50 for hnr in self.split_regexp.split(value):
52 yield from self._regularize(hnr)
55 def _regularize(self, hnr):
60 """ Create a housenumber processing function.
63 return _HousenumberSanitizer(config)