2 Tests for converting a config file to ICU rules.
4 from textwrap import dedent
9 from nominatim.tokenizer.icu_rule_loader import ICURuleLoader
10 from nominatim.errors import UsageError
12 from icu import Transliterator
15 def test_config(def_config, tmp_path):
16 project_dir = tmp_path / 'project_dir'
18 def_config.project_dir = project_dir
24 def cfgrules(test_config):
25 def _create_config(*variants, **kwargs):
29 - "[[:Nonspacing Mark:] [:Cf:]] >"
31 - "[[:Punctuation:][:Space:]]+ > ' '"
35 - "[[:Punctuation:][:Space:]]+ > ' '"
37 content += "token-analysis:\n - analyzer: generic\n variants:\n - words:\n"
38 content += '\n'.join((" - " + s for s in variants)) + '\n'
40 content += " {}: {}\n".format(k, v)
41 (test_config.project_dir / 'icu_tokenizer.yaml').write_text(content)
48 def test_empty_rule_set(test_config):
49 (test_config.project_dir / 'icu_tokenizer.yaml').write_text(dedent("""\
57 rules = ICURuleLoader(test_config)
58 assert rules.get_search_rules() == ''
59 assert rules.get_normalization_rules() == ''
60 assert rules.get_transliteration_rules() == ''
62 CONFIG_SECTIONS = ('normalization', 'transliteration', 'token-analysis')
64 @pytest.mark.parametrize("section", CONFIG_SECTIONS)
65 def test_missing_section(section, test_config):
66 rule_cfg = { s: [] for s in CONFIG_SECTIONS if s != section}
67 (test_config.project_dir / 'icu_tokenizer.yaml').write_text(yaml.dump(rule_cfg))
69 with pytest.raises(UsageError):
70 ICURuleLoader(test_config)
73 def test_get_search_rules(cfgrules):
74 loader = ICURuleLoader(cfgrules())
76 rules = loader.get_search_rules()
77 trans = Transliterator.createFromRules("test", rules)
79 assert trans.transliterate(" Baum straße ") == " baum straße "
80 assert trans.transliterate(" Baumstraße ") == " baumstraße "
81 assert trans.transliterate(" Baumstrasse ") == " baumstrasse "
82 assert trans.transliterate(" Baumstr ") == " baumstr "
83 assert trans.transliterate(" Baumwegstr ") == " baumwegstr "
84 assert trans.transliterate(" Αθήνα ") == " athēna "
85 assert trans.transliterate(" проспект ") == " prospekt "
88 def test_get_normalization_rules(cfgrules):
89 loader = ICURuleLoader(cfgrules())
90 rules = loader.get_normalization_rules()
91 trans = Transliterator.createFromRules("test", rules)
93 assert trans.transliterate(" проспект-Prospekt ") == " проспект prospekt "
96 def test_get_transliteration_rules(cfgrules):
97 loader = ICURuleLoader(cfgrules())
98 rules = loader.get_transliteration_rules()
99 trans = Transliterator.createFromRules("test", rules)
101 assert trans.transliterate(" проспект-Prospekt ") == " prospekt Prospekt "
104 def test_transliteration_rules_from_file(test_config):
105 cfgpath = test_config.project_dir / ('icu_tokenizer.yaml')
106 cfgpath.write_text(dedent("""\
110 - !include transliteration.yaml
115 transpath = test_config.project_dir / ('transliteration.yaml')
116 transpath.write_text('- "x > y"')
118 loader = ICURuleLoader(test_config)
119 rules = loader.get_transliteration_rules()
120 trans = Transliterator.createFromRules("test", rules)
122 assert trans.transliterate(" axxt ") == " byt "
125 def test_search_rules(cfgrules):
126 config = cfgrules('~street => s,st', 'master => mstr')
127 proc = ICURuleLoader(config).make_token_analysis()
129 assert proc.search.transliterate('Master Street').strip() == 'master street'
130 assert proc.search.transliterate('Earnes St').strip() == 'earnes st'
131 assert proc.search.transliterate('Nostreet').strip() == 'nostreet'
134 class TestGetReplacements:
136 @pytest.fixture(autouse=True)
137 def setup_cfg(self, cfgrules):
138 self.cfgrules = cfgrules
140 def get_replacements(self, *variants):
141 loader = ICURuleLoader(self.cfgrules(*variants))
142 rules = loader.analysis[None].config['replacements']
144 return sorted((k, sorted(v)) for k,v in rules)
147 @pytest.mark.parametrize("variant", ['foo > bar', 'foo -> bar -> bar',
148 '~foo~ -> bar', 'fo~ o -> bar'])
149 def test_invalid_variant_description(self, variant):
150 with pytest.raises(UsageError):
151 ICURuleLoader(self.cfgrules(variant))
153 def test_add_full(self):
154 repl = self.get_replacements("foo -> bar")
156 assert repl == [(' foo ', [' bar', ' foo'])]
159 def test_replace_full(self):
160 repl = self.get_replacements("foo => bar")
162 assert repl == [(' foo ', [' bar'])]
165 def test_add_suffix_no_decompose(self):
166 repl = self.get_replacements("~berg |-> bg")
168 assert repl == [(' berg ', [' berg', ' bg']),
169 ('berg ', ['berg', 'bg'])]
172 def test_replace_suffix_no_decompose(self):
173 repl = self.get_replacements("~berg |=> bg")
175 assert repl == [(' berg ', [' bg']),('berg ', ['bg'])]
178 def test_add_suffix_decompose(self):
179 repl = self.get_replacements("~berg -> bg")
181 assert repl == [(' berg ', [' berg', ' bg', 'berg', 'bg']),
182 ('berg ', [' berg', ' bg', 'berg', 'bg'])]
185 def test_replace_suffix_decompose(self):
186 repl = self.get_replacements("~berg => bg")
188 assert repl == [(' berg ', [' bg', 'bg']),
189 ('berg ', [' bg', 'bg'])]
192 def test_add_prefix_no_compose(self):
193 repl = self.get_replacements("hinter~ |-> hnt")
195 assert repl == [(' hinter', [' hinter', ' hnt']),
196 (' hinter ', [' hinter', ' hnt'])]
199 def test_replace_prefix_no_compose(self):
200 repl = self.get_replacements("hinter~ |=> hnt")
202 assert repl == [(' hinter', [' hnt']), (' hinter ', [' hnt'])]
205 def test_add_prefix_compose(self):
206 repl = self.get_replacements("hinter~-> h")
208 assert repl == [(' hinter', [' h', ' h ', ' hinter', ' hinter ']),
209 (' hinter ', [' h', ' h', ' hinter', ' hinter'])]
212 def test_replace_prefix_compose(self):
213 repl = self.get_replacements("hinter~=> h")
215 assert repl == [(' hinter', [' h', ' h ']),
216 (' hinter ', [' h', ' h'])]
219 def test_add_beginning_only(self):
220 repl = self.get_replacements("^Premier -> Pr")
222 assert repl == [('^ premier ', ['^ pr', '^ premier'])]
225 def test_replace_beginning_only(self):
226 repl = self.get_replacements("^Premier => Pr")
228 assert repl == [('^ premier ', ['^ pr'])]
231 def test_add_final_only(self):
232 repl = self.get_replacements("road$ -> rd")
234 assert repl == [(' road ^', [' rd ^', ' road ^'])]
237 def test_replace_final_only(self):
238 repl = self.get_replacements("road$ => rd")
240 assert repl == [(' road ^', [' rd ^'])]
243 def test_decompose_only(self):
244 repl = self.get_replacements("~foo -> foo")
246 assert repl == [(' foo ', [' foo', 'foo']),
247 ('foo ', [' foo', 'foo'])]
250 def test_add_suffix_decompose_end_only(self):
251 repl = self.get_replacements("~berg |-> bg", "~berg$ -> bg")
253 assert repl == [(' berg ', [' berg', ' bg']),
254 (' berg ^', [' berg ^', ' bg ^', 'berg ^', 'bg ^']),
255 ('berg ', ['berg', 'bg']),
256 ('berg ^', [' berg ^', ' bg ^', 'berg ^', 'bg ^'])]
259 def test_replace_suffix_decompose_end_only(self):
260 repl = self.get_replacements("~berg |=> bg", "~berg$ => bg")
262 assert repl == [(' berg ', [' bg']),
263 (' berg ^', [' bg ^', 'bg ^']),
265 ('berg ^', [' bg ^', 'bg ^'])]
268 def test_add_multiple_suffix(self):
269 repl = self.get_replacements("~berg,~burg -> bg")
271 assert repl == [(' berg ', [' berg', ' bg', 'berg', 'bg']),
272 (' burg ', [' bg', ' burg', 'bg', 'burg']),
273 ('berg ', [' berg', ' bg', 'berg', 'bg']),
274 ('burg ', [' bg', ' burg', 'bg', 'burg'])]