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(*variants, **kwargs):
20 - "[[:Nonspacing Mark:] [:Cf:]] >"
22 - "[[:Punctuation:][:Space:]]+ > ' '"
28 content += "variants:\n - words:\n"
29 content += '\n'.join((" - " + s for s in variants)) + '\n'
31 content += " {}: {}\n".format(k, v)
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))
43 def test_variants_empty(cfgfile):
44 fpath = cfgfile('saint -> 🜵', 'street -> st')
46 rules = ICUNameProcessorRules(loader=ICURuleLoader(fpath))
47 proc = ICUNameProcessor(rules)
49 assert get_normalized_variants(proc, '🜵') == []
50 assert get_normalized_variants(proc, '🜳') == []
51 assert get_normalized_variants(proc, 'saint') == ['saint']
55 (('~strasse,~straße -> str', '~weg => weg'), "hallo", {'hallo'}),
56 (('weg => wg',), "holzweg", {'holzweg'}),
57 (('weg -> wg',), "holzweg", {'holzweg'}),
58 (('~weg => weg',), "holzweg", {'holz weg', 'holzweg'}),
59 (('~weg -> weg',), "holzweg", {'holz weg', 'holzweg'}),
60 (('~weg => w',), "holzweg", {'holz w', 'holzw'}),
61 (('~weg -> w',), "holzweg", {'holz weg', 'holzweg', 'holz w', 'holzw'}),
62 (('~weg => weg',), "Meier Weg", {'meier weg', 'meierweg'}),
63 (('~weg -> weg',), "Meier Weg", {'meier weg', 'meierweg'}),
64 (('~weg => w',), "Meier Weg", {'meier w', 'meierw'}),
65 (('~weg -> w',), "Meier Weg", {'meier weg', 'meierweg', 'meier w', 'meierw'}),
66 (('weg => wg',), "Meier Weg", {'meier wg'}),
67 (('weg -> wg',), "Meier Weg", {'meier weg', 'meier wg'}),
68 (('~strasse,~straße -> str', '~weg => weg'), "Bauwegstraße",
69 {'bauweg straße', 'bauweg str', 'bauwegstraße', 'bauwegstr'}),
70 (('am => a', 'bach => b'), "am bach", {'a b'}),
71 (('am => a', '~bach => b'), "am bach", {'a b'}),
72 (('am -> a', '~bach -> b'), "am bach", {'am bach', 'a bach', 'am b', 'a b'}),
73 (('am -> a', '~bach -> b'), "ambach", {'ambach', 'am bach', 'amb', 'am b'}),
74 (('saint -> s,st', 'street -> st'), "Saint Johns Street",
75 {'saint johns street', 's johns street', 'st johns street',
76 'saint johns st', 's johns st', 'st johns st'}),
77 (('river$ -> r',), "River Bend Road", {'river bend road'}),
78 (('river$ -> r',), "Bent River", {'bent river', 'bent r'}),
79 (('^north => n',), "North 2nd Street", {'n 2nd street'}),
80 (('^north => n',), "Airport North", {'airport north'}),
81 (('am -> a',), "am am am am am am am am", {'am am am am am am am am'}),
82 (('am => a',), "am am am am am am am am", {'a a a a a a a a'})
85 @pytest.mark.parametrize("rules,name,variants", VARIANT_TESTS)
86 def test_variants(cfgfile, rules, name, variants):
87 fpath = cfgfile(*rules)
88 proc = ICUNameProcessor(ICUNameProcessorRules(loader=ICURuleLoader(fpath)))
90 result = get_normalized_variants(proc, name)
92 assert len(result) == len(set(result))
93 assert set(get_normalized_variants(proc, name)) == variants
96 def test_search_normalized(cfgfile):
97 fpath = cfgfile('~street => s,st', 'master => mstr')
99 rules = ICUNameProcessorRules(loader=ICURuleLoader(fpath))
100 proc = ICUNameProcessor(rules)
102 assert proc.get_search_normalized('Master Street') == 'master street'
103 assert proc.get_search_normalized('Earnes St') == 'earnes st'
104 assert proc.get_search_normalized('Nostreet') == 'nostreet'