2 Tests for converting a config file to ICU rules.
5 from textwrap import dedent
7 from nominatim.tokenizer.icu_rule_loader import ICURuleLoader
8 from nominatim.errors import UsageError
10 from icu import Transliterator
13 def cfgfile(tmp_path, suffix='.yaml'):
14 def _create_config(*variants, **kwargs):
18 - "[[:Nonspacing Mark:] [:Cf:]] >"
20 - "[[:Punctuation:][:Space:]]+ > ' '"
24 - "[[:Punctuation:][:Space:]]+ > ' '"
26 content += "variants:\n - words:\n"
27 content += '\n'.join((" - " + s for s in variants)) + '\n'
29 content += " {}: {}\n".format(k, v)
30 fpath = tmp_path / ('test_config' + suffix)
31 fpath.write_text(dedent(content))
37 def test_empty_rule_file(tmp_path):
38 fpath = tmp_path / ('test_config.yaml')
39 fpath.write_text(dedent("""\
45 rules = ICURuleLoader(fpath)
46 assert rules.get_search_rules() == ''
47 assert rules.get_normalization_rules() == ''
48 assert rules.get_transliteration_rules() == ''
49 assert list(rules.get_replacement_pairs()) == []
51 CONFIG_SECTIONS = ('normalization', 'transliteration', 'variants')
53 @pytest.mark.parametrize("section", CONFIG_SECTIONS)
54 def test_missing_normalization(tmp_path, section):
55 fpath = tmp_path / ('test_config.yaml')
56 with fpath.open('w') as fd:
57 for name in CONFIG_SECTIONS:
59 fd.write(name + ':\n')
61 with pytest.raises(UsageError):
65 def test_get_search_rules(cfgfile):
66 loader = ICURuleLoader(cfgfile())
68 rules = loader.get_search_rules()
69 trans = Transliterator.createFromRules("test", rules)
71 assert trans.transliterate(" Baum straße ") == " baum straße "
72 assert trans.transliterate(" Baumstraße ") == " baumstraße "
73 assert trans.transliterate(" Baumstrasse ") == " baumstrasse "
74 assert trans.transliterate(" Baumstr ") == " baumstr "
75 assert trans.transliterate(" Baumwegstr ") == " baumwegstr "
76 assert trans.transliterate(" Αθήνα ") == " athēna "
77 assert trans.transliterate(" проспект ") == " prospekt "
80 def test_get_normalization_rules(cfgfile):
81 loader = ICURuleLoader(cfgfile())
82 rules = loader.get_normalization_rules()
83 trans = Transliterator.createFromRules("test", rules)
85 assert trans.transliterate(" проспект-Prospekt ") == " проспект prospekt "
88 def test_get_transliteration_rules(cfgfile):
89 loader = ICURuleLoader(cfgfile())
90 rules = loader.get_transliteration_rules()
91 trans = Transliterator.createFromRules("test", rules)
93 assert trans.transliterate(" проспект-Prospekt ") == " prospekt Prospekt "
96 def test_transliteration_rules_from_file(tmp_path):
97 cfgpath = tmp_path / ('test_config.yaml')
98 cfgpath.write_text(dedent("""\
102 - !include transliteration.yaml
105 transpath = tmp_path / ('transliteration.yaml')
106 transpath.write_text('- "x > y"')
108 loader = ICURuleLoader(cfgpath)
109 rules = loader.get_transliteration_rules()
110 trans = Transliterator.createFromRules("test", rules)
112 assert trans.transliterate(" axxt ") == " byt "
115 class TestGetReplacements:
117 @pytest.fixture(autouse=True)
118 def setup_cfg(self, cfgfile):
119 self.cfgfile = cfgfile
121 def get_replacements(self, *variants):
122 loader = ICURuleLoader(self.cfgfile(*variants))
123 rules = loader.get_replacement_pairs()
125 return set((v.source, v.replacement) for v in rules)
128 @pytest.mark.parametrize("variant", ['foo > bar', 'foo -> bar -> bar',
129 '~foo~ -> bar', 'fo~ o -> bar'])
130 def test_invalid_variant_description(self, variant):
131 with pytest.raises(UsageError):
132 ICURuleLoader(self.cfgfile(variant))
134 def test_add_full(self):
135 repl = self.get_replacements("foo -> bar")
137 assert repl == {(' foo ', ' bar '), (' foo ', ' foo ')}
140 def test_replace_full(self):
141 repl = self.get_replacements("foo => bar")
143 assert repl == {(' foo ', ' bar ')}
146 def test_add_suffix_no_decompose(self):
147 repl = self.get_replacements("~berg |-> bg")
149 assert repl == {('berg ', 'berg '), ('berg ', 'bg '),
150 (' berg ', ' berg '), (' berg ', ' bg ')}
153 def test_replace_suffix_no_decompose(self):
154 repl = self.get_replacements("~berg |=> bg")
156 assert repl == {('berg ', 'bg '), (' berg ', ' bg ')}
159 def test_add_suffix_decompose(self):
160 repl = self.get_replacements("~berg -> bg")
162 assert repl == {('berg ', 'berg '), ('berg ', ' berg '),
163 (' berg ', ' berg '), (' berg ', 'berg '),
164 ('berg ', 'bg '), ('berg ', ' bg '),
165 (' berg ', 'bg '), (' berg ', ' bg ')}
168 def test_replace_suffix_decompose(self):
169 repl = self.get_replacements("~berg => bg")
171 assert repl == {('berg ', 'bg '), ('berg ', ' bg '),
172 (' berg ', 'bg '), (' berg ', ' bg ')}
175 def test_add_prefix_no_compose(self):
176 repl = self.get_replacements("hinter~ |-> hnt")
178 assert repl == {(' hinter', ' hinter'), (' hinter ', ' hinter '),
179 (' hinter', ' hnt'), (' hinter ', ' hnt ')}
182 def test_replace_prefix_no_compose(self):
183 repl = self.get_replacements("hinter~ |=> hnt")
185 assert repl == {(' hinter', ' hnt'), (' hinter ', ' hnt ')}
188 def test_add_prefix_compose(self):
189 repl = self.get_replacements("hinter~-> h")
191 assert repl == {(' hinter', ' hinter'), (' hinter', ' hinter '),
192 (' hinter', ' h'), (' hinter', ' h '),
193 (' hinter ', ' hinter '), (' hinter ', ' hinter'),
194 (' hinter ', ' h '), (' hinter ', ' h')}
197 def test_replace_prefix_compose(self):
198 repl = self.get_replacements("hinter~=> h")
200 assert repl == {(' hinter', ' h'), (' hinter', ' h '),
201 (' hinter ', ' h '), (' hinter ', ' h')}
204 def test_add_beginning_only(self):
205 repl = self.get_replacements("^Premier -> Pr")
207 assert repl == {('^ premier ', '^ premier '), ('^ premier ', '^ pr ')}
210 def test_replace_beginning_only(self):
211 repl = self.get_replacements("^Premier => Pr")
213 assert repl == {('^ premier ', '^ pr ')}
216 def test_add_final_only(self):
217 repl = self.get_replacements("road$ -> rd")
219 assert repl == {(' road ^', ' road ^'), (' road ^', ' rd ^')}
222 def test_replace_final_only(self):
223 repl = self.get_replacements("road$ => rd")
225 assert repl == {(' road ^', ' rd ^')}
228 def test_decompose_only(self):
229 repl = self.get_replacements("~foo -> foo")
231 assert repl == {('foo ', 'foo '), ('foo ', ' foo '),
232 (' foo ', 'foo '), (' foo ', ' foo ')}
235 def test_add_suffix_decompose_end_only(self):
236 repl = self.get_replacements("~berg |-> bg", "~berg$ -> bg")
238 assert repl == {('berg ', 'berg '), ('berg ', 'bg '),
239 (' berg ', ' berg '), (' berg ', ' bg '),
240 ('berg ^', 'berg ^'), ('berg ^', ' berg ^'),
241 ('berg ^', 'bg ^'), ('berg ^', ' bg ^'),
242 (' berg ^', 'berg ^'), (' berg ^', 'bg ^'),
243 (' berg ^', ' berg ^'), (' berg ^', ' bg ^')}
246 def test_replace_suffix_decompose_end_only(self):
247 repl = self.get_replacements("~berg |=> bg", "~berg$ => bg")
249 assert repl == {('berg ', 'bg '), (' berg ', ' bg '),
250 ('berg ^', 'bg ^'), ('berg ^', ' bg ^'),
251 (' berg ^', 'bg ^'), (' berg ^', ' bg ^')}
254 def test_add_multiple_suffix(self):
255 repl = self.get_replacements("~berg,~burg -> bg")
257 assert repl == {('berg ', 'berg '), ('berg ', ' berg '),
258 (' berg ', ' berg '), (' berg ', 'berg '),
259 ('berg ', 'bg '), ('berg ', ' bg '),
260 (' berg ', 'bg '), (' berg ', ' bg '),
261 ('burg ', 'burg '), ('burg ', ' burg '),
262 (' burg ', ' burg '), (' burg ', 'burg '),
263 ('burg ', 'bg '), ('burg ', ' bg '),
264 (' burg ', 'bg '), (' burg ', ' bg ')}