]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tokenizer/token_analysis/test_generic_mutation.py
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / test / python / tokenizer / token_analysis / test_generic_mutation.py
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2025 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_db.tokenizer.token_analysis.generic as module
15 from nominatim_db.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
28 class TestMutationNoVariants:
29
30     def make_analyser(self, *mutations):
31         rules = {'analyzer': 'generic',
32                  'mutations': [{'pattern': m[0], 'replacements': m[1]}
33                                for m in mutations]
34                  }
35         trans = Transliterator.createFromRules("test_trans", DEFAULT_TRANSLITERATION)
36         norm = Transliterator.createFromRules("test_norm", DEFAULT_NORMALIZATION)
37         config = module.configure(rules, norm, trans)
38
39         self.analysis = module.create(norm, trans, config)
40
41     def variants(self, name):
42         norm = Transliterator.createFromRules("test_norm", DEFAULT_NORMALIZATION)
43         return set(self.analysis.compute_variants(norm.transliterate(name).strip()))
44
45     @pytest.mark.parametrize('pattern', ('(capture)', ['a list']))
46     def test_bad_pattern(self, pattern):
47         with pytest.raises(UsageError):
48             self.make_analyser((pattern, ['b']))
49
50     @pytest.mark.parametrize('replacements', (None, 'a string'))
51     def test_bad_replacement(self, replacements):
52         with pytest.raises(UsageError):
53             self.make_analyser(('a', replacements))
54
55     def test_simple_replacement(self):
56         self.make_analyser(('a', ['b']))
57
58         assert self.variants('none') == {'none'}
59         assert self.variants('abba') == {'bbbb'}
60         assert self.variants('2 aar') == {'2 bbr'}
61
62     def test_multichar_replacement(self):
63         self.make_analyser(('1 1', ['1 1 1']))
64
65         assert self.variants('1 1456') == {'1 1 1456'}
66         assert self.variants('1 1 1') == {'1 1 1 1'}
67
68     def test_removement_replacement(self):
69         self.make_analyser((' ', [' ', '']))
70
71         assert self.variants('A 345') == {'a 345', 'a345'}
72         assert self.variants('a g b') == {'a g b', 'ag b', 'a gb', 'agb'}
73
74     def test_regex_pattern(self):
75         self.make_analyser(('[^a-z]+', ['XXX', ' ']))
76
77         assert self.variants('a-34n12') == {'aXXXnXXX', 'aXXXn', 'a nXXX', 'a n'}
78
79     def test_multiple_mutations(self):
80         self.make_analyser(('Ă€', ['Ă€', 'ae']), ('ö', ['ö', 'oe']))
81
82         assert self.variants('LĂ€ngenöhr') == {'lĂ€ngenöhr', 'laengenöhr',
83                                               'lĂ€ngenoehr', 'laengenoehr'}