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)
35 norm = Transliterator.createFromRules("test_norm", DEFAULT_NORMALIZATION)
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.get_variants_ascii(norm.transliterate(name).strip())
45 def test_no_variants():
46 rules = { 'analyzer': 'generic' }
47 config = module.configure(rules, DEFAULT_NORMALIZATION)
48 trans = Transliterator.createFromRules("test_trans", DEFAULT_TRANSLITERATION)
49 norm = Transliterator.createFromRules("test_norm", DEFAULT_NORMALIZATION)
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 return module.configure(rules, DEFAULT_NORMALIZATION)
129 def get_replacements(self, *variants):
130 config = self.configure_rules(*variants)
132 return sorted((k, sorted(v)) for k,v in config['replacements'])
135 @pytest.mark.parametrize("variant", ['foo > bar', 'foo -> bar -> bar',
136 '~foo~ -> bar', 'fo~ o -> bar'])
137 def test_invalid_variant_description(self, variant):
138 with pytest.raises(UsageError):
139 self.configure_rules(variant)
142 @pytest.mark.parametrize("rule", ["!!! -> bar", "bar => !!!"])
143 def test_ignore_unnormalizable_terms(self, rule):
144 repl = self.get_replacements(rule)
149 def test_add_full(self):
150 repl = self.get_replacements("foo -> bar")
152 assert repl == [(' foo ', [' bar', ' foo'])]
155 def test_replace_full(self):
156 repl = self.get_replacements("foo => bar")
158 assert repl == [(' foo ', [' bar'])]
161 def test_add_suffix_no_decompose(self):
162 repl = self.get_replacements("~berg |-> bg")
164 assert repl == [(' berg ', [' berg', ' bg']),
165 ('berg ', ['berg', 'bg'])]
168 def test_replace_suffix_no_decompose(self):
169 repl = self.get_replacements("~berg |=> bg")
171 assert repl == [(' berg ', [' bg']),('berg ', ['bg'])]
174 def test_add_suffix_decompose(self):
175 repl = self.get_replacements("~berg -> bg")
177 assert repl == [(' berg ', [' berg', ' bg', 'berg', 'bg']),
178 ('berg ', [' berg', ' bg', 'berg', 'bg'])]
181 def test_replace_suffix_decompose(self):
182 repl = self.get_replacements("~berg => bg")
184 assert repl == [(' berg ', [' bg', 'bg']),
185 ('berg ', [' bg', 'bg'])]
188 def test_add_prefix_no_compose(self):
189 repl = self.get_replacements("hinter~ |-> hnt")
191 assert repl == [(' hinter', [' hinter', ' hnt']),
192 (' hinter ', [' hinter', ' hnt'])]
195 def test_replace_prefix_no_compose(self):
196 repl = self.get_replacements("hinter~ |=> hnt")
198 assert repl == [(' hinter', [' hnt']), (' hinter ', [' hnt'])]
201 def test_add_prefix_compose(self):
202 repl = self.get_replacements("hinter~-> h")
204 assert repl == [(' hinter', [' h', ' h ', ' hinter', ' hinter ']),
205 (' hinter ', [' h', ' h', ' hinter', ' hinter'])]
208 def test_replace_prefix_compose(self):
209 repl = self.get_replacements("hinter~=> h")
211 assert repl == [(' hinter', [' h', ' h ']),
212 (' hinter ', [' h', ' h'])]
215 def test_add_beginning_only(self):
216 repl = self.get_replacements("^Premier -> Pr")
218 assert repl == [('^ premier ', ['^ pr', '^ premier'])]
221 def test_replace_beginning_only(self):
222 repl = self.get_replacements("^Premier => Pr")
224 assert repl == [('^ premier ', ['^ pr'])]
227 def test_add_final_only(self):
228 repl = self.get_replacements("road$ -> rd")
230 assert repl == [(' road ^', [' rd ^', ' road ^'])]
233 def test_replace_final_only(self):
234 repl = self.get_replacements("road$ => rd")
236 assert repl == [(' road ^', [' rd ^'])]
239 def test_decompose_only(self):
240 repl = self.get_replacements("~foo -> foo")
242 assert repl == [(' foo ', [' foo', 'foo']),
243 ('foo ', [' foo', 'foo'])]
246 def test_add_suffix_decompose_end_only(self):
247 repl = self.get_replacements("~berg |-> bg", "~berg$ -> bg")
249 assert repl == [(' berg ', [' berg', ' bg']),
250 (' berg ^', [' berg ^', ' bg ^', 'berg ^', 'bg ^']),
251 ('berg ', ['berg', 'bg']),
252 ('berg ^', [' berg ^', ' bg ^', 'berg ^', 'bg ^'])]
255 def test_replace_suffix_decompose_end_only(self):
256 repl = self.get_replacements("~berg |=> bg", "~berg$ => bg")
258 assert repl == [(' berg ', [' bg']),
259 (' berg ^', [' bg ^', 'bg ^']),
261 ('berg ^', [' bg ^', 'bg ^'])]
264 @pytest.mark.parametrize('rule', ["~berg,~burg -> bg",
265 "~berg, ~burg -> bg",
266 "~berg,,~burg -> bg"])
267 def test_add_multiple_suffix(self, rule):
268 repl = self.get_replacements(rule)
270 assert repl == [(' berg ', [' berg', ' bg', 'berg', 'bg']),
271 (' burg ', [' bg', ' burg', 'bg', 'burg']),
272 ('berg ', [' berg', ' bg', 'berg', 'bg']),
273 ('burg ', [' bg', ' burg', 'bg', 'burg'])]