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
14 CONFIG_SECTIONS = ('normalization', 'transliteration', 'token-analysis')
16 class TestIcuRuleLoader:
18 @pytest.fixture(autouse=True)
19 def init_env(self, project_env):
20 self.project_env = project_env
23 def write_config(self, content):
24 (self.project_env.project_dir / 'icu_tokenizer.yaml').write_text(dedent(content))
27 def config_rules(self, *variants):
31 - "[[:Nonspacing Mark:] [:Cf:]] >"
33 - "[[:Punctuation:][:Space:]]+ > ' '"
37 - "[[:Punctuation:][:Space:]]+ > ' '"
43 content += '\n'.join((" - " + s for s in variants)) + '\n'
44 self.write_config(content)
47 def get_replacements(self, *variants):
48 self.config_rules(*variants)
49 loader = ICURuleLoader(self.project_env)
50 rules = loader.analysis[None].config['replacements']
52 return sorted((k, sorted(v)) for k,v in rules)
55 def test_empty_rule_set(self):
56 self.write_config("""\
64 rules = ICURuleLoader(self.project_env)
65 assert rules.get_search_rules() == ''
66 assert rules.get_normalization_rules() == ''
67 assert rules.get_transliteration_rules() == ''
70 @pytest.mark.parametrize("section", CONFIG_SECTIONS)
71 def test_missing_section(self, section):
72 rule_cfg = { s: [] for s in CONFIG_SECTIONS if s != section}
73 self.write_config(yaml.dump(rule_cfg))
75 with pytest.raises(UsageError):
76 ICURuleLoader(self.project_env)
79 def test_get_search_rules(self):
81 loader = ICURuleLoader(self.project_env)
83 rules = loader.get_search_rules()
84 trans = Transliterator.createFromRules("test", rules)
86 assert trans.transliterate(" Baum straße ") == " baum straße "
87 assert trans.transliterate(" Baumstraße ") == " baumstraße "
88 assert trans.transliterate(" Baumstrasse ") == " baumstrasse "
89 assert trans.transliterate(" Baumstr ") == " baumstr "
90 assert trans.transliterate(" Baumwegstr ") == " baumwegstr "
91 assert trans.transliterate(" Αθήνα ") == " athēna "
92 assert trans.transliterate(" проспект ") == " prospekt "
95 def test_get_normalization_rules(self):
97 loader = ICURuleLoader(self.project_env)
98 rules = loader.get_normalization_rules()
99 trans = Transliterator.createFromRules("test", rules)
101 assert trans.transliterate(" проспект-Prospekt ") == " проспект prospekt "
104 def test_get_transliteration_rules(self):
106 loader = ICURuleLoader(self.project_env)
107 rules = loader.get_transliteration_rules()
108 trans = Transliterator.createFromRules("test", rules)
110 assert trans.transliterate(" проспект-Prospekt ") == " prospekt Prospekt "
113 def test_transliteration_rules_from_file(self):
114 self.write_config("""\
118 - !include transliteration.yaml
123 transpath = self.project_env.project_dir / ('transliteration.yaml')
124 transpath.write_text('- "x > y"')
126 loader = ICURuleLoader(self.project_env)
127 rules = loader.get_transliteration_rules()
128 trans = Transliterator.createFromRules("test", rules)
130 assert trans.transliterate(" axxt ") == " byt "
133 def test_search_rules(self):
134 self.config_rules('~street => s,st', 'master => mstr')
135 proc = ICURuleLoader(self.project_env).make_token_analysis()
137 assert proc.search.transliterate('Master Street').strip() == 'master street'
138 assert proc.search.transliterate('Earnes St').strip() == 'earnes st'
139 assert proc.search.transliterate('Nostreet').strip() == 'nostreet'
142 @pytest.mark.parametrize("variant", ['foo > bar', 'foo -> bar -> bar',
143 '~foo~ -> bar', 'fo~ o -> bar'])
144 def test_invalid_variant_description(self, variant):
145 self.config_rules(variant)
146 with pytest.raises(UsageError):
147 ICURuleLoader(self.project_env)
149 def test_add_full(self):
150 repl = self.get_replacements("foo -> bar")
152 assert repl == [(' foo ', [' bar', ' foo'])]
155 def test_replace_full(self):
156 repl = self.get_replacements("foo => bar")
158 assert repl == [(' foo ', [' bar'])]
161 def test_add_suffix_no_decompose(self):
162 repl = self.get_replacements("~berg |-> bg")
164 assert repl == [(' berg ', [' berg', ' bg']),
165 ('berg ', ['berg', 'bg'])]
168 def test_replace_suffix_no_decompose(self):
169 repl = self.get_replacements("~berg |=> bg")
171 assert repl == [(' berg ', [' bg']),('berg ', ['bg'])]
174 def test_add_suffix_decompose(self):
175 repl = self.get_replacements("~berg -> bg")
177 assert repl == [(' berg ', [' berg', ' bg', 'berg', 'bg']),
178 ('berg ', [' berg', ' bg', 'berg', 'bg'])]
181 def test_replace_suffix_decompose(self):
182 repl = self.get_replacements("~berg => bg")
184 assert repl == [(' berg ', [' bg', 'bg']),
185 ('berg ', [' bg', 'bg'])]
188 def test_add_prefix_no_compose(self):
189 repl = self.get_replacements("hinter~ |-> hnt")
191 assert repl == [(' hinter', [' hinter', ' hnt']),
192 (' hinter ', [' hinter', ' hnt'])]
195 def test_replace_prefix_no_compose(self):
196 repl = self.get_replacements("hinter~ |=> hnt")
198 assert repl == [(' hinter', [' hnt']), (' hinter ', [' hnt'])]
201 def test_add_prefix_compose(self):
202 repl = self.get_replacements("hinter~-> h")
204 assert repl == [(' hinter', [' h', ' h ', ' hinter', ' hinter ']),
205 (' hinter ', [' h', ' h', ' hinter', ' hinter'])]
208 def test_replace_prefix_compose(self):
209 repl = self.get_replacements("hinter~=> h")
211 assert repl == [(' hinter', [' h', ' h ']),
212 (' hinter ', [' h', ' h'])]
215 def test_add_beginning_only(self):
216 repl = self.get_replacements("^Premier -> Pr")
218 assert repl == [('^ premier ', ['^ pr', '^ premier'])]
221 def test_replace_beginning_only(self):
222 repl = self.get_replacements("^Premier => Pr")
224 assert repl == [('^ premier ', ['^ pr'])]
227 def test_add_final_only(self):
228 repl = self.get_replacements("road$ -> rd")
230 assert repl == [(' road ^', [' rd ^', ' road ^'])]
233 def test_replace_final_only(self):
234 repl = self.get_replacements("road$ => rd")
236 assert repl == [(' road ^', [' rd ^'])]
239 def test_decompose_only(self):
240 repl = self.get_replacements("~foo -> foo")
242 assert repl == [(' foo ', [' foo', 'foo']),
243 ('foo ', [' foo', 'foo'])]
246 def test_add_suffix_decompose_end_only(self):
247 repl = self.get_replacements("~berg |-> bg", "~berg$ -> bg")
249 assert repl == [(' berg ', [' berg', ' bg']),
250 (' berg ^', [' berg ^', ' bg ^', 'berg ^', 'bg ^']),
251 ('berg ', ['berg', 'bg']),
252 ('berg ^', [' berg ^', ' bg ^', 'berg ^', 'bg ^'])]
255 def test_replace_suffix_decompose_end_only(self):
256 repl = self.get_replacements("~berg |=> bg", "~berg$ => bg")
258 assert repl == [(' berg ', [' bg']),
259 (' berg ^', [' bg ^', 'bg ^']),
261 ('berg ^', [' bg ^', 'bg ^'])]
264 def test_add_multiple_suffix(self):
265 repl = self.get_replacements("~berg,~burg -> bg")
267 assert repl == [(' berg ', [' berg', ' bg', 'berg', 'bg']),
268 (' burg ', [' bg', ' burg', 'bg', 'burg']),
269 ('berg ', [' berg', ' bg', 'berg', 'bg']),
270 ('burg ', [' bg', ' burg', 'bg', 'burg'])]