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):
19 - "[[:Nonspacing Mark:] [:Cf:]] >"
21 - "[[:Punctuation:][:Space:]]+ > ' '"
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))
37 def get_normalized_variants(proc, name):
38 return proc.get_variants_ascii(proc.get_normalized(name))
40 def test_simple_variants(cfgfile):
41 fpath = cfgfile(['strasse', 'straße', 'weg'],
42 ['strasse,straße => str',
45 rules = ICUNameProcessorRules(loader=ICURuleLoader(fpath))
46 proc = ICUNameProcessor(rules)
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']
55 def test_multiple_replacements(cfgfile):
56 fpath = cfgfile([], ['saint => s,st', 'street => st'])
58 rules = ICUNameProcessorRules(loader=ICURuleLoader(fpath))
59 proc = ICUNameProcessor(rules)
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'}
66 def test_search_normalized(cfgfile):
67 fpath = cfgfile(['street'], ['street => s,st', 'master => mstr'])
69 rules = ICUNameProcessorRules(loader=ICURuleLoader(fpath))
70 proc = ICUNameProcessor(rules)
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'