]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/tokenizer/token_analysis/housenumbers.py
add new analyser for houenumbers
[nominatim.git] / nominatim / tokenizer / token_analysis / 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 Specialized processor for housenumbers. Analyses common housenumber patterns
9 and creates variants for them.
10 """
11 import re
12
13 from nominatim.tokenizer.token_analysis.generic_mutation import MutationVariantGenerator
14
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)')
18
19 ### Configuration section
20
21 def configure(rules, normalization_rules):
22     """ All behaviour is currently hard-coded.
23     """
24     return None
25
26 ### Analysis section
27
28 def create(normalizer, transliterator, config):
29     """ Create a new token analysis instance for this module.
30     """
31     return HousenumberTokenAnalysis(normalizer, transliterator)
32
33
34 class HousenumberTokenAnalysis:
35     """ Detects common housenumber patterns and normalizes them.
36     """
37     def __init__(self, norm, trans):
38         self.norm = norm
39         self.trans = trans
40
41         self.mutator = MutationVariantGenerator('␣', (' ', ''))
42
43     def normalize(self, name):
44         """ Return the normalized form of the housenumber.
45         """
46         # shortcut for number-only numbers, which make up 90% of the data.
47         if RE_NON_DIGIT.search(name) is None:
48             return name
49
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)
53
54         return norm
55
56     def get_variants_ascii(self, norm_name):
57         """ Compute the spelling variants for the given normalized housenumber.
58
59             Generates variants for optional spaces (marked with '␣').
60         """
61         return list(self.mutator.generate([norm_name]))