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 - 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("""\
56 rules = ICURuleLoader(test_config)
57 assert rules.get_search_rules() == ''
58 assert rules.get_normalization_rules() == ''
59 assert rules.get_transliteration_rules() == ''
61 CONFIG_SECTIONS = ('normalization', 'transliteration', 'token-analysis')
63 @pytest.mark.parametrize("section", CONFIG_SECTIONS)
64 def test_missing_section(section, test_config):
65 rule_cfg = { s: [] for s in CONFIG_SECTIONS if s != section}
66 (test_config.project_dir / 'icu_tokenizer.yaml').write_text(yaml.dump(rule_cfg))
68 with pytest.raises(UsageError):
69 ICURuleLoader(test_config)
72 def test_get_search_rules(cfgrules):
73 loader = ICURuleLoader(cfgrules())
75 rules = loader.get_search_rules()
76 trans = Transliterator.createFromRules("test", rules)
78 assert trans.transliterate(" Baum straße ") == " baum straße "
79 assert trans.transliterate(" Baumstraße ") == " baumstraße "
80 assert trans.transliterate(" Baumstrasse ") == " baumstrasse "
81 assert trans.transliterate(" Baumstr ") == " baumstr "
82 assert trans.transliterate(" Baumwegstr ") == " baumwegstr "
83 assert trans.transliterate(" Αθήνα ") == " athēna "
84 assert trans.transliterate(" проспект ") == " prospekt "
87 def test_get_normalization_rules(cfgrules):
88 loader = ICURuleLoader(cfgrules())
89 rules = loader.get_normalization_rules()
90 trans = Transliterator.createFromRules("test", rules)
92 assert trans.transliterate(" проспект-Prospekt ") == " проспект prospekt "
95 def test_get_transliteration_rules(cfgrules):
96 loader = ICURuleLoader(cfgrules())
97 rules = loader.get_transliteration_rules()
98 trans = Transliterator.createFromRules("test", rules)
100 assert trans.transliterate(" проспект-Prospekt ") == " prospekt Prospekt "
103 def test_transliteration_rules_from_file(test_config):
104 cfgpath = test_config.project_dir / ('icu_tokenizer.yaml')
105 cfgpath.write_text(dedent("""\
109 - !include transliteration.yaml
113 transpath = test_config.project_dir / ('transliteration.yaml')
114 transpath.write_text('- "x > y"')
116 loader = ICURuleLoader(test_config)
117 rules = loader.get_transliteration_rules()
118 trans = Transliterator.createFromRules("test", rules)
120 assert trans.transliterate(" axxt ") == " byt "
123 class TestGetReplacements:
125 @pytest.fixture(autouse=True)
126 def setup_cfg(self, cfgrules):
127 self.cfgrules = cfgrules
129 def get_replacements(self, *variants):
130 loader = ICURuleLoader(self.cfgrules(*variants))
131 rules = loader.analysis[None].variants
133 return set((v.source, v.replacement) for v in rules)
136 @pytest.mark.parametrize("variant", ['foo > bar', 'foo -> bar -> bar',
137 '~foo~ -> bar', 'fo~ o -> bar'])
138 def test_invalid_variant_description(self, variant):
139 with pytest.raises(UsageError):
140 ICURuleLoader(self.cfgrules(variant))
142 def test_add_full(self):
143 repl = self.get_replacements("foo -> bar")
145 assert repl == {(' foo ', ' bar '), (' foo ', ' foo ')}
148 def test_replace_full(self):
149 repl = self.get_replacements("foo => bar")
151 assert repl == {(' foo ', ' bar ')}
154 def test_add_suffix_no_decompose(self):
155 repl = self.get_replacements("~berg |-> bg")
157 assert repl == {('berg ', 'berg '), ('berg ', 'bg '),
158 (' berg ', ' berg '), (' berg ', ' bg ')}
161 def test_replace_suffix_no_decompose(self):
162 repl = self.get_replacements("~berg |=> bg")
164 assert repl == {('berg ', 'bg '), (' berg ', ' bg ')}
167 def test_add_suffix_decompose(self):
168 repl = self.get_replacements("~berg -> bg")
170 assert repl == {('berg ', 'berg '), ('berg ', ' berg '),
171 (' berg ', ' berg '), (' berg ', 'berg '),
172 ('berg ', 'bg '), ('berg ', ' bg '),
173 (' berg ', 'bg '), (' berg ', ' bg ')}
176 def test_replace_suffix_decompose(self):
177 repl = self.get_replacements("~berg => bg")
179 assert repl == {('berg ', 'bg '), ('berg ', ' bg '),
180 (' berg ', 'bg '), (' berg ', ' bg ')}
183 def test_add_prefix_no_compose(self):
184 repl = self.get_replacements("hinter~ |-> hnt")
186 assert repl == {(' hinter', ' hinter'), (' hinter ', ' hinter '),
187 (' hinter', ' hnt'), (' hinter ', ' hnt ')}
190 def test_replace_prefix_no_compose(self):
191 repl = self.get_replacements("hinter~ |=> hnt")
193 assert repl == {(' hinter', ' hnt'), (' hinter ', ' hnt ')}
196 def test_add_prefix_compose(self):
197 repl = self.get_replacements("hinter~-> h")
199 assert repl == {(' hinter', ' hinter'), (' hinter', ' hinter '),
200 (' hinter', ' h'), (' hinter', ' h '),
201 (' hinter ', ' hinter '), (' hinter ', ' hinter'),
202 (' hinter ', ' h '), (' hinter ', ' h')}
205 def test_replace_prefix_compose(self):
206 repl = self.get_replacements("hinter~=> h")
208 assert repl == {(' hinter', ' h'), (' hinter', ' h '),
209 (' hinter ', ' h '), (' hinter ', ' h')}
212 def test_add_beginning_only(self):
213 repl = self.get_replacements("^Premier -> Pr")
215 assert repl == {('^ premier ', '^ premier '), ('^ premier ', '^ pr ')}
218 def test_replace_beginning_only(self):
219 repl = self.get_replacements("^Premier => Pr")
221 assert repl == {('^ premier ', '^ pr ')}
224 def test_add_final_only(self):
225 repl = self.get_replacements("road$ -> rd")
227 assert repl == {(' road ^', ' road ^'), (' road ^', ' rd ^')}
230 def test_replace_final_only(self):
231 repl = self.get_replacements("road$ => rd")
233 assert repl == {(' road ^', ' rd ^')}
236 def test_decompose_only(self):
237 repl = self.get_replacements("~foo -> foo")
239 assert repl == {('foo ', 'foo '), ('foo ', ' foo '),
240 (' foo ', 'foo '), (' foo ', ' foo ')}
243 def test_add_suffix_decompose_end_only(self):
244 repl = self.get_replacements("~berg |-> bg", "~berg$ -> bg")
246 assert repl == {('berg ', 'berg '), ('berg ', 'bg '),
247 (' berg ', ' berg '), (' berg ', ' bg '),
248 ('berg ^', 'berg ^'), ('berg ^', ' berg ^'),
249 ('berg ^', 'bg ^'), ('berg ^', ' bg ^'),
250 (' berg ^', 'berg ^'), (' berg ^', 'bg ^'),
251 (' berg ^', ' berg ^'), (' berg ^', ' bg ^')}
254 def test_replace_suffix_decompose_end_only(self):
255 repl = self.get_replacements("~berg |=> bg", "~berg$ => bg")
257 assert repl == {('berg ', 'bg '), (' berg ', ' bg '),
258 ('berg ^', 'bg ^'), ('berg ^', ' bg ^'),
259 (' berg ^', 'bg ^'), (' berg ^', ' bg ^')}
262 def test_add_multiple_suffix(self):
263 repl = self.get_replacements("~berg,~burg -> bg")
265 assert repl == {('berg ', 'berg '), ('berg ', ' berg '),
266 (' berg ', ' berg '), (' berg ', 'berg '),
267 ('berg ', 'bg '), ('berg ', ' bg '),
268 (' berg ', 'bg '), (' berg ', ' bg '),
269 ('burg ', 'burg '), ('burg ', ' burg '),
270 (' burg ', ' burg '), (' burg ', 'burg '),
271 ('burg ', 'bg '), ('burg ', ' bg '),
272 (' burg ', 'bg '), (' burg ', ' bg ')}