2 Tests for import name normalisation and variant generation.
4 from textwrap import dedent
8 from nominatim.tokenizer.icu_rule_loader import ICURuleLoader
9 from nominatim.tokenizer.icu_name_processor import ICUNameProcessor, ICUNameProcessorRules
11 from nominatim.errors import UsageError
14 def cfgfile(tmp_path, suffix='.yaml'):
15 def _create_config(suffixes, abbr):
20 - "[[:Nonspacing Mark:] [:Cf:]] >"
22 - "[[:Punctuation:][:Space:]]+ > ' '"
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))
39 def get_normalized_variants(proc, name):
40 return proc.get_variants_ascii(proc.get_normalized(name))
42 def test_simple_variants(cfgfile):
43 fpath = cfgfile(['strasse', 'straße', 'weg'],
44 ['strasse,straße => str',
47 rules = ICUNameProcessorRules(loader=ICURuleLoader(fpath))
48 proc = ICUNameProcessor(rules)
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']
57 def test_variants_empty(cfgfile):
58 fpath = cfgfile([], ['saint => 🜵', 'street => st'])
60 rules = ICUNameProcessorRules(loader=ICURuleLoader(fpath))
61 proc = ICUNameProcessor(rules)
63 assert get_normalized_variants(proc, '🜵') == []
64 assert get_normalized_variants(proc, '🜳') == []
65 assert get_normalized_variants(proc, 'saint') == ['saint']
68 def test_multiple_replacements(cfgfile):
69 fpath = cfgfile([], ['saint => s,st', 'street => st'])
71 rules = ICUNameProcessorRules(loader=ICURuleLoader(fpath))
72 proc = ICUNameProcessor(rules)
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'}
79 def test_search_normalized(cfgfile):
80 fpath = cfgfile(['street'], ['street => s,st', 'master => mstr'])
82 rules = ICUNameProcessorRules(loader=ICURuleLoader(fpath))
83 proc = ICUNameProcessor(rules)
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'