]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tokenizer/token_analysis/test_generic_mutation.py
Merge pull request #2621 from lonvia/housenumber-analyzer
[nominatim.git] / test / python / tokenizer / token_analysis / test_generic_mutation.py
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Tests for generic token analysis, mutation part.
9 """
10 import pytest
11
12 from icu import Transliterator
13
14 import nominatim.tokenizer.token_analysis.generic as module
15 from nominatim.errors import UsageError
16
17 DEFAULT_NORMALIZATION = """ '🜳' > ' ';
18                             [[:Nonspacing Mark:] [:Cf:]] >;
19                             :: lower ();
20                             [[:Punctuation:][:Space:]]+ > ' '
21                         """
22
23 DEFAULT_TRANSLITERATION = """ ::  Latin ();
24                               'đŸœ”' > ' ';
25                           """
26
27 class TestMutationNoVariants:
28
29     def make_analyser(self, *mutations):
30         rules = { 'analyzer': 'generic',
31                   'mutations': [ {'pattern': m[0], 'replacements': m[1]}
32                                  for m in mutations]
33                 }
34         config = module.configure(rules, DEFAULT_NORMALIZATION)
35         trans = Transliterator.createFromRules("test_trans", DEFAULT_TRANSLITERATION)
36         norm = Transliterator.createFromRules("test_norm", DEFAULT_NORMALIZATION)
37
38         self.analysis = module.create(norm, trans, config)
39
40
41     def variants(self, name):
42         norm = Transliterator.createFromRules("test_norm", DEFAULT_NORMALIZATION)
43         return set(self.analysis.get_variants_ascii(norm.transliterate(name).strip()))
44
45
46     @pytest.mark.parametrize('pattern', ('(capture)', ['a list']))
47     def test_bad_pattern(self, pattern):
48         with pytest.raises(UsageError):
49             self.make_analyser((pattern, ['b']))
50
51
52     @pytest.mark.parametrize('replacements', (None, 'a string'))
53     def test_bad_replacement(self, replacements):
54         with pytest.raises(UsageError):
55             self.make_analyser(('a', replacements))
56
57
58     def test_simple_replacement(self):
59         self.make_analyser(('a', ['b']))
60
61         assert self.variants('none') == {'none'}
62         assert self.variants('abba') == {'bbbb'}
63         assert self.variants('2 aar') == {'2 bbr'}
64
65
66     def test_multichar_replacement(self):
67         self.make_analyser(('1 1', ['1 1 1']))
68
69         assert self.variants('1 1456') == {'1 1 1456'}
70         assert self.variants('1 1 1') == {'1 1 1 1'}
71
72
73     def test_removement_replacement(self):
74         self.make_analyser((' ', [' ', '']))
75
76         assert self.variants('A 345') == {'a 345', 'a345'}
77         assert self.variants('a g b') == {'a g b', 'ag b', 'a gb', 'agb'}
78
79
80     def test_regex_pattern(self):
81         self.make_analyser(('[^a-z]+', ['XXX', ' ']))
82
83         assert self.variants('a-34n12') == {'aXXXnXXX', 'aXXXn', 'a nXXX', 'a n'}
84
85
86     def test_multiple_mutations(self):
87         self.make_analyser(('Ă€', ['Ă€', 'ae']), ('ö', ['ö', 'oe']))
88
89         assert self.variants('LĂ€ngenöhr') == {'lĂ€ngenöhr', 'laengenöhr',
90                                               'lĂ€ngenoehr', 'laengenoehr'}