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['replacements']
135 return sorted((k, sorted(v)) for k,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'])]
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', ' bg']),
160 ('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', ' bg', 'berg', 'bg']),
173 ('berg ', [' berg', ' bg', 'berg', 'bg'])]
176 def test_replace_suffix_decompose(self):
177 repl = self.get_replacements("~berg => bg")
179 assert repl == [(' berg ', [' bg', 'bg']),
180 ('berg ', [' bg', 'bg'])]
183 def test_add_prefix_no_compose(self):
184 repl = self.get_replacements("hinter~ |-> hnt")
186 assert repl == [(' hinter', [' hinter', ' hnt']),
187 (' hinter ', [' 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', [' h', ' h ', ' hinter', ' hinter ']),
200 (' hinter ', [' h', ' h', ' hinter', ' hinter'])]
203 def test_replace_prefix_compose(self):
204 repl = self.get_replacements("hinter~=> h")
206 assert repl == [(' hinter', [' h', ' h ']),
207 (' hinter ', [' h', ' h'])]
210 def test_add_beginning_only(self):
211 repl = self.get_replacements("^Premier -> Pr")
213 assert repl == [('^ premier ', ['^ pr', '^ premier'])]
216 def test_replace_beginning_only(self):
217 repl = self.get_replacements("^Premier => Pr")
219 assert repl == [('^ premier ', ['^ pr'])]
222 def test_add_final_only(self):
223 repl = self.get_replacements("road$ -> rd")
225 assert repl == [(' road ^', [' rd ^', ' road ^'])]
228 def test_replace_final_only(self):
229 repl = self.get_replacements("road$ => rd")
231 assert repl == [(' road ^', [' rd ^'])]
234 def test_decompose_only(self):
235 repl = self.get_replacements("~foo -> foo")
237 assert repl == [(' foo ', [' foo', 'foo']),
238 ('foo ', [' foo', 'foo'])]
241 def test_add_suffix_decompose_end_only(self):
242 repl = self.get_replacements("~berg |-> bg", "~berg$ -> bg")
244 assert repl == [(' berg ', [' berg', ' bg']),
245 (' berg ^', [' berg ^', ' bg ^', 'berg ^', 'bg ^']),
246 ('berg ', ['berg', 'bg']),
247 ('berg ^', [' berg ^', ' bg ^', 'berg ^', 'bg ^'])]
250 def test_replace_suffix_decompose_end_only(self):
251 repl = self.get_replacements("~berg |=> bg", "~berg$ => bg")
253 assert repl == [(' berg ', [' bg']),
254 (' berg ^', [' bg ^', 'bg ^']),
256 ('berg ^', [' bg ^', 'bg ^'])]
259 def test_add_multiple_suffix(self):
260 repl = self.get_replacements("~berg,~burg -> bg")
262 assert repl == [(' berg ', [' berg', ' bg', 'berg', 'bg']),
263 (' burg ', [' bg', ' burg', 'bg', 'burg']),
264 ('berg ', [' berg', ' bg', 'berg', 'bg']),
265 ('burg ', [' bg', ' burg', 'bg', 'burg'])]