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
13 from nominatim.tokenizer.icu_token_analysis import ICUTokenAnalysis
14 import nominatim.tools.country_info
16 LOG = logging.getLogger()
18 DBCFG_IMPORT_NORM_RULES = "tokenizer_import_normalisation"
19 DBCFG_IMPORT_TRANS_RULES = "tokenizer_import_transliteration"
20 DBCFG_IMPORT_ANALYSIS_RULES = "tokenizer_import_analysis_rules"
23 def _get_section(rules, section):
24 """ Get the section named 'section' from the rules. If the section does
25 not exist, raise a usage error with a meaningful message.
27 if section not in rules:
28 LOG.fatal("Section '%s' not found in tokenizer config.", section)
29 raise UsageError("Syntax error in tokenizer configuration file.")
35 """ Compiler for ICU rules from a tokenizer configuration file.
38 def __init__(self, config):
39 rules = config.load_sub_configuration('icu_tokenizer.yaml',
40 config='TOKENIZER_CONFIG')
42 # Make sure country information is available to analyzers and sanatizers.
43 nominatim.tools.country_info.setup_country_config(config)
45 self.normalization_rules = self._cfg_to_icu_rules(rules, 'normalization')
46 self.transliteration_rules = self._cfg_to_icu_rules(rules, 'transliteration')
47 self.analysis_rules = _get_section(rules, 'token-analysis')
48 self._setup_analysis()
50 # Load optional sanitizer rule set.
51 self.sanitizer_rules = rules.get('sanitizers', [])
54 def load_config_from_db(self, conn):
55 """ Get previously saved parts of the configuration from the
58 self.normalization_rules = get_property(conn, DBCFG_IMPORT_NORM_RULES)
59 self.transliteration_rules = get_property(conn, DBCFG_IMPORT_TRANS_RULES)
60 self.analysis_rules = json.loads(get_property(conn, DBCFG_IMPORT_ANALYSIS_RULES))
61 self._setup_analysis()
64 def save_config_to_db(self, conn):
65 """ Save the part of the configuration that cannot be changed into
68 set_property(conn, DBCFG_IMPORT_NORM_RULES, self.normalization_rules)
69 set_property(conn, DBCFG_IMPORT_TRANS_RULES, self.transliteration_rules)
70 set_property(conn, DBCFG_IMPORT_ANALYSIS_RULES, json.dumps(self.analysis_rules))
73 def make_sanitizer(self):
74 """ Create a place sanitizer from the configured rules.
76 return PlaceSanitizer(self.sanitizer_rules)
79 def make_token_analysis(self):
80 """ Create a token analyser from the reviouly loaded rules.
82 return ICUTokenAnalysis(self.normalization_rules,
83 self.transliteration_rules, self.analysis)
86 def get_search_rules(self):
87 """ Return the ICU rules to be used during search.
88 The rules combine normalization and transliteration.
90 # First apply the normalization rules.
92 rules.write(self.normalization_rules)
94 # Then add transliteration.
95 rules.write(self.transliteration_rules)
96 return rules.getvalue()
99 def get_normalization_rules(self):
100 """ Return rules for normalisation of a term.
102 return self.normalization_rules
105 def get_transliteration_rules(self):
106 """ Return the rules for converting a string into its asciii representation.
108 return self.transliteration_rules
111 def _setup_analysis(self):
112 """ Process the rules used for creating the various token analyzers.
116 if not isinstance(self.analysis_rules, list):
117 raise UsageError("Configuration section 'token-analysis' must be a list.")
119 for section in self.analysis_rules:
120 name = section.get('id', None)
121 if name in self.analysis:
123 LOG.fatal("ICU tokenizer configuration has two default token analyzers.")
125 LOG.fatal("ICU tokenizer configuration has two token "
126 "analyzers with id '%s'.", name)
127 raise UsageError("Syntax error in ICU tokenizer config.")
128 self.analysis[name] = TokenAnalyzerRule(section, self.normalization_rules)
132 def _cfg_to_icu_rules(rules, section):
133 """ Load an ICU ruleset from the given section. If the section is a
134 simple string, it is interpreted as a file name and the rules are
135 loaded verbatim from the given file. The filename is expected to be
136 relative to the tokenizer rule file. If the section is a list then
137 each line is assumed to be a rule. All rules are concatenated and returned.
139 content = _get_section(rules, section)
144 return ';'.join(flatten_config_list(content, section)) + ';'
147 class TokenAnalyzerRule:
148 """ Factory for a single analysis module. The class saves the configuration
149 and creates a new token analyzer on request.
152 def __init__(self, rules, normalization_rules):
153 # Find the analysis module
154 module_name = 'nominatim.tokenizer.token_analysis.' \
155 + _get_section(rules, 'analyzer').replace('-', '_')
156 analysis_mod = importlib.import_module(module_name)
157 self.create = analysis_mod.create
159 # Load the configuration.
160 self.config = analysis_mod.configure(rules, normalization_rules)