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 Specialized processor for housenumbers. Analyses common housenumber patterns
9 and creates variants for them.
13 from nominatim.tokenizer.token_analysis.generic_mutation import MutationVariantGenerator
15 RE_NON_DIGIT = re.compile('[^0-9]')
16 RE_DIGIT_ALPHA = re.compile(r'(\d)\s*([^\d\s␣])')
17 RE_ALPHA_DIGIT = re.compile(r'([^\s\d␣])\s*(\d)')
19 ### Configuration section
21 def configure(rules, normalization_rules):
22 """ All behaviour is currently hard-coded.
28 def create(normalizer, transliterator, config):
29 """ Create a new token analysis instance for this module.
31 return HousenumberTokenAnalysis(normalizer, transliterator)
34 class HousenumberTokenAnalysis:
35 """ Detects common housenumber patterns and normalizes them.
37 def __init__(self, norm, trans):
41 self.mutator = MutationVariantGenerator('␣', (' ', ''))
43 def normalize(self, name):
44 """ Return the normalized form of the housenumber.
46 # shortcut for number-only numbers, which make up 90% of the data.
47 if RE_NON_DIGIT.search(name) is None:
50 norm = self.trans.transliterate(self.norm.transliterate(name))
51 norm = RE_DIGIT_ALPHA.sub(r'\1␣\2', norm)
52 norm = RE_ALPHA_DIGIT.sub(r'\1␣\2', norm)
56 def get_variants_ascii(self, norm_name):
57 """ Compute the spelling variants for the given normalized housenumber.
59 Generates variants for optional spaces (marked with '␣').
61 return list(self.mutator.generate([norm_name]))