]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_tokenizer_icu_name_processor.py
adapt 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             - "[[:Nonspacing Mark:] [:Cf:]] >"
20             - ":: lower ()"
21             - "[[:Punctuation:][:Space:]]+ > ' '"
22             - ":: NFC ()"
23         transliteration:
24             - "::  Latin ()"
25         """)
26         content += "compound_suffixes:\n"
27         content += '\n'.join(("    - " + s for s in suffixes)) + '\n'
28         content += "abbreviations:\n"
29         content += '\n'.join(("    - " + s for s in abbr)) + '\n'
30         fpath = tmp_path / ('test_config' + suffix)
31         fpath.write_text(dedent(content))
32         return fpath
33
34     return _create_config
35
36
37 def get_normalized_variants(proc, name):
38     return proc.get_variants_ascii(proc.get_normalized(name))
39
40 def test_simple_variants(cfgfile):
41     fpath = cfgfile(['strasse', 'straße', 'weg'],
42                     ['strasse,straße => str',
43                      'prospekt => pr'])
44
45     rules = ICUNameProcessorRules(loader=ICURuleLoader(fpath))
46     proc = ICUNameProcessor(rules)
47
48     assert set(get_normalized_variants(proc, "Bauwegstraße")) \
49             == {'bauweg straße', 'bauweg str'}
50     assert get_normalized_variants(proc, "Bauwegstr") == ['bauweg str']
51     assert get_normalized_variants(proc, "holzweg") == ['holz weg']
52     assert get_normalized_variants(proc, "hallo") == ['hallo']
53
54
55 def test_multiple_replacements(cfgfile):
56     fpath = cfgfile([], ['saint => s,st', 'street => st'])
57
58     rules = ICUNameProcessorRules(loader=ICURuleLoader(fpath))
59     proc = ICUNameProcessor(rules)
60
61     assert set(get_normalized_variants(proc, "Saint Johns Street")) == \
62             {'saint johns street', 's johns street', 'st johns street',
63              'saint johns st', 's johns st', 'st johns st'}
64
65
66 def test_search_normalized(cfgfile):
67     fpath = cfgfile(['street'], ['street => s,st', 'master => mstr'])
68
69     rules = ICUNameProcessorRules(loader=ICURuleLoader(fpath))
70     proc = ICUNameProcessor(rules)
71
72     assert proc.get_search_normalized('Master Street') == 'master  street'
73     assert proc.get_search_normalized('Earnes St') == 'earne s  st'
74     assert proc.get_search_normalized('Nostreet') == 'no street'