1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2024 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Tests for converting a config file to ICU rules.
10 from textwrap import dedent
15 from nominatim_db.tokenizer.icu_rule_loader import ICURuleLoader
16 from nominatim_db.errors import UsageError
18 from icu import Transliterator
20 CONFIG_SECTIONS = ('normalization', 'transliteration', 'token-analysis')
22 class TestIcuRuleLoader:
24 @pytest.fixture(autouse=True)
25 def init_env(self, project_env):
26 self.project_env = project_env
29 def write_config(self, content):
30 (self.project_env.project_dir / 'icu_tokenizer.yaml').write_text(dedent(content))
33 def config_rules(self, *variants):
37 - "[[:Nonspacing Mark:] [:Cf:]] >"
39 - "[[:Punctuation:][:Space:]]+ > ' '"
43 - "[[:Punctuation:][:Space:]]+ > ' '"
49 content += '\n'.join((" - " + s for s in variants)) + '\n'
50 self.write_config(content)
53 def get_replacements(self, *variants):
54 self.config_rules(*variants)
55 loader = ICURuleLoader(self.project_env)
56 rules = loader.analysis[None].config['replacements']
58 return sorted((k, sorted(v)) for k,v in rules)
61 def test_empty_rule_set(self):
62 self.write_config("""\
70 rules = ICURuleLoader(self.project_env)
71 assert rules.get_search_rules() == ''
72 assert rules.get_normalization_rules() == ''
73 assert rules.get_transliteration_rules() == ''
76 @pytest.mark.parametrize("section", CONFIG_SECTIONS)
77 def test_missing_section(self, section):
78 rule_cfg = { s: [] for s in CONFIG_SECTIONS if s != section}
79 self.write_config(yaml.dump(rule_cfg))
81 with pytest.raises(UsageError):
82 ICURuleLoader(self.project_env)
85 def test_get_search_rules(self):
87 loader = ICURuleLoader(self.project_env)
89 rules = loader.get_search_rules()
90 trans = Transliterator.createFromRules("test", rules)
92 assert trans.transliterate(" Baum straße ") == " baum straße "
93 assert trans.transliterate(" Baumstraße ") == " baumstraße "
94 assert trans.transliterate(" Baumstrasse ") == " baumstrasse "
95 assert trans.transliterate(" Baumstr ") == " baumstr "
96 assert trans.transliterate(" Baumwegstr ") == " baumwegstr "
97 assert trans.transliterate(" Αθήνα ") == " athēna "
98 assert trans.transliterate(" проспект ") == " prospekt "
101 def test_get_normalization_rules(self):
103 loader = ICURuleLoader(self.project_env)
104 rules = loader.get_normalization_rules()
105 trans = Transliterator.createFromRules("test", rules)
107 assert trans.transliterate(" проспект-Prospekt ") == " проспект prospekt "
110 def test_get_transliteration_rules(self):
112 loader = ICURuleLoader(self.project_env)
113 rules = loader.get_transliteration_rules()
114 trans = Transliterator.createFromRules("test", rules)
116 assert trans.transliterate(" проспект-Prospekt ") == " prospekt Prospekt "
119 def test_transliteration_rules_from_file(self):
120 self.write_config("""\
124 - !include transliteration.yaml
129 transpath = self.project_env.project_dir / ('transliteration.yaml')
130 transpath.write_text('- "x > y"')
132 loader = ICURuleLoader(self.project_env)
133 rules = loader.get_transliteration_rules()
134 trans = Transliterator.createFromRules("test", rules)
136 assert trans.transliterate(" axxt ") == " byt "
139 def test_search_rules(self):
140 self.config_rules('~street => s,st', 'master => mstr')
141 proc = ICURuleLoader(self.project_env).make_token_analysis()
143 assert proc.search.transliterate('Master Street').strip() == 'master street'
144 assert proc.search.transliterate('Earnes St').strip() == 'earnes st'
145 assert proc.search.transliterate('Nostreet').strip() == 'nostreet'
148 @pytest.mark.parametrize("variant", ['foo > bar', 'foo -> bar -> bar',
149 '~foo~ -> bar', 'fo~ o -> bar'])
150 def test_invalid_variant_description(self, variant):
151 self.config_rules(variant)
152 with pytest.raises(UsageError):
153 ICURuleLoader(self.project_env)
155 def test_add_full(self):
156 repl = self.get_replacements("foo -> bar")
158 assert repl == [(' foo ', [' bar', ' foo'])]
161 def test_replace_full(self):
162 repl = self.get_replacements("foo => bar")
164 assert repl == [(' foo ', [' bar'])]
167 def test_add_suffix_no_decompose(self):
168 repl = self.get_replacements("~berg |-> bg")
170 assert repl == [(' berg ', [' berg', ' bg']),
171 ('berg ', ['berg', 'bg'])]
174 def test_replace_suffix_no_decompose(self):
175 repl = self.get_replacements("~berg |=> bg")
177 assert repl == [(' berg ', [' bg']),('berg ', ['bg'])]
180 def test_add_suffix_decompose(self):
181 repl = self.get_replacements("~berg -> bg")
183 assert repl == [(' berg ', [' berg', ' bg', 'berg', 'bg']),
184 ('berg ', [' berg', ' bg', 'berg', 'bg'])]
187 def test_replace_suffix_decompose(self):
188 repl = self.get_replacements("~berg => bg")
190 assert repl == [(' berg ', [' bg', 'bg']),
191 ('berg ', [' bg', 'bg'])]
194 def test_add_prefix_no_compose(self):
195 repl = self.get_replacements("hinter~ |-> hnt")
197 assert repl == [(' hinter', [' hinter', ' hnt']),
198 (' hinter ', [' hinter', ' hnt'])]
201 def test_replace_prefix_no_compose(self):
202 repl = self.get_replacements("hinter~ |=> hnt")
204 assert repl == [(' hinter', [' hnt']), (' hinter ', [' hnt'])]
207 def test_add_prefix_compose(self):
208 repl = self.get_replacements("hinter~-> h")
210 assert repl == [(' hinter', [' h', ' h ', ' hinter', ' hinter ']),
211 (' hinter ', [' h', ' h', ' hinter', ' hinter'])]
214 def test_replace_prefix_compose(self):
215 repl = self.get_replacements("hinter~=> h")
217 assert repl == [(' hinter', [' h', ' h ']),
218 (' hinter ', [' h', ' h'])]
221 def test_add_beginning_only(self):
222 repl = self.get_replacements("^Premier -> Pr")
224 assert repl == [('^ premier ', ['^ pr', '^ premier'])]
227 def test_replace_beginning_only(self):
228 repl = self.get_replacements("^Premier => Pr")
230 assert repl == [('^ premier ', ['^ pr'])]
233 def test_add_final_only(self):
234 repl = self.get_replacements("road$ -> rd")
236 assert repl == [(' road ^', [' rd ^', ' road ^'])]
239 def test_replace_final_only(self):
240 repl = self.get_replacements("road$ => rd")
242 assert repl == [(' road ^', [' rd ^'])]
245 def test_decompose_only(self):
246 repl = self.get_replacements("~foo -> foo")
248 assert repl == [(' foo ', [' foo', 'foo']),
249 ('foo ', [' foo', 'foo'])]
252 def test_add_suffix_decompose_end_only(self):
253 repl = self.get_replacements("~berg |-> bg", "~berg$ -> bg")
255 assert repl == [(' berg ', [' berg', ' bg']),
256 (' berg ^', [' berg ^', ' bg ^', 'berg ^', 'bg ^']),
257 ('berg ', ['berg', 'bg']),
258 ('berg ^', [' berg ^', ' bg ^', 'berg ^', 'bg ^'])]
261 def test_replace_suffix_decompose_end_only(self):
262 repl = self.get_replacements("~berg |=> bg", "~berg$ => bg")
264 assert repl == [(' berg ', [' bg']),
265 (' berg ^', [' bg ^', 'bg ^']),
267 ('berg ^', [' bg ^', 'bg ^'])]
270 def test_add_multiple_suffix(self):
271 repl = self.get_replacements("~berg,~burg -> bg")
273 assert repl == [(' berg ', [' berg', ' bg', 'berg', 'bg']),
274 (' burg ', [' bg', ' burg', 'bg', 'burg']),
275 ('berg ', [' berg', ' bg', 'berg', 'bg']),
276 ('burg ', [' bg', ' burg', 'bg', 'burg'])]