]> git.openstreetmap.org Git - nominatim.git/commitdiff
add support for external token analysis modules
authorSarah Hoffmann <lonvia@denofr.de>
Mon, 25 Jul 2022 14:27:22 +0000 (16:27 +0200)
committerSarah Hoffmann <lonvia@denofr.de>
Mon, 25 Jul 2022 14:27:22 +0000 (16:27 +0200)
nominatim/config.py
nominatim/tokenizer/icu_rule_loader.py

index 7f57a3be58daf555eb53f56f47a978694076057c..7502aff703ebaaf4610ddc4b591d212957c1229c 100644 (file)
@@ -222,7 +222,7 @@ class Configuration:
         return result
 
 
-    def load_plugin_module(self, module_name: str, internal_path: str) -> object:
+    def load_plugin_module(self, module_name: str, internal_path: str) -> Any:
         """ Load a Python module as a plugin.
 
             The module_name may have three variants:
index cf9fdb88ac16fa96ea2e6a5cda2a4e1afb43d5f5..f461a1f11d8eafc24f56d058dc2430bdf554270a 100644 (file)
@@ -8,7 +8,6 @@
 Helper class to create ICU rules from a configuration file.
 """
 from typing import Mapping, Any, Dict, Optional
-import importlib
 import io
 import json
 import logging
@@ -145,7 +144,9 @@ class ICURuleLoader:
                     LOG.fatal("ICU tokenizer configuration has two token "
                               "analyzers with id '%s'.", name)
                 raise UsageError("Syntax error in ICU tokenizer config.")
-            self.analysis[name] = TokenAnalyzerRule(section, self.normalization_rules)
+            self.analysis[name] = TokenAnalyzerRule(section,
+                                                    self.normalization_rules,
+                                                    self.config)
 
 
     @staticmethod
@@ -169,15 +170,18 @@ class TokenAnalyzerRule:
         and creates a new token analyzer on request.
     """
 
-    def __init__(self, rules: Mapping[str, Any], normalization_rules: str) -> None:
-        # Find the analysis module
-        module_name = 'nominatim.tokenizer.token_analysis.' \
-                      + _get_section(rules, 'analyzer').replace('-', '_')
-        self._analysis_mod: AnalysisModule = importlib.import_module(module_name)
+    def __init__(self, rules: Mapping[str, Any], normalization_rules: str,
+                 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')
 
-        # Load the configuration.
         self.config = self._analysis_mod.configure(rules, normalization_rules)
 
+
     def create(self, normalizer: Any, transliterator: Any) -> Analyser:
         """ Create a new analyser instance for the given rule.
         """