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 += "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("""\
55 rules = ICURuleLoader(test_config)
56 assert rules.get_search_rules() == ''
57 assert rules.get_normalization_rules() == ''
58 assert rules.get_transliteration_rules() == ''
59 assert list(rules.get_replacement_pairs()) == []
61 CONFIG_SECTIONS = ('normalization', 'transliteration', 'variants')
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
112 transpath = test_config.project_dir / ('transliteration.yaml')
113 transpath.write_text('- "x > y"')
115 loader = ICURuleLoader(test_config)
116 rules = loader.get_transliteration_rules()
117 trans = Transliterator.createFromRules("test", rules)
119 assert trans.transliterate(" axxt ") == " byt "
122 class TestGetReplacements:
124 @pytest.fixture(autouse=True)
125 def setup_cfg(self, cfgrules):
126 self.cfgrules = cfgrules
128 def get_replacements(self, *variants):
129 loader = ICURuleLoader(self.cfgrules(*variants))
130 rules = loader.get_replacement_pairs()
132 return set((v.source, v.replacement) for v in rules)
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 ICURuleLoader(self.cfgrules(variant))
141 def test_add_full(self):
142 repl = self.get_replacements("foo -> bar")
144 assert repl == {(' foo ', ' bar '), (' foo ', ' foo ')}
147 def test_replace_full(self):
148 repl = self.get_replacements("foo => bar")
150 assert repl == {(' foo ', ' bar ')}
153 def test_add_suffix_no_decompose(self):
154 repl = self.get_replacements("~berg |-> bg")
156 assert repl == {('berg ', 'berg '), ('berg ', 'bg '),
157 (' berg ', ' berg '), (' berg ', ' bg ')}
160 def test_replace_suffix_no_decompose(self):
161 repl = self.get_replacements("~berg |=> bg")
163 assert repl == {('berg ', 'bg '), (' berg ', ' bg ')}
166 def test_add_suffix_decompose(self):
167 repl = self.get_replacements("~berg -> bg")
169 assert repl == {('berg ', 'berg '), ('berg ', ' berg '),
170 (' berg ', ' berg '), (' berg ', 'berg '),
171 ('berg ', 'bg '), ('berg ', ' bg '),
172 (' berg ', 'bg '), (' berg ', ' bg ')}
175 def test_replace_suffix_decompose(self):
176 repl = self.get_replacements("~berg => bg")
178 assert repl == {('berg ', 'bg '), ('berg ', ' bg '),
179 (' berg ', 'bg '), (' berg ', ' bg ')}
182 def test_add_prefix_no_compose(self):
183 repl = self.get_replacements("hinter~ |-> hnt")
185 assert repl == {(' hinter', ' hinter'), (' hinter ', ' hinter '),
186 (' hinter', ' hnt'), (' hinter ', ' hnt ')}
189 def test_replace_prefix_no_compose(self):
190 repl = self.get_replacements("hinter~ |=> hnt")
192 assert repl == {(' hinter', ' hnt'), (' hinter ', ' hnt ')}
195 def test_add_prefix_compose(self):
196 repl = self.get_replacements("hinter~-> h")
198 assert repl == {(' hinter', ' hinter'), (' hinter', ' hinter '),
199 (' hinter', ' h'), (' hinter', ' h '),
200 (' hinter ', ' hinter '), (' hinter ', ' hinter'),
201 (' hinter ', ' h '), (' hinter ', ' h')}
204 def test_replace_prefix_compose(self):
205 repl = self.get_replacements("hinter~=> h")
207 assert repl == {(' hinter', ' h'), (' hinter', ' h '),
208 (' hinter ', ' h '), (' hinter ', ' h')}
211 def test_add_beginning_only(self):
212 repl = self.get_replacements("^Premier -> Pr")
214 assert repl == {('^ premier ', '^ premier '), ('^ premier ', '^ pr ')}
217 def test_replace_beginning_only(self):
218 repl = self.get_replacements("^Premier => Pr")
220 assert repl == {('^ premier ', '^ pr ')}
223 def test_add_final_only(self):
224 repl = self.get_replacements("road$ -> rd")
226 assert repl == {(' road ^', ' road ^'), (' road ^', ' rd ^')}
229 def test_replace_final_only(self):
230 repl = self.get_replacements("road$ => rd")
232 assert repl == {(' road ^', ' rd ^')}
235 def test_decompose_only(self):
236 repl = self.get_replacements("~foo -> foo")
238 assert repl == {('foo ', 'foo '), ('foo ', ' foo '),
239 (' foo ', 'foo '), (' foo ', ' foo ')}
242 def test_add_suffix_decompose_end_only(self):
243 repl = self.get_replacements("~berg |-> bg", "~berg$ -> bg")
245 assert repl == {('berg ', 'berg '), ('berg ', 'bg '),
246 (' berg ', ' berg '), (' berg ', ' bg '),
247 ('berg ^', 'berg ^'), ('berg ^', ' berg ^'),
248 ('berg ^', 'bg ^'), ('berg ^', ' bg ^'),
249 (' berg ^', 'berg ^'), (' berg ^', 'bg ^'),
250 (' berg ^', ' berg ^'), (' 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 '), (' berg ', ' bg '),
257 ('berg ^', 'bg ^'), ('berg ^', ' bg ^'),
258 (' berg ^', 'bg ^'), (' berg ^', ' bg ^')}
261 def test_add_multiple_suffix(self):
262 repl = self.get_replacements("~berg,~burg -> bg")
264 assert repl == {('berg ', 'berg '), ('berg ', ' berg '),
265 (' berg ', ' berg '), (' berg ', 'berg '),
266 ('berg ', 'bg '), ('berg ', ' bg '),
267 (' berg ', 'bg '), (' berg ', ' bg '),
268 ('burg ', 'burg '), ('burg ', ' burg '),
269 (' burg ', ' burg '), (' burg ', 'burg '),
270 ('burg ', 'bg '), ('burg ', ' bg '),
271 (' burg ', 'bg '), (' burg ', ' bg ')}