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')
24 from nominatim.tokenizer.sanitizers.helpers import create_split_regex, create_kind_filter
26 class _HousenumberSanitizer:
28 def __init__(self, config):
29 self.filter_kind = create_kind_filter(config, 'housenumber')
30 self.split_regexp = create_split_regex(config)
33 def __call__(self, obj):
38 for item in obj.address:
39 if self.filter_kind(item):
40 new_address.extend(item.clone(kind='housenumber', name=n)
41 for n in self.sanitize(item.name))
43 # Don't touch other address items.
44 new_address.append(item)
46 obj.address = new_address
49 def sanitize(self, value):
50 """ Extract housenumbers in a regularized format from an OSM value.
52 The function works as a generator that yields all valid housenumbers
53 that can be created from the value.
55 for hnr in self.split_regexp.split(value):
57 yield from self._regularize(hnr)
66 """ Create a housenumber processing function.
69 return _HousenumberSanitizer(config)