2 Tests for import name normalisation and variant generation.
4 from textwrap import dedent
8 from nominatim.tokenizer.icu_rule_loader import ICURuleLoader
10 from nominatim.errors import UsageError
13 def cfgfile(def_config, tmp_path):
14 project_dir = tmp_path / 'project_dir'
16 def_config.project_dir = project_dir
18 def _create_config(*variants, **kwargs):
23 - "[[:Nonspacing Mark:] [:Cf:]] >"
25 - "[[:Punctuation:][:Space:]]+ > ' '"
31 content += "token-analysis:\n - analyzer: generic\n variants:\n - words:\n"
32 content += '\n'.join((" - " + s for s in variants)) + '\n'
34 content += " {}: {}\n".format(k, v)
35 (project_dir / 'icu_tokenizer.yaml').write_text(content)
42 def get_normalized_variants(proc, name):
43 return proc.get_variants_ascii(proc.get_normalized(name))
46 def test_variants_empty(cfgfile):
47 config = cfgfile('saint -> 🜵', 'street -> st')
49 proc = ICURuleLoader(config).make_token_analysis()
51 assert get_normalized_variants(proc, '🜵') == []
52 assert get_normalized_variants(proc, '🜳') == []
53 assert get_normalized_variants(proc, 'saint') == ['saint']
57 (('~strasse,~straße -> str', '~weg => weg'), "hallo", {'hallo'}),
58 (('weg => wg',), "holzweg", {'holzweg'}),
59 (('weg -> wg',), "holzweg", {'holzweg'}),
60 (('~weg => weg',), "holzweg", {'holz weg', 'holzweg'}),
61 (('~weg -> weg',), "holzweg", {'holz weg', 'holzweg'}),
62 (('~weg => w',), "holzweg", {'holz w', 'holzw'}),
63 (('~weg -> w',), "holzweg", {'holz weg', 'holzweg', 'holz w', 'holzw'}),
64 (('~weg => weg',), "Meier Weg", {'meier weg', 'meierweg'}),
65 (('~weg -> weg',), "Meier Weg", {'meier weg', 'meierweg'}),
66 (('~weg => w',), "Meier Weg", {'meier w', 'meierw'}),
67 (('~weg -> w',), "Meier Weg", {'meier weg', 'meierweg', 'meier w', 'meierw'}),
68 (('weg => wg',), "Meier Weg", {'meier wg'}),
69 (('weg -> wg',), "Meier Weg", {'meier weg', 'meier wg'}),
70 (('~strasse,~straße -> str', '~weg => weg'), "Bauwegstraße",
71 {'bauweg straße', 'bauweg str', 'bauwegstraße', 'bauwegstr'}),
72 (('am => a', 'bach => b'), "am bach", {'a b'}),
73 (('am => a', '~bach => b'), "am bach", {'a b'}),
74 (('am -> a', '~bach -> b'), "am bach", {'am bach', 'a bach', 'am b', 'a b'}),
75 (('am -> a', '~bach -> b'), "ambach", {'ambach', 'am bach', 'amb', 'am b'}),
76 (('saint -> s,st', 'street -> st'), "Saint Johns Street",
77 {'saint johns street', 's johns street', 'st johns street',
78 'saint johns st', 's johns st', 'st johns st'}),
79 (('river$ -> r',), "River Bend Road", {'river bend road'}),
80 (('river$ -> r',), "Bent River", {'bent river', 'bent r'}),
81 (('^north => n',), "North 2nd Street", {'n 2nd street'}),
82 (('^north => n',), "Airport North", {'airport north'}),
83 (('am -> a',), "am am am am am am am am", {'am am am am am am am am'}),
84 (('am => a',), "am am am am am am am am", {'a a a a a a a a'})
87 @pytest.mark.parametrize("rules,name,variants", VARIANT_TESTS)
88 def test_variants(cfgfile, rules, name, variants):
89 config = cfgfile(*rules)
90 proc = ICURuleLoader(config).make_token_analysis()
92 result = get_normalized_variants(proc, name)
94 assert len(result) == len(set(result))
95 assert set(get_normalized_variants(proc, name)) == variants
98 def test_search_normalized(cfgfile):
99 config = cfgfile('~street => s,st', 'master => mstr')
100 proc = ICURuleLoader(config).make_token_analysis()
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'