From 9ba20194706ad9e6e6488ca9afb3c67f22025de3 Mon Sep 17 00:00:00 2001 From: Sarah Hoffmann Date: Tue, 5 Oct 2021 10:20:08 +0200 Subject: [PATCH] precompute replacements while loading configuration --- nominatim/tokenizer/token_analysis/generic.py | 42 +++++----- test/python/test_tokenizer_icu_rule_loader.py | 81 +++++++++---------- 2 files changed, 56 insertions(+), 67 deletions(-) diff --git a/nominatim/tokenizer/token_analysis/generic.py b/nominatim/tokenizer/token_analysis/generic.py index f0de0cca..343534ee 100644 --- a/nominatim/tokenizer/token_analysis/generic.py +++ b/nominatim/tokenizer/token_analysis/generic.py @@ -17,14 +17,12 @@ import nominatim.tokenizer.icu_variants as variants def configure(rules, normalization_rules): """ Extract and preprocess the configuration for this module. """ - return {'variants': _parse_variant_list(rules.get('variants'), - normalization_rules)} - - -def _parse_variant_list(rules, normalization_rules): - vset = set() + rules = rules.get('variants') + immediate = defaultdict(list) + chars = set() if rules: + vset = set() rules = flatten_config_list(rules, 'variants') vmaker = _VariantMaker(normalization_rules) @@ -44,7 +42,17 @@ def _parse_variant_list(rules, normalization_rules): for rule in (section.get('words') or []): vset.update(vmaker.compute(rule, props)) - return vset + # Intermediate reorder by source. Also compute required character set. + for variant in vset: + if variant.source[-1] == ' ' and variant.replacement[-1] == ' ': + replstr = variant.replacement[:-1] + else: + replstr = variant.replacement + immediate[variant.source].append(replstr) + chars.update(variant.source) + + return {'replacements': list(immediate.items()), + 'chars': ''.join(chars)} class _VariantMaker: @@ -136,7 +144,7 @@ def _create_variants(src, preflag, postflag, repl, decompose): def create(norm_rules, trans_rules, config): """ Create a new token analysis instance for this module. """ - return GenericTokenAnalysis(norm_rules, trans_rules, config['variants']) + return GenericTokenAnalysis(norm_rules, trans_rules, config) class GenericTokenAnalysis: @@ -144,7 +152,7 @@ class GenericTokenAnalysis: and provides the functions to apply the transformations. """ - def __init__(self, norm_rules, trans_rules, replacements): + def __init__(self, norm_rules, trans_rules, config): self.normalizer = Transliterator.createFromRules("icu_normalization", norm_rules) self.to_ascii = Transliterator.createFromRules("icu_to_ascii", @@ -153,19 +161,9 @@ class GenericTokenAnalysis: self.search = Transliterator.createFromRules("icu_search", norm_rules + trans_rules) - # Intermediate reorder by source. Also compute required character set. - immediate = defaultdict(list) - chars = set() - for variant in replacements: - if variant.source[-1] == ' ' and variant.replacement[-1] == ' ': - replstr = variant.replacement[:-1] - else: - replstr = variant.replacement - immediate[variant.source].append(replstr) - chars.update(variant.source) - # Then copy to datrie - self.replacements = datrie.Trie(''.join(chars)) - for src, repllist in immediate.items(): + # Set up datrie + self.replacements = datrie.Trie(config['chars']) + for src, repllist in config['replacements']: self.replacements[src] = repllist diff --git a/test/python/test_tokenizer_icu_rule_loader.py b/test/python/test_tokenizer_icu_rule_loader.py index b76eee75..4671e0fe 100644 --- a/test/python/test_tokenizer_icu_rule_loader.py +++ b/test/python/test_tokenizer_icu_rule_loader.py @@ -130,9 +130,9 @@ class TestGetReplacements: def get_replacements(self, *variants): loader = ICURuleLoader(self.cfgrules(*variants)) - rules = loader.analysis[None].config['variants'] + rules = loader.analysis[None].config['replacements'] - return set((v.source, v.replacement) for v in rules) + return sorted((k, sorted(v)) for k,v in rules) @pytest.mark.parametrize("variant", ['foo > bar', 'foo -> bar -> bar', @@ -144,131 +144,122 @@ class TestGetReplacements: def test_add_full(self): repl = self.get_replacements("foo -> bar") - assert repl == {(' foo ', ' bar '), (' foo ', ' foo ')} + assert repl == [(' foo ', [' bar', ' foo'])] def test_replace_full(self): repl = self.get_replacements("foo => bar") - assert repl == {(' foo ', ' bar ')} + assert repl == [(' foo ', [' bar'])] def test_add_suffix_no_decompose(self): repl = self.get_replacements("~berg |-> bg") - assert repl == {('berg ', 'berg '), ('berg ', 'bg '), - (' berg ', ' berg '), (' 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 ')} + assert repl == [(' berg ', [' bg']),('berg ', ['bg'])] def test_add_suffix_decompose(self): repl = self.get_replacements("~berg -> bg") - assert repl == {('berg ', 'berg '), ('berg ', ' berg '), - (' berg ', ' berg '), (' berg ', 'berg '), - ('berg ', 'bg '), ('berg ', ' bg '), - (' berg ', 'bg '), (' 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 '), ('berg ', ' bg '), - (' berg ', 'bg '), (' 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'), (' hinter ', ' hinter '), - (' hinter', ' hnt'), (' 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 ')} + assert repl == [(' hinter', [' hnt']), (' hinter ', [' hnt'])] def test_add_prefix_compose(self): repl = self.get_replacements("hinter~-> h") - assert repl == {(' hinter', ' hinter'), (' hinter', ' hinter '), - (' hinter', ' h'), (' hinter', ' h '), - (' hinter ', ' hinter '), (' hinter ', ' hinter'), - (' hinter ', ' h '), (' 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'), (' hinter', ' h '), - (' hinter ', ' h '), (' 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 ', '^ premier '), ('^ premier ', '^ pr ')} + assert repl == [('^ premier ', ['^ pr', '^ premier'])] def test_replace_beginning_only(self): repl = self.get_replacements("^Premier => Pr") - assert repl == {('^ premier ', '^ pr ')} + assert repl == [('^ premier ', ['^ pr'])] def test_add_final_only(self): repl = self.get_replacements("road$ -> rd") - assert repl == {(' road ^', ' road ^'), (' road ^', ' rd ^')} + assert repl == [(' road ^', [' rd ^', ' road ^'])] def test_replace_final_only(self): repl = self.get_replacements("road$ => rd") - assert repl == {(' 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 '), (' 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 '), ('berg ', 'bg '), - (' berg ', ' berg '), (' berg ', ' bg '), - ('berg ^', 'berg ^'), ('berg ^', ' berg ^'), - ('berg ^', 'bg ^'), ('berg ^', ' bg ^'), - (' berg ^', 'berg ^'), (' berg ^', 'bg ^'), - (' berg ^', ' berg ^'), (' 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 '), - ('berg ^', 'bg ^'), ('berg ^', ' bg ^'), - (' berg ^', 'bg ^'), (' berg ^', ' bg ^')} + assert repl == [(' berg ', [' bg']), + (' berg ^', [' bg ^', 'bg ^']), + ('berg ', ['bg']), + ('berg ^', [' bg ^', 'bg ^'])] def test_add_multiple_suffix(self): repl = self.get_replacements("~berg,~burg -> bg") - assert repl == {('berg ', 'berg '), ('berg ', ' berg '), - (' berg ', ' berg '), (' berg ', 'berg '), - ('berg ', 'bg '), ('berg ', ' bg '), - (' berg ', 'bg '), (' berg ', ' bg '), - ('burg ', 'burg '), ('burg ', ' burg '), - (' burg ', ' burg '), (' burg ', 'burg '), - ('burg ', 'bg '), ('burg ', ' bg '), - (' burg ', 'bg '), (' burg ', ' bg ')} + assert repl == [(' berg ', [' berg', ' bg', 'berg', 'bg']), + (' burg ', [' bg', ' burg', 'bg', 'burg']), + ('berg ', [' berg', ' bg', 'berg', 'bg']), + ('burg ', [' bg', ' burg', 'bg', 'burg'])] -- 2.39.5