1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2024 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Tests for import name normalisation and variant generation.
12 from icu import Transliterator
14 import nominatim_db.tokenizer.token_analysis.generic as module
15 from nominatim_db.errors import UsageError
17 DEFAULT_NORMALIZATION = """ :: NFD ();
19 [[:Nonspacing Mark:] [:Cf:]] >;
21 [[:Punctuation:][:Space:]]+ > ' ';
25 DEFAULT_TRANSLITERATION = """ :: Latin ();
29 def make_analyser(*variants, variant_only=False):
30 rules = { 'analyzer': 'generic', 'variants': [{'words': variants}]}
32 rules['mode'] = 'variant-only'
33 trans = Transliterator.createFromRules("test_trans", DEFAULT_TRANSLITERATION)
34 norm = Transliterator.createFromRules("test_norm", DEFAULT_NORMALIZATION)
35 config = module.configure(rules, norm, trans)
37 return module.create(norm, trans, config)
40 def get_normalized_variants(proc, name):
41 norm = Transliterator.createFromRules("test_norm", DEFAULT_NORMALIZATION)
42 return proc.compute_variants(norm.transliterate(name).strip())
45 def test_no_variants():
46 rules = { 'analyzer': 'generic' }
47 trans = Transliterator.createFromRules("test_trans", DEFAULT_TRANSLITERATION)
48 norm = Transliterator.createFromRules("test_norm", DEFAULT_NORMALIZATION)
49 config = module.configure(rules, norm, trans)
51 proc = module.create(norm, trans, config)
53 assert get_normalized_variants(proc, '大德!') == ['dà dé']
56 def test_variants_empty():
57 proc = make_analyser('saint -> 🜵', 'street -> st')
59 assert get_normalized_variants(proc, '🜵') == []
60 assert get_normalized_variants(proc, '🜳') == []
61 assert get_normalized_variants(proc, 'saint') == ['saint']
65 (('~strasse,~straße -> str', '~weg => weg'), "hallo", {'hallo'}),
66 (('weg => wg',), "holzweg", {'holzweg'}),
67 (('weg -> wg',), "holzweg", {'holzweg'}),
68 (('~weg => weg',), "holzweg", {'holz weg', 'holzweg'}),
69 (('~weg -> weg',), "holzweg", {'holz weg', 'holzweg'}),
70 (('~weg => w',), "holzweg", {'holz w', 'holzw'}),
71 (('~weg -> w',), "holzweg", {'holz weg', 'holzweg', 'holz w', 'holzw'}),
72 (('~weg => weg',), "Meier Weg", {'meier weg', 'meierweg'}),
73 (('~weg -> weg',), "Meier Weg", {'meier weg', 'meierweg'}),
74 (('~weg => w',), "Meier Weg", {'meier w', 'meierw'}),
75 (('~weg -> w',), "Meier Weg", {'meier weg', 'meierweg', 'meier w', 'meierw'}),
76 (('weg => wg',), "Meier Weg", {'meier wg'}),
77 (('weg -> wg',), "Meier Weg", {'meier weg', 'meier wg'}),
78 (('~strasse,~straße -> str', '~weg => weg'), "Bauwegstraße",
79 {'bauweg straße', 'bauweg str', 'bauwegstraße', 'bauwegstr'}),
80 (('am => a', 'bach => b'), "am bach", {'a b'}),
81 (('am => a', '~bach => b'), "am bach", {'a b'}),
82 (('am -> a', '~bach -> b'), "am bach", {'am bach', 'a bach', 'am b', 'a b'}),
83 (('am -> a', '~bach -> b'), "ambach", {'ambach', 'am bach', 'amb', 'am b'}),
84 (('saint -> s,st', 'street -> st'), "Saint Johns Street",
85 {'saint johns street', 's johns street', 'st johns street',
86 'saint johns st', 's johns st', 'st johns st'}),
87 (('river$ -> r',), "River Bend Road", {'river bend road'}),
88 (('river$ -> r',), "Bent River", {'bent river', 'bent r'}),
89 (('^north => n',), "North 2nd Street", {'n 2nd street'}),
90 (('^north => n',), "Airport North", {'airport north'}),
91 (('am -> a',), "am am am am am am am am", {'am am am am am am am am'}),
92 (('am => a',), "am am am am am am am am", {'a a a a a a a a'})
95 @pytest.mark.parametrize("rules,name,variants", VARIANT_TESTS)
96 def test_variants(rules, name, variants):
97 proc = make_analyser(*rules)
99 result = get_normalized_variants(proc, name)
101 assert len(result) == len(set(result))
102 assert set(get_normalized_variants(proc, name)) == variants
105 VARIANT_ONLY_TESTS = [
106 (('weg => wg',), "hallo", set()),
107 (('weg => wg',), "Meier Weg", {'meier wg'}),
108 (('weg -> wg',), "Meier Weg", {'meier wg'}),
111 @pytest.mark.parametrize("rules,name,variants", VARIANT_ONLY_TESTS)
112 def test_variants_only(rules, name, variants):
113 proc = make_analyser(*rules, variant_only=True)
115 result = get_normalized_variants(proc, name)
117 assert len(result) == len(set(result))
118 assert set(get_normalized_variants(proc, name)) == variants
121 class TestGetReplacements:
124 def configure_rules(*variants):
125 rules = { 'analyzer': 'generic', 'variants': [{'words': variants}]}
126 trans = Transliterator.createFromRules("test_trans", DEFAULT_TRANSLITERATION)
127 norm = Transliterator.createFromRules("test_norm", DEFAULT_NORMALIZATION)
128 return module.configure(rules, norm, trans)
131 def get_replacements(self, *variants):
132 config = self.configure_rules(*variants)
134 return sorted((k, sorted(v)) for k,v in config['replacements'])
137 @pytest.mark.parametrize("variant", ['foo > bar', 'foo -> bar -> bar',
138 '~foo~ -> bar', 'fo~ o -> bar'])
139 def test_invalid_variant_description(self, variant):
140 with pytest.raises(UsageError):
141 self.configure_rules(variant)
144 @pytest.mark.parametrize("rule", ["!!! -> bar", "bar => !!!"])
145 def test_ignore_unnormalizable_terms(self, rule):
146 repl = self.get_replacements(rule)
151 def test_add_full(self):
152 repl = self.get_replacements("foo -> bar")
154 assert repl == [(' foo ', [' bar', ' foo'])]
157 def test_replace_full(self):
158 repl = self.get_replacements("foo => bar")
160 assert repl == [(' foo ', [' bar'])]
163 def test_add_suffix_no_decompose(self):
164 repl = self.get_replacements("~berg |-> bg")
166 assert repl == [(' berg ', [' berg', ' bg']),
167 ('berg ', ['berg', 'bg'])]
170 def test_replace_suffix_no_decompose(self):
171 repl = self.get_replacements("~berg |=> bg")
173 assert repl == [(' berg ', [' bg']),('berg ', ['bg'])]
176 def test_add_suffix_decompose(self):
177 repl = self.get_replacements("~berg -> bg")
179 assert repl == [(' berg ', [' berg', ' bg', 'berg', 'bg']),
180 ('berg ', [' berg', ' bg', 'berg', 'bg'])]
183 def test_replace_suffix_decompose(self):
184 repl = self.get_replacements("~berg => bg")
186 assert repl == [(' berg ', [' bg', 'bg']),
187 ('berg ', [' bg', 'bg'])]
190 def test_add_prefix_no_compose(self):
191 repl = self.get_replacements("hinter~ |-> hnt")
193 assert repl == [(' hinter', [' hinter', ' hnt']),
194 (' hinter ', [' hinter', ' hnt'])]
197 def test_replace_prefix_no_compose(self):
198 repl = self.get_replacements("hinter~ |=> hnt")
200 assert repl == [(' hinter', [' hnt']), (' hinter ', [' hnt'])]
203 def test_add_prefix_compose(self):
204 repl = self.get_replacements("hinter~-> h")
206 assert repl == [(' hinter', [' h', ' h ', ' hinter', ' hinter ']),
207 (' hinter ', [' h', ' h', ' hinter', ' hinter'])]
210 def test_replace_prefix_compose(self):
211 repl = self.get_replacements("hinter~=> h")
213 assert repl == [(' hinter', [' h', ' h ']),
214 (' hinter ', [' h', ' h'])]
217 def test_add_beginning_only(self):
218 repl = self.get_replacements("^Premier -> Pr")
220 assert repl == [('^ premier ', ['^ pr', '^ premier'])]
223 def test_replace_beginning_only(self):
224 repl = self.get_replacements("^Premier => Pr")
226 assert repl == [('^ premier ', ['^ pr'])]
229 def test_add_final_only(self):
230 repl = self.get_replacements("road$ -> rd")
232 assert repl == [(' road ^', [' rd ^', ' road ^'])]
235 def test_replace_final_only(self):
236 repl = self.get_replacements("road$ => rd")
238 assert repl == [(' road ^', [' rd ^'])]
241 def test_decompose_only(self):
242 repl = self.get_replacements("~foo -> foo")
244 assert repl == [(' foo ', [' foo', 'foo']),
245 ('foo ', [' foo', 'foo'])]
248 def test_add_suffix_decompose_end_only(self):
249 repl = self.get_replacements("~berg |-> bg", "~berg$ -> bg")
251 assert repl == [(' berg ', [' berg', ' bg']),
252 (' berg ^', [' berg ^', ' bg ^', 'berg ^', 'bg ^']),
253 ('berg ', ['berg', 'bg']),
254 ('berg ^', [' berg ^', ' bg ^', 'berg ^', 'bg ^'])]
257 def test_replace_suffix_decompose_end_only(self):
258 repl = self.get_replacements("~berg |=> bg", "~berg$ => bg")
260 assert repl == [(' berg ', [' bg']),
261 (' berg ^', [' bg ^', 'bg ^']),
263 ('berg ^', [' bg ^', 'bg ^'])]
266 @pytest.mark.parametrize('rule', ["~berg,~burg -> bg",
267 "~berg, ~burg -> bg",
268 "~berg,,~burg -> bg"])
269 def test_add_multiple_suffix(self, rule):
270 repl = self.get_replacements(rule)
272 assert repl == [(' berg ', [' berg', ' bg', 'berg', 'bg']),
273 (' burg ', [' bg', ' burg', 'bg', 'burg']),
274 ('berg ', [' berg', ' bg', 'berg', 'bg']),
275 ('burg ', [' bg', ' burg', 'bg', 'burg'])]