2 Tests for import name normalisation and variant generation.
6 from icu import Transliterator
8 import nominatim.tokenizer.token_analysis.generic as module
9 from nominatim.errors import UsageError
11 DEFAULT_NORMALIZATION = """ :: NFD ();
13 [[:Nonspacing Mark:] [:Cf:]] >;
15 [[:Punctuation:][:Space:]]+ > ' ';
19 DEFAULT_TRANSLITERATION = """ :: Latin ();
23 def make_analyser(*variants, variant_only=False):
24 rules = { 'analyzer': 'generic', 'variants': [{'words': variants}]}
26 rules['mode'] = 'variant-only'
27 config = module.configure(rules, DEFAULT_NORMALIZATION)
28 trans = Transliterator.createFromRules("test_trans", DEFAULT_TRANSLITERATION)
30 return module.create(trans, config)
33 def get_normalized_variants(proc, name):
34 norm = Transliterator.createFromRules("test_norm", DEFAULT_NORMALIZATION)
35 return proc.get_variants_ascii(norm.transliterate(name).strip())
38 def test_no_variants():
39 rules = { 'analyzer': 'generic' }
40 config = module.configure(rules, DEFAULT_NORMALIZATION)
41 trans = Transliterator.createFromRules("test_trans", DEFAULT_TRANSLITERATION)
43 proc = module.create(trans, config)
45 assert get_normalized_variants(proc, '大德!') == ['dà dé']
48 def test_variants_empty():
49 proc = make_analyser('saint -> 🜵', 'street -> st')
51 assert get_normalized_variants(proc, '🜵') == []
52 assert get_normalized_variants(proc, '🜳') == []
53 assert get_normalized_variants(proc, 'saint') == ['saint']
57 (('~strasse,~straße -> str', '~weg => weg'), "hallo", {'hallo'}),
58 (('weg => wg',), "holzweg", {'holzweg'}),
59 (('weg -> wg',), "holzweg", {'holzweg'}),
60 (('~weg => weg',), "holzweg", {'holz weg', 'holzweg'}),
61 (('~weg -> weg',), "holzweg", {'holz weg', 'holzweg'}),
62 (('~weg => w',), "holzweg", {'holz w', 'holzw'}),
63 (('~weg -> w',), "holzweg", {'holz weg', 'holzweg', 'holz w', 'holzw'}),
64 (('~weg => weg',), "Meier Weg", {'meier weg', 'meierweg'}),
65 (('~weg -> weg',), "Meier Weg", {'meier weg', 'meierweg'}),
66 (('~weg => w',), "Meier Weg", {'meier w', 'meierw'}),
67 (('~weg -> w',), "Meier Weg", {'meier weg', 'meierweg', 'meier w', 'meierw'}),
68 (('weg => wg',), "Meier Weg", {'meier wg'}),
69 (('weg -> wg',), "Meier Weg", {'meier weg', 'meier wg'}),
70 (('~strasse,~straße -> str', '~weg => weg'), "Bauwegstraße",
71 {'bauweg straße', 'bauweg str', 'bauwegstraße', 'bauwegstr'}),
72 (('am => a', 'bach => b'), "am bach", {'a b'}),
73 (('am => a', '~bach => b'), "am bach", {'a b'}),
74 (('am -> a', '~bach -> b'), "am bach", {'am bach', 'a bach', 'am b', 'a b'}),
75 (('am -> a', '~bach -> b'), "ambach", {'ambach', 'am bach', 'amb', 'am b'}),
76 (('saint -> s,st', 'street -> st'), "Saint Johns Street",
77 {'saint johns street', 's johns street', 'st johns street',
78 'saint johns st', 's johns st', 'st johns st'}),
79 (('river$ -> r',), "River Bend Road", {'river bend road'}),
80 (('river$ -> r',), "Bent River", {'bent river', 'bent r'}),
81 (('^north => n',), "North 2nd Street", {'n 2nd street'}),
82 (('^north => n',), "Airport North", {'airport north'}),
83 (('am -> a',), "am am am am am am am am", {'am am am am am am am am'}),
84 (('am => a',), "am am am am am am am am", {'a a a a a a a a'})
87 @pytest.mark.parametrize("rules,name,variants", VARIANT_TESTS)
88 def test_variants(rules, name, variants):
89 proc = make_analyser(*rules)
91 result = get_normalized_variants(proc, name)
93 assert len(result) == len(set(result))
94 assert set(get_normalized_variants(proc, name)) == variants
97 VARIANT_ONLY_TESTS = [
98 (('weg => wg',), "hallo", set()),
99 (('weg => wg',), "Meier Weg", {'meier wg'}),
100 (('weg -> wg',), "Meier Weg", {'meier wg'}),
103 @pytest.mark.parametrize("rules,name,variants", VARIANT_ONLY_TESTS)
104 def test_variants_only(rules, name, variants):
105 proc = make_analyser(*rules, variant_only=True)
107 result = get_normalized_variants(proc, name)
109 assert len(result) == len(set(result))
110 assert set(get_normalized_variants(proc, name)) == variants
113 class TestGetReplacements:
116 def configure_rules(*variants):
117 rules = { 'analyzer': 'generic', 'variants': [{'words': variants}]}
118 return module.configure(rules, DEFAULT_NORMALIZATION)
121 def get_replacements(self, *variants):
122 config = self.configure_rules(*variants)
124 return sorted((k, sorted(v)) for k,v in config['replacements'])
127 @pytest.mark.parametrize("variant", ['foo > bar', 'foo -> bar -> bar',
128 '~foo~ -> bar', 'fo~ o -> bar'])
129 def test_invalid_variant_description(self, variant):
130 with pytest.raises(UsageError):
131 self.configure_rules(variant)
134 @pytest.mark.parametrize("rule", ["!!! -> bar", "bar => !!!"])
135 def test_ignore_unnormalizable_terms(self, rule):
136 repl = self.get_replacements(rule)
141 def test_add_full(self):
142 repl = self.get_replacements("foo -> bar")
144 assert repl == [(' foo ', [' bar', ' foo'])]
147 def test_replace_full(self):
148 repl = self.get_replacements("foo => bar")
150 assert repl == [(' foo ', [' bar'])]
153 def test_add_suffix_no_decompose(self):
154 repl = self.get_replacements("~berg |-> bg")
156 assert repl == [(' berg ', [' berg', ' bg']),
157 ('berg ', ['berg', 'bg'])]
160 def test_replace_suffix_no_decompose(self):
161 repl = self.get_replacements("~berg |=> bg")
163 assert repl == [(' berg ', [' bg']),('berg ', ['bg'])]
166 def test_add_suffix_decompose(self):
167 repl = self.get_replacements("~berg -> bg")
169 assert repl == [(' berg ', [' berg', ' bg', 'berg', 'bg']),
170 ('berg ', [' berg', ' bg', 'berg', 'bg'])]
173 def test_replace_suffix_decompose(self):
174 repl = self.get_replacements("~berg => bg")
176 assert repl == [(' berg ', [' bg', 'bg']),
177 ('berg ', [' bg', 'bg'])]
180 def test_add_prefix_no_compose(self):
181 repl = self.get_replacements("hinter~ |-> hnt")
183 assert repl == [(' hinter', [' hinter', ' hnt']),
184 (' hinter ', [' hinter', ' hnt'])]
187 def test_replace_prefix_no_compose(self):
188 repl = self.get_replacements("hinter~ |=> hnt")
190 assert repl == [(' hinter', [' hnt']), (' hinter ', [' hnt'])]
193 def test_add_prefix_compose(self):
194 repl = self.get_replacements("hinter~-> h")
196 assert repl == [(' hinter', [' h', ' h ', ' hinter', ' hinter ']),
197 (' hinter ', [' h', ' h', ' hinter', ' hinter'])]
200 def test_replace_prefix_compose(self):
201 repl = self.get_replacements("hinter~=> h")
203 assert repl == [(' hinter', [' h', ' h ']),
204 (' hinter ', [' h', ' h'])]
207 def test_add_beginning_only(self):
208 repl = self.get_replacements("^Premier -> Pr")
210 assert repl == [('^ premier ', ['^ pr', '^ premier'])]
213 def test_replace_beginning_only(self):
214 repl = self.get_replacements("^Premier => Pr")
216 assert repl == [('^ premier ', ['^ pr'])]
219 def test_add_final_only(self):
220 repl = self.get_replacements("road$ -> rd")
222 assert repl == [(' road ^', [' rd ^', ' road ^'])]
225 def test_replace_final_only(self):
226 repl = self.get_replacements("road$ => rd")
228 assert repl == [(' road ^', [' rd ^'])]
231 def test_decompose_only(self):
232 repl = self.get_replacements("~foo -> foo")
234 assert repl == [(' foo ', [' foo', 'foo']),
235 ('foo ', [' foo', 'foo'])]
238 def test_add_suffix_decompose_end_only(self):
239 repl = self.get_replacements("~berg |-> bg", "~berg$ -> bg")
241 assert repl == [(' berg ', [' berg', ' bg']),
242 (' berg ^', [' berg ^', ' bg ^', 'berg ^', 'bg ^']),
243 ('berg ', ['berg', 'bg']),
244 ('berg ^', [' berg ^', ' bg ^', 'berg ^', 'bg ^'])]
247 def test_replace_suffix_decompose_end_only(self):
248 repl = self.get_replacements("~berg |=> bg", "~berg$ => bg")
250 assert repl == [(' berg ', [' bg']),
251 (' berg ^', [' bg ^', 'bg ^']),
253 ('berg ^', [' bg ^', 'bg ^'])]
256 @pytest.mark.parametrize('rule', ["~berg,~burg -> bg",
257 "~berg, ~burg -> bg",
258 "~berg,,~burg -> bg"])
259 def test_add_multiple_suffix(self, rule):
260 repl = self.get_replacements(rule)
262 assert repl == [(' berg ', [' berg', ' bg', 'berg', 'bg']),
263 (' burg ', [' bg', ' burg', 'bg', 'burg']),
264 ('berg ', [' berg', ' bg', 'berg', 'bg']),
265 ('burg ', [' bg', ' burg', 'bg', 'burg'])]