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
16 def _create_config(*variants, **kwargs):
20 - "[[:Nonspacing Mark:] [:Cf:]] >"
22 - "[[:Punctuation:][:Space:]]+ > ' '"
26 - "[[:Punctuation:][:Space:]]+ > ' '"
28 content += "variants:\n - words:\n"
29 content += '\n'.join((" - " + s for s in variants)) + '\n'
31 content += " {}: {}\n".format(k, v)
32 return yaml.safe_load(content)
37 def test_empty_rule_set():
38 rule_cfg = yaml.safe_load(dedent("""\
44 rules = ICURuleLoader(rule_cfg)
45 assert rules.get_search_rules() == ''
46 assert rules.get_normalization_rules() == ''
47 assert rules.get_transliteration_rules() == ''
48 assert list(rules.get_replacement_pairs()) == []
50 CONFIG_SECTIONS = ('normalization', 'transliteration', 'variants')
52 @pytest.mark.parametrize("section", CONFIG_SECTIONS)
53 def test_missing_section(section):
54 rule_cfg = { s: {} for s in CONFIG_SECTIONS if s != section}
56 with pytest.raises(UsageError):
57 ICURuleLoader(rule_cfg)
60 def test_get_search_rules(cfgrules):
61 loader = ICURuleLoader(cfgrules())
63 rules = loader.get_search_rules()
64 trans = Transliterator.createFromRules("test", rules)
66 assert trans.transliterate(" Baum straße ") == " baum straße "
67 assert trans.transliterate(" Baumstraße ") == " baumstraße "
68 assert trans.transliterate(" Baumstrasse ") == " baumstrasse "
69 assert trans.transliterate(" Baumstr ") == " baumstr "
70 assert trans.transliterate(" Baumwegstr ") == " baumwegstr "
71 assert trans.transliterate(" Αθήνα ") == " athēna "
72 assert trans.transliterate(" проспект ") == " prospekt "
75 def test_get_normalization_rules(cfgrules):
76 loader = ICURuleLoader(cfgrules())
77 rules = loader.get_normalization_rules()
78 trans = Transliterator.createFromRules("test", rules)
80 assert trans.transliterate(" проспект-Prospekt ") == " проспект prospekt "
83 def test_get_transliteration_rules(cfgrules):
84 loader = ICURuleLoader(cfgrules())
85 rules = loader.get_transliteration_rules()
86 trans = Transliterator.createFromRules("test", rules)
88 assert trans.transliterate(" проспект-Prospekt ") == " prospekt Prospekt "
91 def test_transliteration_rules_from_file(def_config, tmp_path):
92 def_config.project_dir = tmp_path
93 cfgpath = tmp_path / ('test_config.yaml')
94 cfgpath.write_text(dedent("""\
98 - !include transliteration.yaml
101 transpath = tmp_path / ('transliteration.yaml')
102 transpath.write_text('- "x > y"')
104 loader = ICURuleLoader(def_config.load_sub_configuration('test_config.yaml'))
105 rules = loader.get_transliteration_rules()
106 trans = Transliterator.createFromRules("test", rules)
108 assert trans.transliterate(" axxt ") == " byt "
111 class TestGetReplacements:
113 @pytest.fixture(autouse=True)
114 def setup_cfg(self, cfgrules):
115 self.cfgrules = cfgrules
117 def get_replacements(self, *variants):
118 loader = ICURuleLoader(self.cfgrules(*variants))
119 rules = loader.get_replacement_pairs()
121 return set((v.source, v.replacement) for v in rules)
124 @pytest.mark.parametrize("variant", ['foo > bar', 'foo -> bar -> bar',
125 '~foo~ -> bar', 'fo~ o -> bar'])
126 def test_invalid_variant_description(self, variant):
127 with pytest.raises(UsageError):
128 ICURuleLoader(self.cfgrules(variant))
130 def test_add_full(self):
131 repl = self.get_replacements("foo -> bar")
133 assert repl == {(' foo ', ' bar '), (' foo ', ' foo ')}
136 def test_replace_full(self):
137 repl = self.get_replacements("foo => bar")
139 assert repl == {(' foo ', ' bar ')}
142 def test_add_suffix_no_decompose(self):
143 repl = self.get_replacements("~berg |-> bg")
145 assert repl == {('berg ', 'berg '), ('berg ', 'bg '),
146 (' berg ', ' berg '), (' berg ', ' bg ')}
149 def test_replace_suffix_no_decompose(self):
150 repl = self.get_replacements("~berg |=> bg")
152 assert repl == {('berg ', 'bg '), (' berg ', ' bg ')}
155 def test_add_suffix_decompose(self):
156 repl = self.get_replacements("~berg -> bg")
158 assert repl == {('berg ', 'berg '), ('berg ', ' berg '),
159 (' berg ', ' berg '), (' berg ', 'berg '),
160 ('berg ', 'bg '), ('berg ', ' bg '),
161 (' berg ', 'bg '), (' berg ', ' bg ')}
164 def test_replace_suffix_decompose(self):
165 repl = self.get_replacements("~berg => bg")
167 assert repl == {('berg ', 'bg '), ('berg ', ' bg '),
168 (' berg ', 'bg '), (' berg ', ' bg ')}
171 def test_add_prefix_no_compose(self):
172 repl = self.get_replacements("hinter~ |-> hnt")
174 assert repl == {(' hinter', ' hinter'), (' hinter ', ' hinter '),
175 (' hinter', ' hnt'), (' hinter ', ' hnt ')}
178 def test_replace_prefix_no_compose(self):
179 repl = self.get_replacements("hinter~ |=> hnt")
181 assert repl == {(' hinter', ' hnt'), (' hinter ', ' hnt ')}
184 def test_add_prefix_compose(self):
185 repl = self.get_replacements("hinter~-> h")
187 assert repl == {(' hinter', ' hinter'), (' hinter', ' hinter '),
188 (' hinter', ' h'), (' hinter', ' h '),
189 (' hinter ', ' hinter '), (' hinter ', ' hinter'),
190 (' hinter ', ' h '), (' hinter ', ' h')}
193 def test_replace_prefix_compose(self):
194 repl = self.get_replacements("hinter~=> h")
196 assert repl == {(' hinter', ' h'), (' hinter', ' h '),
197 (' hinter ', ' h '), (' hinter ', ' h')}
200 def test_add_beginning_only(self):
201 repl = self.get_replacements("^Premier -> Pr")
203 assert repl == {('^ premier ', '^ premier '), ('^ premier ', '^ pr ')}
206 def test_replace_beginning_only(self):
207 repl = self.get_replacements("^Premier => Pr")
209 assert repl == {('^ premier ', '^ pr ')}
212 def test_add_final_only(self):
213 repl = self.get_replacements("road$ -> rd")
215 assert repl == {(' road ^', ' road ^'), (' road ^', ' rd ^')}
218 def test_replace_final_only(self):
219 repl = self.get_replacements("road$ => rd")
221 assert repl == {(' road ^', ' rd ^')}
224 def test_decompose_only(self):
225 repl = self.get_replacements("~foo -> foo")
227 assert repl == {('foo ', 'foo '), ('foo ', ' foo '),
228 (' foo ', 'foo '), (' foo ', ' foo ')}
231 def test_add_suffix_decompose_end_only(self):
232 repl = self.get_replacements("~berg |-> bg", "~berg$ -> bg")
234 assert repl == {('berg ', 'berg '), ('berg ', 'bg '),
235 (' berg ', ' berg '), (' berg ', ' bg '),
236 ('berg ^', 'berg ^'), ('berg ^', ' berg ^'),
237 ('berg ^', 'bg ^'), ('berg ^', ' bg ^'),
238 (' berg ^', 'berg ^'), (' berg ^', 'bg ^'),
239 (' berg ^', ' berg ^'), (' berg ^', ' bg ^')}
242 def test_replace_suffix_decompose_end_only(self):
243 repl = self.get_replacements("~berg |=> bg", "~berg$ => bg")
245 assert repl == {('berg ', 'bg '), (' berg ', ' bg '),
246 ('berg ^', 'bg ^'), ('berg ^', ' bg ^'),
247 (' berg ^', 'bg ^'), (' berg ^', ' bg ^')}
250 def test_add_multiple_suffix(self):
251 repl = self.get_replacements("~berg,~burg -> bg")
253 assert repl == {('berg ', 'berg '), ('berg ', ' berg '),
254 (' berg ', ' berg '), (' berg ', 'berg '),
255 ('berg ', 'bg '), ('berg ', ' bg '),
256 (' berg ', 'bg '), (' berg ', ' bg '),
257 ('burg ', 'burg '), ('burg ', ' burg '),
258 (' burg ', ' burg '), (' burg ', 'burg '),
259 ('burg ', 'bg '), ('burg ', ' bg '),
260 (' burg ', 'bg '), (' burg ', ' bg ')}