]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_tokenizer_icu_name_processor.py
limit the number of variants that can be produced
[nominatim.git] / test / python / test_tokenizer_icu_name_processor.py
1 """
2 Tests for import name normalisation and variant generation.
3 """
4 from textwrap import dedent
5
6 import pytest
7
8 from nominatim.tokenizer.icu_rule_loader import ICURuleLoader
9 from nominatim.tokenizer.icu_name_processor import ICUNameProcessor, ICUNameProcessorRules
10
11 from nominatim.errors import UsageError
12
13 @pytest.fixture
14 def cfgfile(tmp_path, suffix='.yaml'):
15     def _create_config(*variants, **kwargs):
16         content = dedent("""\
17         normalization:
18             - ":: NFD ()"
19             - "'🜳' > ' '"
20             - "[[:Nonspacing Mark:] [:Cf:]] >"
21             - ":: lower ()"
22             - "[[:Punctuation:][:Space:]]+ > ' '"
23             - ":: NFC ()"
24         transliteration:
25             - "::  Latin ()"
26             - "'🜵' > ' '"
27         """)
28         content += "variants:\n  - words:\n"
29         content += '\n'.join(("      - " + s for s in variants)) + '\n'
30         for k, v in kwargs:
31             content += "    {}: {}\n".format(k, v)
32         fpath = tmp_path / ('test_config' + suffix)
33         fpath.write_text(dedent(content))
34         return fpath
35
36     return _create_config
37
38
39 def get_normalized_variants(proc, name):
40     return proc.get_variants_ascii(proc.get_normalized(name))
41
42
43 def test_variants_empty(cfgfile):
44     fpath = cfgfile('saint -> 🜵', 'street -> st')
45
46     rules = ICUNameProcessorRules(loader=ICURuleLoader(fpath))
47     proc = ICUNameProcessor(rules)
48
49     assert get_normalized_variants(proc, '🜵') == []
50     assert get_normalized_variants(proc, '🜳') == []
51     assert get_normalized_variants(proc, 'saint') == ['saint']
52
53
54 VARIANT_TESTS = [
55 (('~strasse,~straße -> str', '~weg => weg'), "hallo", {'hallo'}),
56 (('weg => wg',), "holzweg", {'holzweg'}),
57 (('weg -> wg',), "holzweg", {'holzweg'}),
58 (('~weg => weg',), "holzweg", {'holz weg', 'holzweg'}),
59 (('~weg -> weg',), "holzweg",  {'holz weg', 'holzweg'}),
60 (('~weg => w',), "holzweg", {'holz w', 'holzw'}),
61 (('~weg -> w',), "holzweg",  {'holz weg', 'holzweg', 'holz w', 'holzw'}),
62 (('~weg => weg',), "Meier Weg", {'meier weg', 'meierweg'}),
63 (('~weg -> weg',), "Meier Weg", {'meier weg', 'meierweg'}),
64 (('~weg => w',), "Meier Weg", {'meier w', 'meierw'}),
65 (('~weg -> w',), "Meier Weg", {'meier weg', 'meierweg', 'meier w', 'meierw'}),
66 (('weg => wg',), "Meier Weg", {'meier wg'}),
67 (('weg -> wg',), "Meier Weg", {'meier weg', 'meier wg'}),
68 (('~strasse,~straße -> str', '~weg => weg'), "Bauwegstraße",
69      {'bauweg straße', 'bauweg str', 'bauwegstraße', 'bauwegstr'}),
70 (('am => a', 'bach => b'), "am bach", {'a b'}),
71 (('am => a', '~bach => b'), "am bach", {'a b'}),
72 (('am -> a', '~bach -> b'), "am bach", {'am bach', 'a bach', 'am b', 'a b'}),
73 (('am -> a', '~bach -> b'), "ambach", {'ambach', 'am bach', 'amb', 'am b'}),
74 (('saint -> s,st', 'street -> st'), "Saint Johns Street",
75      {'saint johns street', 's johns street', 'st johns street',
76       'saint johns st', 's johns st', 'st johns st'}),
77 (('river$ -> r',), "River Bend Road", {'river bend road'}),
78 (('river$ -> r',), "Bent River", {'bent river', 'bent r'}),
79 (('^north => n',), "North 2nd Street", {'n 2nd street'}),
80 (('^north => n',), "Airport North", {'airport north'}),
81 (('am -> a',), "am am am am am am am am", {'am am am am am am am am'}),
82 (('am => a',), "am am am am am am am am", {'a a a a a a a a'})
83 ]
84
85 @pytest.mark.parametrize("rules,name,variants", VARIANT_TESTS)
86 def test_variants(cfgfile, rules, name, variants):
87     fpath = cfgfile(*rules)
88     proc = ICUNameProcessor(ICUNameProcessorRules(loader=ICURuleLoader(fpath)))
89
90     result = get_normalized_variants(proc, name)
91
92     assert len(result) == len(set(result))
93     assert set(get_normalized_variants(proc, name)) == variants
94
95
96 def test_search_normalized(cfgfile):
97     fpath = cfgfile('~street => s,st', 'master => mstr')
98
99     rules = ICUNameProcessorRules(loader=ICURuleLoader(fpath))
100     proc = ICUNameProcessor(rules)
101
102     assert proc.get_search_normalized('Master Street') == 'master street'
103     assert proc.get_search_normalized('Earnes St') == 'earnes st'
104     assert proc.get_search_normalized('Nostreet') == 'nostreet'