X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/7cfcbacfc75ab2e39ee7eab6a5cf40e8cbd152f5..c1fa70639ba2f861cd6a7718e5b02f0ade763abc:/test/python/test_tokenizer_icu_rule_loader.py diff --git a/test/python/test_tokenizer_icu_rule_loader.py b/test/python/test_tokenizer_icu_rule_loader.py index b76eee75..e22ccd4b 100644 --- a/test/python/test_tokenizer_icu_rule_loader.py +++ b/test/python/test_tokenizer_icu_rule_loader.py @@ -122,6 +122,15 @@ def test_transliteration_rules_from_file(test_config): assert trans.transliterate(" axxt ") == " byt " +def test_search_rules(cfgrules): + config = cfgrules('~street => s,st', 'master => mstr') + proc = ICURuleLoader(config).make_token_analysis() + + assert proc.search.transliterate('Master Street').strip() == 'master street' + assert proc.search.transliterate('Earnes St').strip() == 'earnes st' + assert proc.search.transliterate('Nostreet').strip() == 'nostreet' + + class TestGetReplacements: @pytest.fixture(autouse=True) @@ -130,9 +139,9 @@ class TestGetReplacements: def get_replacements(self, *variants): loader = ICURuleLoader(self.cfgrules(*variants)) - rules = loader.analysis[None].config['variants'] + rules = loader.analysis[None].config['replacements'] - return set((v.source, v.replacement) for v in rules) + return sorted((k, sorted(v)) for k,v in rules) @pytest.mark.parametrize("variant", ['foo > bar', 'foo -> bar -> bar', @@ -144,131 +153,122 @@ class TestGetReplacements: def test_add_full(self): repl = self.get_replacements("foo -> bar") - assert repl == {(' foo ', ' bar '), (' foo ', ' foo ')} + assert repl == [(' foo ', [' bar', ' foo'])] def test_replace_full(self): repl = self.get_replacements("foo => bar") - assert repl == {(' foo ', ' bar ')} + assert repl == [(' foo ', [' bar'])] def test_add_suffix_no_decompose(self): repl = self.get_replacements("~berg |-> bg") - assert repl == {('berg ', 'berg '), ('berg ', 'bg '), - (' berg ', ' berg '), (' berg ', ' bg ')} + assert repl == [(' berg ', [' berg', ' bg']), + ('berg ', ['berg', 'bg'])] def test_replace_suffix_no_decompose(self): repl = self.get_replacements("~berg |=> bg") - assert repl == {('berg ', 'bg '), (' berg ', ' bg ')} + assert repl == [(' berg ', [' bg']),('berg ', ['bg'])] def test_add_suffix_decompose(self): repl = self.get_replacements("~berg -> bg") - assert repl == {('berg ', 'berg '), ('berg ', ' berg '), - (' berg ', ' berg '), (' berg ', 'berg '), - ('berg ', 'bg '), ('berg ', ' bg '), - (' berg ', 'bg '), (' berg ', ' bg ')} + assert repl == [(' berg ', [' berg', ' bg', 'berg', 'bg']), + ('berg ', [' berg', ' bg', 'berg', 'bg'])] def test_replace_suffix_decompose(self): repl = self.get_replacements("~berg => bg") - assert repl == {('berg ', 'bg '), ('berg ', ' bg '), - (' berg ', 'bg '), (' berg ', ' bg ')} + assert repl == [(' berg ', [' bg', 'bg']), + ('berg ', [' bg', 'bg'])] def test_add_prefix_no_compose(self): repl = self.get_replacements("hinter~ |-> hnt") - assert repl == {(' hinter', ' hinter'), (' hinter ', ' hinter '), - (' hinter', ' hnt'), (' hinter ', ' hnt ')} + assert repl == [(' hinter', [' hinter', ' hnt']), + (' hinter ', [' hinter', ' hnt'])] def test_replace_prefix_no_compose(self): repl = self.get_replacements("hinter~ |=> hnt") - assert repl == {(' hinter', ' hnt'), (' hinter ', ' hnt ')} + assert repl == [(' hinter', [' hnt']), (' hinter ', [' hnt'])] def test_add_prefix_compose(self): repl = self.get_replacements("hinter~-> h") - assert repl == {(' hinter', ' hinter'), (' hinter', ' hinter '), - (' hinter', ' h'), (' hinter', ' h '), - (' hinter ', ' hinter '), (' hinter ', ' hinter'), - (' hinter ', ' h '), (' hinter ', ' h')} + assert repl == [(' hinter', [' h', ' h ', ' hinter', ' hinter ']), + (' hinter ', [' h', ' h', ' hinter', ' hinter'])] def test_replace_prefix_compose(self): repl = self.get_replacements("hinter~=> h") - assert repl == {(' hinter', ' h'), (' hinter', ' h '), - (' hinter ', ' h '), (' hinter ', ' h')} + assert repl == [(' hinter', [' h', ' h ']), + (' hinter ', [' h', ' h'])] def test_add_beginning_only(self): repl = self.get_replacements("^Premier -> Pr") - assert repl == {('^ premier ', '^ premier '), ('^ premier ', '^ pr ')} + assert repl == [('^ premier ', ['^ pr', '^ premier'])] def test_replace_beginning_only(self): repl = self.get_replacements("^Premier => Pr") - assert repl == {('^ premier ', '^ pr ')} + assert repl == [('^ premier ', ['^ pr'])] def test_add_final_only(self): repl = self.get_replacements("road$ -> rd") - assert repl == {(' road ^', ' road ^'), (' road ^', ' rd ^')} + assert repl == [(' road ^', [' rd ^', ' road ^'])] def test_replace_final_only(self): repl = self.get_replacements("road$ => rd") - assert repl == {(' road ^', ' rd ^')} + assert repl == [(' road ^', [' rd ^'])] def test_decompose_only(self): repl = self.get_replacements("~foo -> foo") - assert repl == {('foo ', 'foo '), ('foo ', ' foo '), - (' foo ', 'foo '), (' foo ', ' foo ')} + assert repl == [(' foo ', [' foo', 'foo']), + ('foo ', [' foo', 'foo'])] def test_add_suffix_decompose_end_only(self): repl = self.get_replacements("~berg |-> bg", "~berg$ -> bg") - assert repl == {('berg ', 'berg '), ('berg ', 'bg '), - (' berg ', ' berg '), (' berg ', ' bg '), - ('berg ^', 'berg ^'), ('berg ^', ' berg ^'), - ('berg ^', 'bg ^'), ('berg ^', ' bg ^'), - (' berg ^', 'berg ^'), (' berg ^', 'bg ^'), - (' berg ^', ' berg ^'), (' berg ^', ' bg ^')} + assert repl == [(' berg ', [' berg', ' bg']), + (' berg ^', [' berg ^', ' bg ^', 'berg ^', 'bg ^']), + ('berg ', ['berg', 'bg']), + ('berg ^', [' berg ^', ' bg ^', 'berg ^', 'bg ^'])] def test_replace_suffix_decompose_end_only(self): repl = self.get_replacements("~berg |=> bg", "~berg$ => bg") - assert repl == {('berg ', 'bg '), (' berg ', ' bg '), - ('berg ^', 'bg ^'), ('berg ^', ' bg ^'), - (' berg ^', 'bg ^'), (' berg ^', ' bg ^')} + assert repl == [(' berg ', [' bg']), + (' berg ^', [' bg ^', 'bg ^']), + ('berg ', ['bg']), + ('berg ^', [' bg ^', 'bg ^'])] def test_add_multiple_suffix(self): repl = self.get_replacements("~berg,~burg -> bg") - assert repl == {('berg ', 'berg '), ('berg ', ' berg '), - (' berg ', ' berg '), (' berg ', 'berg '), - ('berg ', 'bg '), ('berg ', ' bg '), - (' berg ', 'bg '), (' berg ', ' bg '), - ('burg ', 'burg '), ('burg ', ' burg '), - (' burg ', ' burg '), (' burg ', 'burg '), - ('burg ', 'bg '), ('burg ', ' bg '), - (' burg ', 'bg '), (' burg ', ' bg ')} + assert repl == [(' berg ', [' berg', ' bg', 'berg', 'bg']), + (' burg ', [' bg', ' burg', 'bg', 'burg']), + ('berg ', [' berg', ' bg', 'berg', 'bg']), + ('burg ', [' bg', ' burg', 'bg', 'burg'])]