]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/tokenizer/sanitizers/tag_analyzer_by_language.py
c0ddd8360275891e967f1511da9f2fdd18c01b0c
[nominatim.git] / nominatim / tokenizer / sanitizers / tag_analyzer_by_language.py
1 """
2 Name processor for tagging the langauge of the name
3 """
4 import re
5
6 from nominatim.tools import country_info
7
8 class _AnalyzerByLanguage:
9     """ Processor for tagging the language of names in a place.
10     """
11
12     def __init__(self, config):
13         if 'filter-kind' in config:
14             self.regexes = [re.compile(regex) for regex in config['filter-kind']]
15         else:
16             self.regexes = None
17
18         self.replace = config.get('mode', 'replace') != 'append'
19         self.whitelist = config.get('whitelist')
20
21         self.__compute_default_languages(config.get('use-defaults', 'no'))
22
23
24     def __compute_default_languages(self, use_defaults):
25         self.deflangs = {}
26
27         if use_defaults in ('mono', 'all'):
28             for ccode, prop in country_info.iterate():
29                 clangs = prop['languages']
30                 if len(clangs) == 1 or use_defaults == 'all':
31                     if self.whitelist:
32                         self.deflangs[ccode] = [l for l in clangs if l in self.whitelist]
33                     else:
34                         self.deflangs[ccode] = clangs
35
36
37     def _kind_matches(self, kind):
38         if self.regexes is None:
39             return True
40
41         return any(regex.fullmatch(kind) for regex in self.regexes)
42
43
44     def _suffix_matches(self, suffix):
45         if self.whitelist is None:
46             return len(suffix) in (2, 3) and suffix.islower()
47
48         return suffix in self.whitelist
49
50
51     def __call__(self, obj):
52         if not obj.names:
53             return
54
55         more_names = []
56
57         for name in (n for n in obj.names
58                      if not n.has_attr('analyzer') and self._kind_matches(n.kind)):
59             if name.suffix:
60                 langs = [name.suffix] if self._suffix_matches(name.suffix) else None
61             else:
62                 langs = self.deflangs.get(obj.place.country_code)
63
64
65             if langs:
66                 if self.replace:
67                     name.set_attr('analyzer', langs[0])
68                 else:
69                     more_names.append(name.clone(attr={'analyzer': langs[0]}))
70
71                 more_names.extend(name.clone(attr={'analyzer': l}) for l in langs[1:])
72
73         obj.names.extend(more_names)
74
75
76 def create(config):
77     """ Create a function that sets the analyzer property depending on the
78         language of the tag. The language is taken from the suffix.
79
80         To restrict the set of languages that should be tagged, use
81         'whitelist'. A list of acceptable suffixes. When unset, all 2- and
82         3-letter codes are accepted.
83
84         'use-defaults' configures what happens when the name has no suffix
85         with a language tag. When set to 'all', a variant is created for
86         each on the spoken languages in the country the feature is in. When
87         set to 'mono', a variant is created, when only one language is spoken
88         in the country. The default is, to do nothing with the default languages
89         of a country.
90
91         'mode' hay be 'replace' (the default) or 'append' and configures if
92         the original name (without any analyzer tagged) is retained.
93
94         With 'filter-kind' the set of names the sanitizer should be applied
95         to can be retricted to the given patterns of 'kind'. It expects a
96         list of regular expression to be matched against 'kind'.
97     """
98     return _AnalyzerByLanguage(config)