2 Helper class to create ICU rules from a configuration file.
9 from nominatim.config import flatten_config_list
10 from nominatim.db.properties import set_property, get_property
11 from nominatim.errors import UsageError
12 from nominatim.tokenizer.place_sanitizer import PlaceSanitizer
14 LOG = logging.getLogger()
16 DBCFG_IMPORT_NORM_RULES = "tokenizer_import_normalisation"
17 DBCFG_IMPORT_TRANS_RULES = "tokenizer_import_transliteration"
18 DBCFG_IMPORT_ANALYSIS_RULES = "tokenizer_import_analysis_rules"
21 def _get_section(rules, section):
22 """ Get the section named 'section' from the rules. If the section does
23 not exist, raise a usage error with a meaningful message.
25 if section not in rules:
26 LOG.fatal("Section '%s' not found in tokenizer config.", section)
27 raise UsageError("Syntax error in tokenizer configuration file.")
33 """ Compiler for ICU rules from a tokenizer configuration file.
36 def __init__(self, config):
37 rules = config.load_sub_configuration('icu_tokenizer.yaml',
38 config='TOKENIZER_CONFIG')
40 self.normalization_rules = self._cfg_to_icu_rules(rules, 'normalization')
41 self.transliteration_rules = self._cfg_to_icu_rules(rules, 'transliteration')
42 self.analysis_rules = _get_section(rules, 'token-analysis')
43 self._setup_analysis()
45 # Load optional sanitizer rule set.
46 self.sanitizer_rules = rules.get('sanitizers', [])
49 def load_config_from_db(self, conn):
50 """ Get previously saved parts of the configuration from the
53 self.normalization_rules = get_property(conn, DBCFG_IMPORT_NORM_RULES)
54 self.transliteration_rules = get_property(conn, DBCFG_IMPORT_TRANS_RULES)
55 self.analysis_rules = json.loads(get_property(conn, DBCFG_IMPORT_ANALYSIS_RULES))
56 self._setup_analysis()
59 def save_config_to_db(self, conn):
60 """ Save the part of the configuration that cannot be changed into
63 set_property(conn, DBCFG_IMPORT_NORM_RULES, self.normalization_rules)
64 set_property(conn, DBCFG_IMPORT_TRANS_RULES, self.transliteration_rules)
65 set_property(conn, DBCFG_IMPORT_ANALYSIS_RULES, json.dumps(self.analysis_rules))
68 def make_sanitizer(self):
69 """ Create a place sanitizer from the configured rules.
71 return PlaceSanitizer(self.sanitizer_rules)
74 def make_token_analysis(self):
75 """ Create a token analyser from the reviouly loaded rules.
77 return self.analysis[None].create(self.normalization_rules,
78 self.transliteration_rules)
81 def get_search_rules(self):
82 """ Return the ICU rules to be used during search.
83 The rules combine normalization and transliteration.
85 # First apply the normalization rules.
87 rules.write(self.normalization_rules)
89 # Then add transliteration.
90 rules.write(self.transliteration_rules)
91 return rules.getvalue()
94 def get_normalization_rules(self):
95 """ Return rules for normalisation of a term.
97 return self.normalization_rules
100 def get_transliteration_rules(self):
101 """ Return the rules for converting a string into its asciii representation.
103 return self.transliteration_rules
106 def _setup_analysis(self):
107 """ Process the rules used for creating the various token analyzers.
111 if not isinstance(self.analysis_rules, list):
112 raise UsageError("Configuration section 'token-analysis' must be a list.")
114 for section in self.analysis_rules:
115 name = section.get('id', None)
116 if name in self.analysis:
118 LOG.fatal("ICU tokenizer configuration has two default token analyzers.")
120 LOG.fatal("ICU tokenizer configuration has two token "
121 "analyzers with id '%s'.", name)
122 UsageError("Syntax error in ICU tokenizer config.")
123 self.analysis[name] = TokenAnalyzerRule(section, self.normalization_rules)
127 def _cfg_to_icu_rules(rules, section):
128 """ Load an ICU ruleset from the given section. If the section is a
129 simple string, it is interpreted as a file name and the rules are
130 loaded verbatim from the given file. The filename is expected to be
131 relative to the tokenizer rule file. If the section is a list then
132 each line is assumed to be a rule. All rules are concatenated and returned.
134 content = _get_section(rules, section)
139 return ';'.join(flatten_config_list(content, section)) + ';'
142 class TokenAnalyzerRule:
143 """ Factory for a single analysis module. The class saves the configuration
144 and creates a new token analyzer on request.
147 def __init__(self, rules, normalization_rules):
148 # Find the analysis module
149 module_name = 'nominatim.tokenizer.token_analysis.' \
150 + _get_section(rules, 'analyzer').replace('-', '_')
151 analysis_mod = importlib.import_module(module_name)
152 self._mod_create = analysis_mod.create
154 # Load the configuration.
155 self.config = analysis_mod.configure(rules, normalization_rules)
158 def create(self, normalization_rules, transliteration_rules):
159 """ Create an analyzer from the given rules.
161 return self._mod_create(normalization_rules,
162 transliteration_rules,