1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Tests for generic token analysis, mutation part.
12 from icu import Transliterator
14 import nominatim.tokenizer.token_analysis.generic as module
15 from nominatim.errors import UsageError
17 DEFAULT_NORMALIZATION = """ 'đł' > ' ';
18 [[:Nonspacing Mark:] [:Cf:]] >;
20 [[:Punctuation:][:Space:]]+ > ' '
23 DEFAULT_TRANSLITERATION = """ :: Latin ();
27 class TestMutationNoVariants:
29 def make_analyser(self, *mutations):
30 rules = { 'analyzer': 'generic',
31 'mutations': [ {'pattern': m[0], 'replacements': m[1]}
34 trans = Transliterator.createFromRules("test_trans", DEFAULT_TRANSLITERATION)
35 norm = Transliterator.createFromRules("test_norm", DEFAULT_NORMALIZATION)
36 config = module.configure(rules, norm, trans)
38 self.analysis = module.create(norm, trans, config)
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()))
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']))
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))
58 def test_simple_replacement(self):
59 self.make_analyser(('a', ['b']))
61 assert self.variants('none') == {'none'}
62 assert self.variants('abba') == {'bbbb'}
63 assert self.variants('2 aar') == {'2 bbr'}
66 def test_multichar_replacement(self):
67 self.make_analyser(('1 1', ['1 1 1']))
69 assert self.variants('1 1456') == {'1 1 1456'}
70 assert self.variants('1 1 1') == {'1 1 1 1'}
73 def test_removement_replacement(self):
74 self.make_analyser((' ', [' ', '']))
76 assert self.variants('A 345') == {'a 345', 'a345'}
77 assert self.variants('a g b') == {'a g b', 'ag b', 'a gb', 'agb'}
80 def test_regex_pattern(self):
81 self.make_analyser(('[^a-z]+', ['XXX', ' ']))
83 assert self.variants('a-34n12') == {'aXXXnXXX', 'aXXXn', 'a nXXX', 'a n'}
86 def test_multiple_mutations(self):
87 self.make_analyser(('À', ['À', 'ae']), ('ö', ['ö', 'oe']))
89 assert self.variants('LÀngenöhr') == {'lÀngenöhr', 'laengenöhr',
90 'lÀngenoehr', 'laengenoehr'}