2 Tests for import name normalisation and variant generation.
4 from textwrap import dedent
9 from nominatim.tokenizer.icu_rule_loader import ICURuleLoader
10 from nominatim.tokenizer.icu_name_processor import ICUNameProcessor, ICUNameProcessorRules
12 from nominatim.errors import UsageError
16 def _create_config(*variants, **kwargs):
21 - "[[:Nonspacing Mark:] [:Cf:]] >"
23 - "[[:Punctuation:][:Space:]]+ > ' '"
29 content += "variants:\n - words:\n"
30 content += '\n'.join((" - " + s for s in variants)) + '\n'
32 content += " {}: {}\n".format(k, v)
33 return yaml.safe_load(content)
38 def get_normalized_variants(proc, name):
39 return proc.get_variants_ascii(proc.get_normalized(name))
42 def test_variants_empty(cfgfile):
43 fpath = cfgfile('saint -> 🜵', 'street -> st')
45 rules = ICUNameProcessorRules(loader=ICURuleLoader(fpath))
46 proc = ICUNameProcessor(rules)
48 assert get_normalized_variants(proc, '🜵') == []
49 assert get_normalized_variants(proc, '🜳') == []
50 assert get_normalized_variants(proc, 'saint') == ['saint']
54 (('~strasse,~straße -> str', '~weg => weg'), "hallo", {'hallo'}),
55 (('weg => wg',), "holzweg", {'holzweg'}),
56 (('weg -> wg',), "holzweg", {'holzweg'}),
57 (('~weg => weg',), "holzweg", {'holz weg', 'holzweg'}),
58 (('~weg -> weg',), "holzweg", {'holz weg', 'holzweg'}),
59 (('~weg => w',), "holzweg", {'holz w', 'holzw'}),
60 (('~weg -> w',), "holzweg", {'holz weg', 'holzweg', 'holz w', 'holzw'}),
61 (('~weg => weg',), "Meier Weg", {'meier weg', 'meierweg'}),
62 (('~weg -> weg',), "Meier Weg", {'meier weg', 'meierweg'}),
63 (('~weg => w',), "Meier Weg", {'meier w', 'meierw'}),
64 (('~weg -> w',), "Meier Weg", {'meier weg', 'meierweg', 'meier w', 'meierw'}),
65 (('weg => wg',), "Meier Weg", {'meier wg'}),
66 (('weg -> wg',), "Meier Weg", {'meier weg', 'meier wg'}),
67 (('~strasse,~straße -> str', '~weg => weg'), "Bauwegstraße",
68 {'bauweg straße', 'bauweg str', 'bauwegstraße', 'bauwegstr'}),
69 (('am => a', 'bach => b'), "am bach", {'a b'}),
70 (('am => a', '~bach => b'), "am bach", {'a b'}),
71 (('am -> a', '~bach -> b'), "am bach", {'am bach', 'a bach', 'am b', 'a b'}),
72 (('am -> a', '~bach -> b'), "ambach", {'ambach', 'am bach', 'amb', 'am b'}),
73 (('saint -> s,st', 'street -> st'), "Saint Johns Street",
74 {'saint johns street', 's johns street', 'st johns street',
75 'saint johns st', 's johns st', 'st johns st'}),
76 (('river$ -> r',), "River Bend Road", {'river bend road'}),
77 (('river$ -> r',), "Bent River", {'bent river', 'bent r'}),
78 (('^north => n',), "North 2nd Street", {'n 2nd street'}),
79 (('^north => n',), "Airport North", {'airport north'}),
80 (('am -> a',), "am am am am am am am am", {'am am am am am am am am'}),
81 (('am => a',), "am am am am am am am am", {'a a a a a a a a'})
84 @pytest.mark.parametrize("rules,name,variants", VARIANT_TESTS)
85 def test_variants(cfgfile, rules, name, variants):
86 fpath = cfgfile(*rules)
87 proc = ICUNameProcessor(ICUNameProcessorRules(loader=ICURuleLoader(fpath)))
89 result = get_normalized_variants(proc, name)
91 assert len(result) == len(set(result))
92 assert set(get_normalized_variants(proc, name)) == variants
95 def test_search_normalized(cfgfile):
96 fpath = cfgfile('~street => s,st', 'master => mstr')
98 rules = ICUNameProcessorRules(loader=ICURuleLoader(fpath))
99 proc = ICUNameProcessor(rules)
101 assert proc.get_search_normalized('Master Street') == 'master street'
102 assert proc.get_search_normalized('Earnes St') == 'earnes st'
103 assert proc.get_search_normalized('Nostreet') == 'nostreet'