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))
42 def test_simple_variants(cfgfile):
43 fpath = cfgfile('~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 set(get_normalized_variants(proc, "Meier Weg")) \
56 == {'meier weg', 'meierweg'}
57 assert get_normalized_variants(proc, "hallo") == ['hallo']
60 def test_variants_empty(cfgfile):
61 fpath = cfgfile('saint -> 🜵', 'street -> st')
63 rules = ICUNameProcessorRules(loader=ICURuleLoader(fpath))
64 proc = ICUNameProcessor(rules)
66 assert get_normalized_variants(proc, '🜵') == []
67 assert get_normalized_variants(proc, '🜳') == []
68 assert get_normalized_variants(proc, 'saint') == ['saint']
71 def test_multiple_replacements(cfgfile):
72 fpath = cfgfile('saint -> s,st', 'street -> st')
74 rules = ICUNameProcessorRules(loader=ICURuleLoader(fpath))
75 proc = ICUNameProcessor(rules)
77 assert set(get_normalized_variants(proc, "Saint Johns Street")) == \
78 {'saint johns street', 's johns street', 'st johns street',
79 'saint johns st', 's johns st', 'st johns st'}
82 def test_search_normalized(cfgfile):
83 fpath = cfgfile('~street => s,st', 'master => mstr')
85 rules = ICUNameProcessorRules(loader=ICURuleLoader(fpath))
86 proc = ICUNameProcessor(rules)
88 assert proc.get_search_normalized('Master Street') == 'master street'
89 assert proc.get_search_normalized('Earnes St') == 'earnes st'
90 assert proc.get_search_normalized('Nostreet') == 'nostreet'