mirror of
https://github.com/osm-search/Nominatim.git
synced 2026-02-16 15:47:58 +00:00
reorganize and complete tests around generic token analysis
This commit is contained in:
@@ -76,7 +76,7 @@ class _VariantMaker:
|
|||||||
|
|
||||||
decompose = parts[1] is None
|
decompose = parts[1] is None
|
||||||
src_terms = [self._parse_variant_word(t) for t in parts[0].split(',')]
|
src_terms = [self._parse_variant_word(t) for t in parts[0].split(',')]
|
||||||
repl_terms = (self.norm.transliterate(t.strip()) for t in parts[3].split(','))
|
repl_terms = (self.norm.transliterate(t).strip() for t in parts[3].split(','))
|
||||||
|
|
||||||
# If the source should be kept, add a 1:1 replacement
|
# If the source should be kept, add a 1:1 replacement
|
||||||
if parts[2] == '-':
|
if parts[2] == '-':
|
||||||
@@ -96,7 +96,7 @@ class _VariantMaker:
|
|||||||
match = re.fullmatch(r'([~^]?)([^~$^]*)([~$]?)', name)
|
match = re.fullmatch(r'([~^]?)([^~$^]*)([~$]?)', name)
|
||||||
if match is None or (match.group(1) == '~' and match.group(3) == '~'):
|
if match is None or (match.group(1) == '~' and match.group(3) == '~'):
|
||||||
raise UsageError("Invalid variant word descriptor '{}'".format(name))
|
raise UsageError("Invalid variant word descriptor '{}'".format(name))
|
||||||
norm_name = self.norm.transliterate(match.group(2))
|
norm_name = self.norm.transliterate(match.group(2)).strip()
|
||||||
if not norm_name:
|
if not norm_name:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@@ -142,10 +142,10 @@ def _create_variants(src, preflag, postflag, repl, decompose):
|
|||||||
|
|
||||||
### Analysis section
|
### Analysis section
|
||||||
|
|
||||||
def create(trans_rules, config):
|
def create(transliterator, config):
|
||||||
""" Create a new token analysis instance for this module.
|
""" Create a new token analysis instance for this module.
|
||||||
"""
|
"""
|
||||||
return GenericTokenAnalysis(trans_rules, config)
|
return GenericTokenAnalysis(transliterator, config)
|
||||||
|
|
||||||
|
|
||||||
class GenericTokenAnalysis:
|
class GenericTokenAnalysis:
|
||||||
|
|||||||
@@ -122,6 +122,15 @@ def test_transliteration_rules_from_file(test_config):
|
|||||||
assert trans.transliterate(" axxt ") == " byt "
|
assert trans.transliterate(" axxt ") == " byt "
|
||||||
|
|
||||||
|
|
||||||
|
def test_search_rules(cfgrules):
|
||||||
|
config = cfgrules('~street => s,st', 'master => mstr')
|
||||||
|
proc = ICURuleLoader(config).make_token_analysis()
|
||||||
|
|
||||||
|
assert proc.search.transliterate('Master Street').strip() == 'master street'
|
||||||
|
assert proc.search.transliterate('Earnes St').strip() == 'earnes st'
|
||||||
|
assert proc.search.transliterate('Nostreet').strip() == 'nostreet'
|
||||||
|
|
||||||
|
|
||||||
class TestGetReplacements:
|
class TestGetReplacements:
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
|
|||||||
@@ -1,52 +1,52 @@
|
|||||||
"""
|
"""
|
||||||
Tests for import name normalisation and variant generation.
|
Tests for import name normalisation and variant generation.
|
||||||
"""
|
"""
|
||||||
from textwrap import dedent
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from nominatim.tokenizer.icu_rule_loader import ICURuleLoader
|
from icu import Transliterator
|
||||||
|
|
||||||
|
import nominatim.tokenizer.token_analysis.generic as module
|
||||||
from nominatim.errors import UsageError
|
from nominatim.errors import UsageError
|
||||||
|
|
||||||
@pytest.fixture
|
DEFAULT_NORMALIZATION = """ :: NFD ();
|
||||||
def cfgfile(def_config, tmp_path):
|
'🜳' > ' ';
|
||||||
project_dir = tmp_path / 'project_dir'
|
[[:Nonspacing Mark:] [:Cf:]] >;
|
||||||
project_dir.mkdir()
|
:: lower ();
|
||||||
def_config.project_dir = project_dir
|
[[:Punctuation:][:Space:]]+ > ' ';
|
||||||
|
:: NFC ();
|
||||||
|
"""
|
||||||
|
|
||||||
def _create_config(*variants, **kwargs):
|
DEFAULT_TRANSLITERATION = """ :: Latin ();
|
||||||
content = dedent("""\
|
'🜵' > ' ';
|
||||||
normalization:
|
"""
|
||||||
- ":: NFD ()"
|
|
||||||
- "'🜳' > ' '"
|
|
||||||
- "[[:Nonspacing Mark:] [:Cf:]] >"
|
|
||||||
- ":: lower ()"
|
|
||||||
- "[[:Punctuation:][:Space:]]+ > ' '"
|
|
||||||
- ":: NFC ()"
|
|
||||||
transliteration:
|
|
||||||
- ":: Latin ()"
|
|
||||||
- "'🜵' > ' '"
|
|
||||||
""")
|
|
||||||
content += "token-analysis:\n - analyzer: generic\n variants:\n - words:\n"
|
|
||||||
content += '\n'.join((" - " + s for s in variants)) + '\n'
|
|
||||||
for k, v in kwargs:
|
|
||||||
content += " {}: {}\n".format(k, v)
|
|
||||||
(project_dir / 'icu_tokenizer.yaml').write_text(content)
|
|
||||||
|
|
||||||
return def_config
|
def make_analyser(*variants, variant_only=False):
|
||||||
|
rules = { 'analyzer': 'generic', 'variants': [{'words': variants}]}
|
||||||
|
if variant_only:
|
||||||
|
rules['mode'] = 'variant-only'
|
||||||
|
config = module.configure(rules, DEFAULT_NORMALIZATION)
|
||||||
|
trans = Transliterator.createFromRules("test_trans", DEFAULT_TRANSLITERATION)
|
||||||
|
|
||||||
return _create_config
|
return module.create(trans, config)
|
||||||
|
|
||||||
|
|
||||||
def get_normalized_variants(proc, name):
|
def get_normalized_variants(proc, name):
|
||||||
return proc.analysis[None].get_variants_ascii(proc.normalizer.transliterate(name).strip())
|
norm = Transliterator.createFromRules("test_norm", DEFAULT_NORMALIZATION)
|
||||||
|
return proc.get_variants_ascii(norm.transliterate(name).strip())
|
||||||
|
|
||||||
|
|
||||||
def test_variants_empty(cfgfile):
|
def test_no_variants():
|
||||||
config = cfgfile('saint -> 🜵', 'street -> st')
|
rules = { 'analyzer': 'generic' }
|
||||||
|
config = module.configure(rules, DEFAULT_NORMALIZATION)
|
||||||
|
trans = Transliterator.createFromRules("test_trans", DEFAULT_TRANSLITERATION)
|
||||||
|
|
||||||
proc = ICURuleLoader(config).make_token_analysis()
|
proc = module.create(trans, config)
|
||||||
|
|
||||||
|
assert get_normalized_variants(proc, '大德!') == ['dà dé']
|
||||||
|
|
||||||
|
|
||||||
|
def test_variants_empty():
|
||||||
|
proc = make_analyser('saint -> 🜵', 'street -> st')
|
||||||
|
|
||||||
assert get_normalized_variants(proc, '🜵') == []
|
assert get_normalized_variants(proc, '🜵') == []
|
||||||
assert get_normalized_variants(proc, '🜳') == []
|
assert get_normalized_variants(proc, '🜳') == []
|
||||||
@@ -85,9 +85,8 @@ VARIANT_TESTS = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
@pytest.mark.parametrize("rules,name,variants", VARIANT_TESTS)
|
@pytest.mark.parametrize("rules,name,variants", VARIANT_TESTS)
|
||||||
def test_variants(cfgfile, rules, name, variants):
|
def test_variants(rules, name, variants):
|
||||||
config = cfgfile(*rules)
|
proc = make_analyser(*rules)
|
||||||
proc = ICURuleLoader(config).make_token_analysis()
|
|
||||||
|
|
||||||
result = get_normalized_variants(proc, name)
|
result = get_normalized_variants(proc, name)
|
||||||
|
|
||||||
@@ -95,10 +94,172 @@ def test_variants(cfgfile, rules, name, variants):
|
|||||||
assert set(get_normalized_variants(proc, name)) == variants
|
assert set(get_normalized_variants(proc, name)) == variants
|
||||||
|
|
||||||
|
|
||||||
def test_search_normalized(cfgfile):
|
VARIANT_ONLY_TESTS = [
|
||||||
config = cfgfile('~street => s,st', 'master => mstr')
|
(('weg => wg',), "hallo", set()),
|
||||||
proc = ICURuleLoader(config).make_token_analysis()
|
(('weg => wg',), "Meier Weg", {'meier wg'}),
|
||||||
|
(('weg -> wg',), "Meier Weg", {'meier wg'}),
|
||||||
|
]
|
||||||
|
|
||||||
assert proc.search.transliterate('Master Street').strip() == 'master street'
|
@pytest.mark.parametrize("rules,name,variants", VARIANT_ONLY_TESTS)
|
||||||
assert proc.search.transliterate('Earnes St').strip() == 'earnes st'
|
def test_variants_only(rules, name, variants):
|
||||||
assert proc.search.transliterate('Nostreet').strip() == 'nostreet'
|
proc = make_analyser(*rules, variant_only=True)
|
||||||
|
|
||||||
|
result = get_normalized_variants(proc, name)
|
||||||
|
|
||||||
|
assert len(result) == len(set(result))
|
||||||
|
assert set(get_normalized_variants(proc, name)) == variants
|
||||||
|
|
||||||
|
|
||||||
|
class TestGetReplacements:
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def configure_rules(*variants):
|
||||||
|
rules = { 'analyzer': 'generic', 'variants': [{'words': variants}]}
|
||||||
|
return module.configure(rules, DEFAULT_NORMALIZATION)
|
||||||
|
|
||||||
|
|
||||||
|
def get_replacements(self, *variants):
|
||||||
|
config = self.configure_rules(*variants)
|
||||||
|
|
||||||
|
return sorted((k, sorted(v)) for k,v in config['replacements'])
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("variant", ['foo > bar', 'foo -> bar -> bar',
|
||||||
|
'~foo~ -> bar', 'fo~ o -> bar'])
|
||||||
|
def test_invalid_variant_description(self, variant):
|
||||||
|
with pytest.raises(UsageError):
|
||||||
|
self.configure_rules(variant)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("rule", ["!!! -> bar", "bar => !!!"])
|
||||||
|
def test_ignore_unnormalizable_terms(self, rule):
|
||||||
|
repl = self.get_replacements(rule)
|
||||||
|
|
||||||
|
assert repl == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_add_full(self):
|
||||||
|
repl = self.get_replacements("foo -> bar")
|
||||||
|
|
||||||
|
assert repl == [(' foo ', [' bar', ' foo'])]
|
||||||
|
|
||||||
|
|
||||||
|
def test_replace_full(self):
|
||||||
|
repl = self.get_replacements("foo => bar")
|
||||||
|
|
||||||
|
assert repl == [(' foo ', [' bar'])]
|
||||||
|
|
||||||
|
|
||||||
|
def test_add_suffix_no_decompose(self):
|
||||||
|
repl = self.get_replacements("~berg |-> bg")
|
||||||
|
|
||||||
|
assert repl == [(' berg ', [' berg', ' bg']),
|
||||||
|
('berg ', ['berg', 'bg'])]
|
||||||
|
|
||||||
|
|
||||||
|
def test_replace_suffix_no_decompose(self):
|
||||||
|
repl = self.get_replacements("~berg |=> bg")
|
||||||
|
|
||||||
|
assert repl == [(' berg ', [' bg']),('berg ', ['bg'])]
|
||||||
|
|
||||||
|
|
||||||
|
def test_add_suffix_decompose(self):
|
||||||
|
repl = self.get_replacements("~berg -> bg")
|
||||||
|
|
||||||
|
assert repl == [(' berg ', [' berg', ' bg', 'berg', 'bg']),
|
||||||
|
('berg ', [' berg', ' bg', 'berg', 'bg'])]
|
||||||
|
|
||||||
|
|
||||||
|
def test_replace_suffix_decompose(self):
|
||||||
|
repl = self.get_replacements("~berg => bg")
|
||||||
|
|
||||||
|
assert repl == [(' berg ', [' bg', 'bg']),
|
||||||
|
('berg ', [' bg', 'bg'])]
|
||||||
|
|
||||||
|
|
||||||
|
def test_add_prefix_no_compose(self):
|
||||||
|
repl = self.get_replacements("hinter~ |-> hnt")
|
||||||
|
|
||||||
|
assert repl == [(' hinter', [' hinter', ' hnt']),
|
||||||
|
(' hinter ', [' hinter', ' hnt'])]
|
||||||
|
|
||||||
|
|
||||||
|
def test_replace_prefix_no_compose(self):
|
||||||
|
repl = self.get_replacements("hinter~ |=> hnt")
|
||||||
|
|
||||||
|
assert repl == [(' hinter', [' hnt']), (' hinter ', [' hnt'])]
|
||||||
|
|
||||||
|
|
||||||
|
def test_add_prefix_compose(self):
|
||||||
|
repl = self.get_replacements("hinter~-> h")
|
||||||
|
|
||||||
|
assert repl == [(' hinter', [' h', ' h ', ' hinter', ' hinter ']),
|
||||||
|
(' hinter ', [' h', ' h', ' hinter', ' hinter'])]
|
||||||
|
|
||||||
|
|
||||||
|
def test_replace_prefix_compose(self):
|
||||||
|
repl = self.get_replacements("hinter~=> h")
|
||||||
|
|
||||||
|
assert repl == [(' hinter', [' h', ' h ']),
|
||||||
|
(' hinter ', [' h', ' h'])]
|
||||||
|
|
||||||
|
|
||||||
|
def test_add_beginning_only(self):
|
||||||
|
repl = self.get_replacements("^Premier -> Pr")
|
||||||
|
|
||||||
|
assert repl == [('^ premier ', ['^ pr', '^ premier'])]
|
||||||
|
|
||||||
|
|
||||||
|
def test_replace_beginning_only(self):
|
||||||
|
repl = self.get_replacements("^Premier => Pr")
|
||||||
|
|
||||||
|
assert repl == [('^ premier ', ['^ pr'])]
|
||||||
|
|
||||||
|
|
||||||
|
def test_add_final_only(self):
|
||||||
|
repl = self.get_replacements("road$ -> rd")
|
||||||
|
|
||||||
|
assert repl == [(' road ^', [' rd ^', ' road ^'])]
|
||||||
|
|
||||||
|
|
||||||
|
def test_replace_final_only(self):
|
||||||
|
repl = self.get_replacements("road$ => rd")
|
||||||
|
|
||||||
|
assert repl == [(' road ^', [' rd ^'])]
|
||||||
|
|
||||||
|
|
||||||
|
def test_decompose_only(self):
|
||||||
|
repl = self.get_replacements("~foo -> foo")
|
||||||
|
|
||||||
|
assert repl == [(' foo ', [' foo', 'foo']),
|
||||||
|
('foo ', [' foo', 'foo'])]
|
||||||
|
|
||||||
|
|
||||||
|
def test_add_suffix_decompose_end_only(self):
|
||||||
|
repl = self.get_replacements("~berg |-> bg", "~berg$ -> bg")
|
||||||
|
|
||||||
|
assert repl == [(' berg ', [' berg', ' bg']),
|
||||||
|
(' berg ^', [' berg ^', ' bg ^', 'berg ^', 'bg ^']),
|
||||||
|
('berg ', ['berg', 'bg']),
|
||||||
|
('berg ^', [' berg ^', ' bg ^', 'berg ^', 'bg ^'])]
|
||||||
|
|
||||||
|
|
||||||
|
def test_replace_suffix_decompose_end_only(self):
|
||||||
|
repl = self.get_replacements("~berg |=> bg", "~berg$ => bg")
|
||||||
|
|
||||||
|
assert repl == [(' berg ', [' bg']),
|
||||||
|
(' berg ^', [' bg ^', 'bg ^']),
|
||||||
|
('berg ', ['bg']),
|
||||||
|
('berg ^', [' bg ^', 'bg ^'])]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('rule', ["~berg,~burg -> bg",
|
||||||
|
"~berg, ~burg -> bg",
|
||||||
|
"~berg,,~burg -> bg"])
|
||||||
|
def test_add_multiple_suffix(self, rule):
|
||||||
|
repl = self.get_replacements(rule)
|
||||||
|
|
||||||
|
assert repl == [(' berg ', [' berg', ' bg', 'berg', 'bg']),
|
||||||
|
(' burg ', [' bg', ' burg', 'bg', 'burg']),
|
||||||
|
('berg ', [' berg', ' bg', 'berg', 'bg']),
|
||||||
|
('burg ', [' bg', ' burg', 'bg', 'burg'])]
|
||||||
|
|||||||
Reference in New Issue
Block a user