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 class TestGetReplacements:
127 @pytest.fixture(autouse=True)
128 def setup_cfg(self, cfgrules):
129 self.cfgrules = cfgrules
131 def get_replacements(self, *variants):
132 loader = ICURuleLoader(self.cfgrules(*variants))
133 rules = loader.analysis[None].config['variants']
135 return set((v.source, v.replacement) for v in rules)
138 @pytest.mark.parametrize("variant", ['foo > bar', 'foo -> bar -> bar',
139 '~foo~ -> bar', 'fo~ o -> bar'])
140 def test_invalid_variant_description(self, variant):
141 with pytest.raises(UsageError):
142 ICURuleLoader(self.cfgrules(variant))
144 def test_add_full(self):
145 repl = self.get_replacements("foo -> bar")
147 assert repl == {(' foo ', ' bar '), (' foo ', ' foo ')}
150 def test_replace_full(self):
151 repl = self.get_replacements("foo => bar")
153 assert repl == {(' foo ', ' bar ')}
156 def test_add_suffix_no_decompose(self):
157 repl = self.get_replacements("~berg |-> bg")
159 assert repl == {('berg ', 'berg '), ('berg ', 'bg '),
160 (' berg ', ' berg '), (' berg ', ' bg ')}
163 def test_replace_suffix_no_decompose(self):
164 repl = self.get_replacements("~berg |=> bg")
166 assert repl == {('berg ', 'bg '), (' berg ', ' bg ')}
169 def test_add_suffix_decompose(self):
170 repl = self.get_replacements("~berg -> bg")
172 assert repl == {('berg ', 'berg '), ('berg ', ' berg '),
173 (' berg ', ' berg '), (' berg ', 'berg '),
174 ('berg ', 'bg '), ('berg ', ' bg '),
175 (' berg ', 'bg '), (' berg ', ' bg ')}
178 def test_replace_suffix_decompose(self):
179 repl = self.get_replacements("~berg => bg")
181 assert repl == {('berg ', 'bg '), ('berg ', ' bg '),
182 (' berg ', 'bg '), (' berg ', ' bg ')}
185 def test_add_prefix_no_compose(self):
186 repl = self.get_replacements("hinter~ |-> hnt")
188 assert repl == {(' hinter', ' hinter'), (' hinter ', ' hinter '),
189 (' hinter', ' hnt'), (' hinter ', ' hnt ')}
192 def test_replace_prefix_no_compose(self):
193 repl = self.get_replacements("hinter~ |=> hnt")
195 assert repl == {(' hinter', ' hnt'), (' hinter ', ' hnt ')}
198 def test_add_prefix_compose(self):
199 repl = self.get_replacements("hinter~-> h")
201 assert repl == {(' hinter', ' hinter'), (' hinter', ' hinter '),
202 (' hinter', ' h'), (' hinter', ' h '),
203 (' hinter ', ' hinter '), (' hinter ', ' hinter'),
204 (' hinter ', ' h '), (' hinter ', ' h')}
207 def test_replace_prefix_compose(self):
208 repl = self.get_replacements("hinter~=> h")
210 assert repl == {(' hinter', ' h'), (' hinter', ' h '),
211 (' hinter ', ' h '), (' hinter ', ' h')}
214 def test_add_beginning_only(self):
215 repl = self.get_replacements("^Premier -> Pr")
217 assert repl == {('^ premier ', '^ premier '), ('^ premier ', '^ pr ')}
220 def test_replace_beginning_only(self):
221 repl = self.get_replacements("^Premier => Pr")
223 assert repl == {('^ premier ', '^ pr ')}
226 def test_add_final_only(self):
227 repl = self.get_replacements("road$ -> rd")
229 assert repl == {(' road ^', ' road ^'), (' road ^', ' rd ^')}
232 def test_replace_final_only(self):
233 repl = self.get_replacements("road$ => rd")
235 assert repl == {(' road ^', ' rd ^')}
238 def test_decompose_only(self):
239 repl = self.get_replacements("~foo -> foo")
241 assert repl == {('foo ', 'foo '), ('foo ', ' foo '),
242 (' foo ', 'foo '), (' foo ', ' foo ')}
245 def test_add_suffix_decompose_end_only(self):
246 repl = self.get_replacements("~berg |-> bg", "~berg$ -> bg")
248 assert repl == {('berg ', 'berg '), ('berg ', 'bg '),
249 (' berg ', ' berg '), (' berg ', ' bg '),
250 ('berg ^', 'berg ^'), ('berg ^', ' berg ^'),
251 ('berg ^', 'bg ^'), ('berg ^', ' bg ^'),
252 (' berg ^', 'berg ^'), (' berg ^', 'bg ^'),
253 (' berg ^', ' berg ^'), (' berg ^', ' bg ^')}
256 def test_replace_suffix_decompose_end_only(self):
257 repl = self.get_replacements("~berg |=> bg", "~berg$ => bg")
259 assert repl == {('berg ', 'bg '), (' berg ', ' bg '),
260 ('berg ^', 'bg ^'), ('berg ^', ' bg ^'),
261 (' berg ^', 'bg ^'), (' berg ^', ' bg ^')}
264 def test_add_multiple_suffix(self):
265 repl = self.get_replacements("~berg,~burg -> bg")
267 assert repl == {('berg ', 'berg '), ('berg ', ' berg '),
268 (' berg ', ' berg '), (' berg ', 'berg '),
269 ('berg ', 'bg '), ('berg ', ' bg '),
270 (' berg ', 'bg '), (' berg ', ' bg '),
271 ('burg ', 'burg '), ('burg ', ' burg '),
272 (' burg ', ' burg '), (' burg ', 'burg '),
273 ('burg ', 'bg '), ('burg ', ' bg '),
274 (' burg ', 'bg '), (' burg ', ' bg ')}