2022-01-03 18:23:58 +03:00
|
|
|
# SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
#
|
|
|
|
# This file is part of Nominatim. (https://nominatim.org)
|
|
|
|
#
|
|
|
|
# Copyright (C) 2022 by the Nominatim developer community.
|
|
|
|
# For a full list of authors see the git log.
|
2021-05-28 23:06:13 +03:00
|
|
|
"""
|
|
|
|
Helper class to create ICU rules from a configuration file.
|
|
|
|
"""
|
2022-07-18 00:18:55 +03:00
|
|
|
from typing import Mapping, Any, Dict, Optional
|
2021-05-28 23:06:13 +03:00
|
|
|
import io
|
2021-09-29 18:37:04 +03:00
|
|
|
import json
|
2021-05-28 23:06:13 +03:00
|
|
|
import logging
|
|
|
|
|
2022-07-29 11:43:07 +03:00
|
|
|
from icu import Transliterator
|
|
|
|
|
2022-07-13 23:55:40 +03:00
|
|
|
from nominatim.config import flatten_config_list, Configuration
|
2021-09-29 18:37:04 +03:00
|
|
|
from nominatim.db.properties import set_property, get_property
|
2022-07-13 23:55:40 +03:00
|
|
|
from nominatim.db.connection import Connection
|
2021-05-28 23:06:13 +03:00
|
|
|
from nominatim.errors import UsageError
|
2021-09-30 22:30:13 +03:00
|
|
|
from nominatim.tokenizer.place_sanitizer import PlaceSanitizer
|
2021-10-05 15:10:32 +03:00
|
|
|
from nominatim.tokenizer.icu_token_analysis import ICUTokenAnalysis
|
2022-07-29 11:52:01 +03:00
|
|
|
from nominatim.tokenizer.token_analysis.base import AnalysisModule, Analyzer
|
2022-07-06 12:08:36 +03:00
|
|
|
import nominatim.data.country_info
|
2021-05-28 23:06:13 +03:00
|
|
|
|
|
|
|
LOG = logging.getLogger()
|
|
|
|
|
2021-09-29 18:37:04 +03:00
|
|
|
DBCFG_IMPORT_NORM_RULES = "tokenizer_import_normalisation"
|
|
|
|
DBCFG_IMPORT_TRANS_RULES = "tokenizer_import_transliteration"
|
|
|
|
DBCFG_IMPORT_ANALYSIS_RULES = "tokenizer_import_analysis_rules"
|
|
|
|
|
|
|
|
|
2022-07-13 23:55:40 +03:00
|
|
|
def _get_section(rules: Mapping[str, Any], section: str) -> Any:
|
2021-10-04 18:34:30 +03:00
|
|
|
""" Get the section named 'section' from the rules. If the section does
|
|
|
|
not exist, raise a usage error with a meaningful message.
|
|
|
|
"""
|
|
|
|
if section not in rules:
|
|
|
|
LOG.fatal("Section '%s' not found in tokenizer config.", section)
|
|
|
|
raise UsageError("Syntax error in tokenizer configuration file.")
|
|
|
|
|
|
|
|
return rules[section]
|
|
|
|
|
|
|
|
|
2021-05-28 23:06:13 +03:00
|
|
|
class ICURuleLoader:
|
|
|
|
""" Compiler for ICU rules from a tokenizer configuration file.
|
|
|
|
"""
|
|
|
|
|
2022-07-13 23:55:40 +03:00
|
|
|
def __init__(self, config: Configuration) -> None:
|
2022-07-25 17:10:19 +03:00
|
|
|
self.config = config
|
2021-09-29 18:37:04 +03:00
|
|
|
rules = config.load_sub_configuration('icu_tokenizer.yaml',
|
|
|
|
config='TOKENIZER_CONFIG')
|
|
|
|
|
2022-02-07 12:48:00 +03:00
|
|
|
# Make sure country information is available to analyzers and sanitizers.
|
2022-07-06 12:08:36 +03:00
|
|
|
nominatim.data.country_info.setup_country_config(config)
|
2021-10-05 18:18:10 +03:00
|
|
|
|
2021-09-03 19:16:12 +03:00
|
|
|
self.normalization_rules = self._cfg_to_icu_rules(rules, 'normalization')
|
|
|
|
self.transliteration_rules = self._cfg_to_icu_rules(rules, 'transliteration')
|
2021-10-04 18:34:30 +03:00
|
|
|
self.analysis_rules = _get_section(rules, 'token-analysis')
|
2021-10-04 17:40:28 +03:00
|
|
|
self._setup_analysis()
|
2021-09-29 18:37:04 +03:00
|
|
|
|
2021-09-30 22:30:13 +03:00
|
|
|
# Load optional sanitizer rule set.
|
|
|
|
self.sanitizer_rules = rules.get('sanitizers', [])
|
|
|
|
|
2021-09-29 18:37:04 +03:00
|
|
|
|
2022-07-13 23:55:40 +03:00
|
|
|
def load_config_from_db(self, conn: Connection) -> None:
|
2021-09-29 18:37:04 +03:00
|
|
|
""" Get previously saved parts of the configuration from the
|
|
|
|
database.
|
|
|
|
"""
|
2022-07-13 23:55:40 +03:00
|
|
|
rules = get_property(conn, DBCFG_IMPORT_NORM_RULES)
|
|
|
|
if rules is not None:
|
|
|
|
self.normalization_rules = rules
|
|
|
|
|
|
|
|
rules = get_property(conn, DBCFG_IMPORT_TRANS_RULES)
|
|
|
|
if rules is not None:
|
|
|
|
self.transliteration_rules = rules
|
|
|
|
|
|
|
|
rules = get_property(conn, DBCFG_IMPORT_ANALYSIS_RULES)
|
|
|
|
if rules:
|
|
|
|
self.analysis_rules = json.loads(rules)
|
|
|
|
else:
|
|
|
|
self.analysis_rules = []
|
2021-10-04 17:40:28 +03:00
|
|
|
self._setup_analysis()
|
2021-09-29 18:37:04 +03:00
|
|
|
|
|
|
|
|
2022-07-13 23:55:40 +03:00
|
|
|
def save_config_to_db(self, conn: Connection) -> None:
|
2021-09-29 18:37:04 +03:00
|
|
|
""" Save the part of the configuration that cannot be changed into
|
|
|
|
the database.
|
|
|
|
"""
|
|
|
|
set_property(conn, DBCFG_IMPORT_NORM_RULES, self.normalization_rules)
|
|
|
|
set_property(conn, DBCFG_IMPORT_TRANS_RULES, self.transliteration_rules)
|
|
|
|
set_property(conn, DBCFG_IMPORT_ANALYSIS_RULES, json.dumps(self.analysis_rules))
|
|
|
|
|
|
|
|
|
2022-07-13 23:55:40 +03:00
|
|
|
def make_sanitizer(self) -> PlaceSanitizer:
|
2021-09-30 22:30:13 +03:00
|
|
|
""" Create a place sanitizer from the configured rules.
|
|
|
|
"""
|
2022-07-25 17:10:19 +03:00
|
|
|
return PlaceSanitizer(self.sanitizer_rules, self.config)
|
2021-09-30 22:30:13 +03:00
|
|
|
|
|
|
|
|
2022-07-13 23:55:40 +03:00
|
|
|
def make_token_analysis(self) -> ICUTokenAnalysis:
|
2021-09-29 18:37:04 +03:00
|
|
|
""" Create a token analyser from the reviouly loaded rules.
|
|
|
|
"""
|
2021-10-05 15:10:32 +03:00
|
|
|
return ICUTokenAnalysis(self.normalization_rules,
|
|
|
|
self.transliteration_rules, self.analysis)
|
2021-05-28 23:06:13 +03:00
|
|
|
|
|
|
|
|
2022-07-13 23:55:40 +03:00
|
|
|
def get_search_rules(self) -> str:
|
2021-06-06 12:00:44 +03:00
|
|
|
""" Return the ICU rules to be used during search.
|
2021-06-11 11:03:31 +03:00
|
|
|
The rules combine normalization and transliteration.
|
2021-05-28 23:06:13 +03:00
|
|
|
"""
|
|
|
|
# First apply the normalization rules.
|
|
|
|
rules = io.StringIO()
|
|
|
|
rules.write(self.normalization_rules)
|
|
|
|
|
2021-06-11 11:03:31 +03:00
|
|
|
# Then add transliteration.
|
2021-05-28 23:06:13 +03:00
|
|
|
rules.write(self.transliteration_rules)
|
|
|
|
return rules.getvalue()
|
|
|
|
|
2021-10-04 17:40:28 +03:00
|
|
|
|
2022-07-13 23:55:40 +03:00
|
|
|
def get_normalization_rules(self) -> str:
|
2021-05-28 23:06:13 +03:00
|
|
|
""" Return rules for normalisation of a term.
|
|
|
|
"""
|
|
|
|
return self.normalization_rules
|
|
|
|
|
2021-10-04 17:40:28 +03:00
|
|
|
|
2022-07-13 23:55:40 +03:00
|
|
|
def get_transliteration_rules(self) -> str:
|
2021-05-28 23:06:13 +03:00
|
|
|
""" Return the rules for converting a string into its asciii representation.
|
|
|
|
"""
|
|
|
|
return self.transliteration_rules
|
|
|
|
|
2021-10-04 17:40:28 +03:00
|
|
|
|
2022-07-13 23:55:40 +03:00
|
|
|
def _setup_analysis(self) -> None:
|
2021-10-04 17:40:28 +03:00
|
|
|
""" Process the rules used for creating the various token analyzers.
|
2021-05-28 23:06:13 +03:00
|
|
|
"""
|
2022-07-18 00:18:55 +03:00
|
|
|
self.analysis: Dict[Optional[str], TokenAnalyzerRule] = {}
|
2021-10-04 17:40:28 +03:00
|
|
|
|
|
|
|
if not isinstance(self.analysis_rules, list):
|
|
|
|
raise UsageError("Configuration section 'token-analysis' must be a list.")
|
|
|
|
|
2022-07-29 11:43:07 +03:00
|
|
|
norm = Transliterator.createFromRules("rule_loader_normalization",
|
|
|
|
self.normalization_rules)
|
|
|
|
trans = Transliterator.createFromRules("rule_loader_transliteration",
|
|
|
|
self.transliteration_rules)
|
|
|
|
|
2021-10-04 17:40:28 +03:00
|
|
|
for section in self.analysis_rules:
|
|
|
|
name = section.get('id', None)
|
|
|
|
if name in self.analysis:
|
|
|
|
if name is None:
|
|
|
|
LOG.fatal("ICU tokenizer configuration has two default token analyzers.")
|
|
|
|
else:
|
|
|
|
LOG.fatal("ICU tokenizer configuration has two token "
|
|
|
|
"analyzers with id '%s'.", name)
|
2021-10-06 13:29:25 +03:00
|
|
|
raise UsageError("Syntax error in ICU tokenizer config.")
|
2022-07-29 11:43:07 +03:00
|
|
|
self.analysis[name] = TokenAnalyzerRule(section, norm, trans,
|
2022-07-25 17:27:22 +03:00
|
|
|
self.config)
|
2021-05-28 23:06:13 +03:00
|
|
|
|
|
|
|
|
2021-09-03 19:16:12 +03:00
|
|
|
@staticmethod
|
2022-07-13 23:55:40 +03:00
|
|
|
def _cfg_to_icu_rules(rules: Mapping[str, Any], section: str) -> str:
|
2021-05-28 23:06:13 +03:00
|
|
|
""" Load an ICU ruleset from the given section. If the section is a
|
|
|
|
simple string, it is interpreted as a file name and the rules are
|
|
|
|
loaded verbatim from the given file. The filename is expected to be
|
|
|
|
relative to the tokenizer rule file. If the section is a list then
|
|
|
|
each line is assumed to be a rule. All rules are concatenated and returned.
|
|
|
|
"""
|
2021-10-04 18:34:30 +03:00
|
|
|
content = _get_section(rules, section)
|
2021-05-28 23:06:13 +03:00
|
|
|
|
2021-06-10 10:36:43 +03:00
|
|
|
if content is None:
|
|
|
|
return ''
|
|
|
|
|
2021-10-04 12:56:54 +03:00
|
|
|
return ';'.join(flatten_config_list(content, section)) + ';'
|
2021-05-28 23:06:13 +03:00
|
|
|
|
|
|
|
|
2022-07-18 00:18:55 +03:00
|
|
|
class TokenAnalyzerRule:
|
2021-10-04 17:40:28 +03:00
|
|
|
""" Factory for a single analysis module. The class saves the configuration
|
|
|
|
and creates a new token analyzer on request.
|
|
|
|
"""
|
|
|
|
|
2022-07-29 11:43:07 +03:00
|
|
|
def __init__(self, rules: Mapping[str, Any],
|
|
|
|
normalizer: Any, transliterator: Any,
|
2022-07-25 17:27:22 +03:00
|
|
|
config: Configuration) -> None:
|
|
|
|
analyzer_name = _get_section(rules, 'analyzer')
|
|
|
|
if not analyzer_name or not isinstance(analyzer_name, str):
|
|
|
|
raise UsageError("'analyzer' parameter needs to be simple string")
|
|
|
|
|
|
|
|
self._analysis_mod: AnalysisModule = \
|
|
|
|
config.load_plugin_module(analyzer_name, 'nominatim.tokenizer.token_analysis')
|
2021-10-04 18:34:30 +03:00
|
|
|
|
2022-07-29 11:43:07 +03:00
|
|
|
self.config = self._analysis_mod.configure(rules, normalizer,
|
|
|
|
transliterator)
|
2022-07-13 23:55:40 +03:00
|
|
|
|
2022-07-25 17:27:22 +03:00
|
|
|
|
2022-07-29 11:52:01 +03:00
|
|
|
def create(self, normalizer: Any, transliterator: Any) -> Analyzer:
|
2022-07-13 23:55:40 +03:00
|
|
|
""" Create a new analyser instance for the given rule.
|
|
|
|
"""
|
|
|
|
return self._analysis_mod.create(normalizer, transliterator, self.config)
|