2 Name processor for tagging the langauge of the name
6 from nominatim.tools import country_info
8 class _AnalyzerByLanguage:
9 """ Processor for tagging the language of names in a place.
12 def __init__(self, config):
13 if 'filter-kind' in config:
14 self.regexes = [re.compile(regex) for regex in config['filter-kind']]
18 self.replace = config.get('mode', 'replace') != 'append'
19 self.whitelist = config.get('whitelist')
21 self.__compute_default_languages(config.get('use-defaults', 'no'))
24 def __compute_default_languages(self, use_defaults):
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':
32 self.deflangs[ccode] = [l for l in clangs if l in self.whitelist]
34 self.deflangs[ccode] = clangs
37 def _kind_matches(self, kind):
38 if self.regexes is None:
41 return any(regex.fullmatch(kind) for regex in self.regexes)
44 def _suffix_matches(self, suffix):
45 if self.whitelist is None:
46 return len(suffix) in (2, 3) and suffix.islower()
48 return suffix in self.whitelist
51 def __call__(self, obj):
57 for name in (n for n in obj.names
58 if not n.has_attr('analyzer') and self._kind_matches(n.kind)):
60 langs = [name.suffix] if self._suffix_matches(name.suffix) else None
62 langs = self.deflangs.get(obj.place.country_code)
67 name.set_attr('analyzer', langs[0])
69 more_names.append(name.clone(attr={'analyzer': langs[0]}))
71 more_names.extend(name.clone(attr={'analyzer': l}) for l in langs[1:])
73 obj.names.extend(more_names)
77 """ Create a function that sets the analyzer property depending on the
78 language of the tag. The language is taken from the suffix.
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.
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
91 'mode' hay be 'replace' (the default) or 'append' and configures if
92 the original name (without any analyzer tagged) is retained.
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'.
98 return _AnalyzerByLanguage(config)