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', '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 get_normalized_variants(proc, "hallo") == ['hallo']
58 def test_variants_empty(cfgfile):
59 fpath = cfgfile([], ['saint => 🜵', 'street => st'])
61 rules = ICUNameProcessorRules(loader=ICURuleLoader(fpath))
62 proc = ICUNameProcessor(rules)
64 assert get_normalized_variants(proc, '🜵') == []
65 assert get_normalized_variants(proc, '🜳') == []
66 assert get_normalized_variants(proc, 'saint') == ['saint']
69 def test_multiple_replacements(cfgfile):
70 fpath = cfgfile([], ['saint => s,st', 'street => st'])
72 rules = ICUNameProcessorRules(loader=ICURuleLoader(fpath))
73 proc = ICUNameProcessor(rules)
75 assert set(get_normalized_variants(proc, "Saint Johns Street")) == \
76 {'saint johns street', 's johns street', 'st johns street',
77 'saint johns st', 's johns st', 'st johns st'}
80 def test_search_normalized(cfgfile):
81 fpath = cfgfile(['street'], ['street => s,st', 'master => mstr'])
83 rules = ICUNameProcessorRules(loader=ICURuleLoader(fpath))
84 proc = ICUNameProcessor(rules)
86 assert proc.get_search_normalized('Master Street') == 'master street'
87 assert proc.get_search_normalized('Earnes St') == 'earnes st'
88 assert proc.get_search_normalized('Nostreet') == 'nostreet'