]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_tokenizer_icu_name_processor.py
complete tests for icu tokenizer
[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(suffixes, abbr):
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 += "compound_suffixes:\n"
29         content += '\n'.join(("    - " + s for s in suffixes)) + '\n'
30         content += "abbreviations:\n"
31         content += '\n'.join(("    - " + s for s in abbr)) + '\n'
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 def test_simple_variants(cfgfile):
43     fpath = cfgfile(['strasse', 'straße', 'weg'],
44                     ['strasse,straße => str',
45                      'prospekt => pr'])
46
47     rules = ICUNameProcessorRules(loader=ICURuleLoader(fpath))
48     proc = ICUNameProcessor(rules)
49
50     assert set(get_normalized_variants(proc, "Bauwegstraße")) \
51             == {'bauweg straße', 'bauweg str'}
52     assert get_normalized_variants(proc, "Bauwegstr") == ['bauweg str']
53     assert get_normalized_variants(proc, "holzweg") == ['holz weg']
54     assert get_normalized_variants(proc, "hallo") == ['hallo']
55
56
57 def test_variants_empty(cfgfile):
58     fpath = cfgfile([], ['saint => 🜵', 'street => st'])
59
60     rules = ICUNameProcessorRules(loader=ICURuleLoader(fpath))
61     proc = ICUNameProcessor(rules)
62
63     assert get_normalized_variants(proc, '🜵') == []
64     assert get_normalized_variants(proc, '🜳') == []
65     assert get_normalized_variants(proc, 'saint') == ['saint']
66
67
68 def test_multiple_replacements(cfgfile):
69     fpath = cfgfile([], ['saint => s,st', 'street => st'])
70
71     rules = ICUNameProcessorRules(loader=ICURuleLoader(fpath))
72     proc = ICUNameProcessor(rules)
73
74     assert set(get_normalized_variants(proc, "Saint Johns Street")) == \
75             {'saint johns street', 's johns street', 'st johns street',
76              'saint johns st', 's johns st', 'st johns st'}
77
78
79 def test_search_normalized(cfgfile):
80     fpath = cfgfile(['street'], ['street => s,st', 'master => mstr'])
81
82     rules = ICUNameProcessorRules(loader=ICURuleLoader(fpath))
83     proc = ICUNameProcessor(rules)
84
85     assert proc.get_search_normalized('Master Street') == 'master  street'
86     assert proc.get_search_normalized('Earnes St') == 'earne s  st'
87     assert proc.get_search_normalized('Nostreet') == 'no street'