1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2022 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.tokenizer.token_analysis.generic as module
15 from nominatim.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 config = module.configure(rules, DEFAULT_NORMALIZATION)
34 trans = Transliterator.createFromRules("test_trans", DEFAULT_TRANSLITERATION)
36 return module.create(trans, config)
39 def get_normalized_variants(proc, name):
40 norm = Transliterator.createFromRules("test_norm", DEFAULT_NORMALIZATION)
41 return proc.get_variants_ascii(norm.transliterate(name).strip())
44 def test_no_variants():
45 rules = { 'analyzer': 'generic' }
46 config = module.configure(rules, DEFAULT_NORMALIZATION)
47 trans = Transliterator.createFromRules("test_trans", DEFAULT_TRANSLITERATION)
49 proc = module.create(trans, config)
51 assert get_normalized_variants(proc, '大德!') == ['dà dé']
54 def test_variants_empty():
55 proc = make_analyser('saint -> 🜵', 'street -> st')
57 assert get_normalized_variants(proc, '🜵') == []
58 assert get_normalized_variants(proc, '🜳') == []
59 assert get_normalized_variants(proc, 'saint') == ['saint']
63 (('~strasse,~straße -> str', '~weg => weg'), "hallo", {'hallo'}),
64 (('weg => wg',), "holzweg", {'holzweg'}),
65 (('weg -> wg',), "holzweg", {'holzweg'}),
66 (('~weg => weg',), "holzweg", {'holz weg', 'holzweg'}),
67 (('~weg -> weg',), "holzweg", {'holz weg', 'holzweg'}),
68 (('~weg => w',), "holzweg", {'holz w', 'holzw'}),
69 (('~weg -> w',), "holzweg", {'holz weg', 'holzweg', 'holz w', 'holzw'}),
70 (('~weg => weg',), "Meier Weg", {'meier weg', 'meierweg'}),
71 (('~weg -> weg',), "Meier Weg", {'meier weg', 'meierweg'}),
72 (('~weg => w',), "Meier Weg", {'meier w', 'meierw'}),
73 (('~weg -> w',), "Meier Weg", {'meier weg', 'meierweg', 'meier w', 'meierw'}),
74 (('weg => wg',), "Meier Weg", {'meier wg'}),
75 (('weg -> wg',), "Meier Weg", {'meier weg', 'meier wg'}),
76 (('~strasse,~straße -> str', '~weg => weg'), "Bauwegstraße",
77 {'bauweg straße', 'bauweg str', 'bauwegstraße', 'bauwegstr'}),
78 (('am => a', 'bach => b'), "am bach", {'a b'}),
79 (('am => a', '~bach => b'), "am bach", {'a b'}),
80 (('am -> a', '~bach -> b'), "am bach", {'am bach', 'a bach', 'am b', 'a b'}),
81 (('am -> a', '~bach -> b'), "ambach", {'ambach', 'am bach', 'amb', 'am b'}),
82 (('saint -> s,st', 'street -> st'), "Saint Johns Street",
83 {'saint johns street', 's johns street', 'st johns street',
84 'saint johns st', 's johns st', 'st johns st'}),
85 (('river$ -> r',), "River Bend Road", {'river bend road'}),
86 (('river$ -> r',), "Bent River", {'bent river', 'bent r'}),
87 (('^north => n',), "North 2nd Street", {'n 2nd street'}),
88 (('^north => n',), "Airport North", {'airport north'}),
89 (('am -> a',), "am am am am am am am am", {'am am am am am am am am'}),
90 (('am => a',), "am am am am am am am am", {'a a a a a a a a'})
93 @pytest.mark.parametrize("rules,name,variants", VARIANT_TESTS)
94 def test_variants(rules, name, variants):
95 proc = make_analyser(*rules)
97 result = get_normalized_variants(proc, name)
99 assert len(result) == len(set(result))
100 assert set(get_normalized_variants(proc, name)) == variants
103 VARIANT_ONLY_TESTS = [
104 (('weg => wg',), "hallo", set()),
105 (('weg => wg',), "Meier Weg", {'meier wg'}),
106 (('weg -> wg',), "Meier Weg", {'meier wg'}),
109 @pytest.mark.parametrize("rules,name,variants", VARIANT_ONLY_TESTS)
110 def test_variants_only(rules, name, variants):
111 proc = make_analyser(*rules, variant_only=True)
113 result = get_normalized_variants(proc, name)
115 assert len(result) == len(set(result))
116 assert set(get_normalized_variants(proc, name)) == variants
119 class TestGetReplacements:
122 def configure_rules(*variants):
123 rules = { 'analyzer': 'generic', 'variants': [{'words': variants}]}
124 return module.configure(rules, DEFAULT_NORMALIZATION)
127 def get_replacements(self, *variants):
128 config = self.configure_rules(*variants)
130 return sorted((k, sorted(v)) for k,v in config['replacements'])
133 @pytest.mark.parametrize("variant", ['foo > bar', 'foo -> bar -> bar',
134 '~foo~ -> bar', 'fo~ o -> bar'])
135 def test_invalid_variant_description(self, variant):
136 with pytest.raises(UsageError):
137 self.configure_rules(variant)
140 @pytest.mark.parametrize("rule", ["!!! -> bar", "bar => !!!"])
141 def test_ignore_unnormalizable_terms(self, rule):
142 repl = self.get_replacements(rule)
147 def test_add_full(self):
148 repl = self.get_replacements("foo -> bar")
150 assert repl == [(' foo ', [' bar', ' foo'])]
153 def test_replace_full(self):
154 repl = self.get_replacements("foo => bar")
156 assert repl == [(' foo ', [' bar'])]
159 def test_add_suffix_no_decompose(self):
160 repl = self.get_replacements("~berg |-> bg")
162 assert repl == [(' berg ', [' berg', ' bg']),
163 ('berg ', ['berg', 'bg'])]
166 def test_replace_suffix_no_decompose(self):
167 repl = self.get_replacements("~berg |=> bg")
169 assert repl == [(' berg ', [' bg']),('berg ', ['bg'])]
172 def test_add_suffix_decompose(self):
173 repl = self.get_replacements("~berg -> bg")
175 assert repl == [(' berg ', [' berg', ' bg', 'berg', 'bg']),
176 ('berg ', [' berg', ' bg', 'berg', 'bg'])]
179 def test_replace_suffix_decompose(self):
180 repl = self.get_replacements("~berg => bg")
182 assert repl == [(' berg ', [' bg', 'bg']),
183 ('berg ', [' bg', 'bg'])]
186 def test_add_prefix_no_compose(self):
187 repl = self.get_replacements("hinter~ |-> hnt")
189 assert repl == [(' hinter', [' hinter', ' hnt']),
190 (' hinter ', [' hinter', ' hnt'])]
193 def test_replace_prefix_no_compose(self):
194 repl = self.get_replacements("hinter~ |=> hnt")
196 assert repl == [(' hinter', [' hnt']), (' hinter ', [' hnt'])]
199 def test_add_prefix_compose(self):
200 repl = self.get_replacements("hinter~-> h")
202 assert repl == [(' hinter', [' h', ' h ', ' hinter', ' hinter ']),
203 (' hinter ', [' h', ' h', ' hinter', ' hinter'])]
206 def test_replace_prefix_compose(self):
207 repl = self.get_replacements("hinter~=> h")
209 assert repl == [(' hinter', [' h', ' h ']),
210 (' hinter ', [' h', ' h'])]
213 def test_add_beginning_only(self):
214 repl = self.get_replacements("^Premier -> Pr")
216 assert repl == [('^ premier ', ['^ pr', '^ premier'])]
219 def test_replace_beginning_only(self):
220 repl = self.get_replacements("^Premier => Pr")
222 assert repl == [('^ premier ', ['^ pr'])]
225 def test_add_final_only(self):
226 repl = self.get_replacements("road$ -> rd")
228 assert repl == [(' road ^', [' rd ^', ' road ^'])]
231 def test_replace_final_only(self):
232 repl = self.get_replacements("road$ => rd")
234 assert repl == [(' road ^', [' rd ^'])]
237 def test_decompose_only(self):
238 repl = self.get_replacements("~foo -> foo")
240 assert repl == [(' foo ', [' foo', 'foo']),
241 ('foo ', [' foo', 'foo'])]
244 def test_add_suffix_decompose_end_only(self):
245 repl = self.get_replacements("~berg |-> bg", "~berg$ -> bg")
247 assert repl == [(' berg ', [' berg', ' bg']),
248 (' berg ^', [' berg ^', ' bg ^', 'berg ^', 'bg ^']),
249 ('berg ', ['berg', 'bg']),
250 ('berg ^', [' berg ^', ' bg ^', 'berg ^', 'bg ^'])]
253 def test_replace_suffix_decompose_end_only(self):
254 repl = self.get_replacements("~berg |=> bg", "~berg$ => bg")
256 assert repl == [(' berg ', [' bg']),
257 (' berg ^', [' bg ^', 'bg ^']),
259 ('berg ^', [' bg ^', 'bg ^'])]
262 @pytest.mark.parametrize('rule', ["~berg,~burg -> bg",
263 "~berg, ~burg -> bg",
264 "~berg,,~burg -> bg"])
265 def test_add_multiple_suffix(self, rule):
266 repl = self.get_replacements(rule)
268 assert repl == [(' berg ', [' berg', ' bg', 'berg', 'bg']),
269 (' burg ', [' bg', ' burg', 'bg', 'burg']),
270 ('berg ', [' berg', ' bg', 'berg', 'bg']),
271 ('burg ', [' bg', ' burg', 'bg', 'burg'])]