]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_tokenizer_icu_name_processor.py
switch to a more flexible variant description format
[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 def test_simple_variants(cfgfile):
43     fpath = cfgfile('~strasse,~straße -> str',
44                     '~weg => weg',
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', 'bauwegstraße', 'bauwegstr'}
52     assert get_normalized_variants(proc, "Bauwegstr") == ['bauwegstr']
53     assert set(get_normalized_variants(proc, "holzweg")) \
54             == {'holz weg', 'holzweg'}
55     assert set(get_normalized_variants(proc, "Meier Weg")) \
56             == {'meier weg', 'meierweg'}
57     assert get_normalized_variants(proc, "hallo") == ['hallo']
58
59
60 def test_variants_empty(cfgfile):
61     fpath = cfgfile('saint -> 🜵', 'street -> st')
62
63     rules = ICUNameProcessorRules(loader=ICURuleLoader(fpath))
64     proc = ICUNameProcessor(rules)
65
66     assert get_normalized_variants(proc, '🜵') == []
67     assert get_normalized_variants(proc, '🜳') == []
68     assert get_normalized_variants(proc, 'saint') == ['saint']
69
70
71 def test_multiple_replacements(cfgfile):
72     fpath = cfgfile('saint -> s,st', 'street -> st')
73
74     rules = ICUNameProcessorRules(loader=ICURuleLoader(fpath))
75     proc = ICUNameProcessor(rules)
76
77     assert set(get_normalized_variants(proc, "Saint Johns Street")) == \
78             {'saint johns street', 's johns street', 'st johns street',
79              'saint johns st', 's johns st', 'st johns st'}
80
81
82 def test_search_normalized(cfgfile):
83     fpath = cfgfile('~street => s,st', 'master => mstr')
84
85     rules = ICUNameProcessorRules(loader=ICURuleLoader(fpath))
86     proc = ICUNameProcessor(rules)
87
88     assert proc.get_search_normalized('Master Street') == 'master street'
89     assert proc.get_search_normalized('Earnes St') == 'earnes st'
90     assert proc.get_search_normalized('Nostreet') == 'nostreet'