2 Tokenizer implementing normalisation as used before Nominatim 4 but using
3 libICU instead of the PostgreSQL module.
5 from collections import Counter
9 from textwrap import dedent
10 from pathlib import Path
12 from nominatim.db.connection import connect
13 from nominatim.db.properties import set_property, get_property
14 from nominatim.db.utils import CopyBuffer
15 from nominatim.db.sql_preprocessor import SQLPreprocessor
16 from nominatim.tokenizer.icu_rule_loader import ICURuleLoader
17 from nominatim.tokenizer.icu_name_processor import ICUNameProcessor, ICUNameProcessorRules
19 DBCFG_MAXWORDFREQ = "tokenizer_maxwordfreq"
20 DBCFG_TERM_NORMALIZATION = "tokenizer_term_normalization"
22 LOG = logging.getLogger()
24 def create(dsn, data_dir):
25 """ Create a new instance of the tokenizer provided by this module.
27 return LegacyICUTokenizer(dsn, data_dir)
30 class LegacyICUTokenizer:
31 """ This tokenizer uses libICU to covert names and queries to ASCII.
32 Otherwise it uses the same algorithms and data structures as the
33 normalization routines in Nominatim 3.
36 def __init__(self, dsn, data_dir):
38 self.data_dir = data_dir
39 self.naming_rules = None
40 self.term_normalization = None
41 self.max_word_frequency = None
44 def init_new_db(self, config, init_db=True):
45 """ Set up a new tokenizer for the database.
47 This copies all necessary data in the project directory to make
48 sure the tokenizer remains stable even over updates.
50 if config.TOKENIZER_CONFIG:
51 cfgfile = Path(config.TOKENIZER_CONFIG)
53 cfgfile = config.config_dir / 'legacy_icu_tokenizer.yaml'
55 loader = ICURuleLoader(cfgfile)
56 self.naming_rules = ICUNameProcessorRules(loader=loader)
57 self.term_normalization = config.TERM_NORMALIZATION
58 self.max_word_frequency = config.MAX_WORD_FREQUENCY
60 self._install_php(config.lib_dir.php)
61 self._save_config(config)
64 self.update_sql_functions(config)
65 self._init_db_tables(config)
68 def init_from_project(self):
69 """ Initialise the tokenizer from the project directory.
71 with connect(self.dsn) as conn:
72 self.naming_rules = ICUNameProcessorRules(conn=conn)
73 self.term_normalization = get_property(conn, DBCFG_TERM_NORMALIZATION)
74 self.max_word_frequency = get_property(conn, DBCFG_MAXWORDFREQ)
77 def finalize_import(self, config):
78 """ Do any required postprocessing to make the tokenizer data ready
81 with connect(self.dsn) as conn:
82 sqlp = SQLPreprocessor(conn, config)
83 sqlp.run_sql_file(conn, 'tokenizer/legacy_tokenizer_indices.sql')
86 def update_sql_functions(self, config):
87 """ Reimport the SQL functions for this tokenizer.
89 with connect(self.dsn) as conn:
90 max_word_freq = get_property(conn, DBCFG_MAXWORDFREQ)
91 sqlp = SQLPreprocessor(conn, config)
92 sqlp.run_sql_file(conn, 'tokenizer/legacy_icu_tokenizer.sql',
93 max_word_freq=max_word_freq)
96 def check_database(self):
97 """ Check that the tokenizer is set up correctly.
99 self.init_from_project()
101 if self.naming_rules is None:
102 return "Configuration for tokenizer 'legacy_icu' are missing."
107 def name_analyzer(self):
108 """ Create a new analyzer for tokenizing names and queries
109 using this tokinzer. Analyzers are context managers and should
113 with tokenizer.name_analyzer() as analyzer:
117 When used outside the with construct, the caller must ensure to
118 call the close() function before destructing the analyzer.
120 Analyzers are not thread-safe. You need to instantiate one per thread.
122 return LegacyICUNameAnalyzer(self.dsn, ICUNameProcessor(self.naming_rules))
124 # pylint: disable=missing-format-attribute
125 def _install_php(self, phpdir):
126 """ Install the php script for the tokenizer.
128 php_file = self.data_dir / "tokenizer.php"
129 php_file.write_text(dedent("""\
131 @define('CONST_Max_Word_Frequency', {0.max_word_frequency});
132 @define('CONST_Term_Normalization_Rules', "{0.term_normalization}");
133 @define('CONST_Transliteration', "{0.naming_rules.search_rules}");
134 require_once('{1}/tokenizer/legacy_icu_tokenizer.php');
135 """.format(self, phpdir)))
138 def _save_config(self, config):
139 """ Save the configuration that needs to remain stable for the given
140 database as database properties.
142 with connect(self.dsn) as conn:
143 self.naming_rules.save_rules(conn)
145 set_property(conn, DBCFG_MAXWORDFREQ, config.MAX_WORD_FREQUENCY)
146 set_property(conn, DBCFG_TERM_NORMALIZATION, self.term_normalization)
149 def _init_db_tables(self, config):
150 """ Set up the word table and fill it with pre-computed word
153 with connect(self.dsn) as conn:
154 sqlp = SQLPreprocessor(conn, config)
155 sqlp.run_sql_file(conn, 'tokenizer/icu_tokenizer_tables.sql')
158 LOG.warning("Precomputing word tokens")
160 # get partial words and their frequencies
162 name_proc = ICUNameProcessor(self.naming_rules)
163 with conn.cursor(name="words") as cur:
164 cur.execute(""" SELECT v, count(*) FROM
165 (SELECT svals(name) as v FROM place)x
166 WHERE length(v) < 75 GROUP BY v""")
168 for name, cnt in cur:
170 for word in name_proc.get_variants_ascii(name_proc.get_normalized(name)):
172 terms.update(word.split())
176 # copy them back into the word table
177 with CopyBuffer() as copystr:
178 for args in words.items():
181 with conn.cursor() as cur:
182 copystr.copy_out(cur, 'word',
183 columns=['word_token', 'search_name_count'])
184 cur.execute("""UPDATE word SET word_id = nextval('seq_word')
185 WHERE word_id is null""")
190 class LegacyICUNameAnalyzer:
191 """ The legacy analyzer uses the ICU library for splitting names.
193 Each instance opens a connection to the database to request the
197 def __init__(self, dsn, name_proc):
198 self.conn = connect(dsn).connection
199 self.conn.autocommit = True
200 self.name_processor = name_proc
202 self._cache = _TokenCache()
209 def __exit__(self, exc_type, exc_value, traceback):
214 """ Free all resources used by the analyzer.
221 def get_word_token_info(self, words):
222 """ Return token information for the given list of words.
223 If a word starts with # it is assumed to be a full name
224 otherwise is a partial name.
226 The function returns a list of tuples with
227 (original word, word token, word id).
229 The function is used for testing and debugging only
230 and not necessarily efficient.
234 if word.startswith('#'):
235 tokens[word] = ' ' + self.name_processor.get_search_normalized(word[1:])
237 tokens[word] = self.name_processor.get_search_normalized(word)
239 with self.conn.cursor() as cur:
240 cur.execute("""SELECT word_token, word_id
241 FROM word, (SELECT unnest(%s::TEXT[]) as term) t
242 WHERE word_token = t.term
243 and class is null and country_code is null""",
244 (list(tokens.values()), ))
245 ids = {r[0]: r[1] for r in cur}
247 return [(k, v, ids.get(v, None)) for k, v in tokens.items()]
251 def normalize_postcode(postcode):
252 """ Convert the postcode to a standardized form.
254 This function must yield exactly the same result as the SQL function
255 'token_normalized_postcode()'.
257 return postcode.strip().upper()
260 def _make_standard_hnr(self, hnr):
261 """ Create a normalised version of a housenumber.
263 This function takes minor shortcuts on transliteration.
265 return self.name_processor.get_search_normalized(hnr)
267 def update_postcodes_from_db(self):
268 """ Update postcode tokens in the word table from the location_postcode
272 with self.conn.cursor() as cur:
273 # This finds us the rows in location_postcode and word that are
274 # missing in the other table.
275 cur.execute("""SELECT * FROM
276 (SELECT pc, word FROM
277 (SELECT distinct(postcode) as pc FROM location_postcode) p
279 (SELECT word FROM word
280 WHERE class ='place' and type = 'postcode') w
282 WHERE pc is null or word is null""")
284 with CopyBuffer() as copystr:
285 for postcode, word in cur:
287 to_delete.append(word)
291 ' ' + self.name_processor.get_search_normalized(postcode),
292 'place', 'postcode', 0)
295 cur.execute("""DELETE FROM WORD
296 WHERE class ='place' and type = 'postcode'
300 copystr.copy_out(cur, 'word',
301 columns=['word', 'word_token', 'class', 'type',
302 'search_name_count'])
305 def update_special_phrases(self, phrases, should_replace):
306 """ Replace the search index for special phrases with the new phrases.
308 norm_phrases = set(((self.name_processor.get_normalized(p[0]), p[1], p[2], p[3])
311 with self.conn.cursor() as cur:
312 # Get the old phrases.
313 existing_phrases = set()
314 cur.execute("""SELECT word, class, type, operator FROM word
315 WHERE class != 'place'
316 OR (type != 'house' AND type != 'postcode')""")
317 for label, cls, typ, oper in cur:
318 existing_phrases.add((label, cls, typ, oper or '-'))
320 added = self._add_special_phrases(cur, norm_phrases, existing_phrases)
322 deleted = self._remove_special_phrases(cur, norm_phrases,
327 LOG.info("Total phrases: %s. Added: %s. Deleted: %s",
328 len(norm_phrases), added, deleted)
331 def _add_special_phrases(self, cursor, new_phrases, existing_phrases):
332 """ Add all phrases to the database that are not yet there.
334 to_add = new_phrases - existing_phrases
337 with CopyBuffer() as copystr:
338 for word, cls, typ, oper in to_add:
339 term = self.name_processor.get_search_normalized(word)
341 copystr.add(word, ' ' + term, cls, typ,
342 oper if oper in ('in', 'near') else None, 0)
345 copystr.copy_out(cursor, 'word',
346 columns=['word', 'word_token', 'class', 'type',
347 'operator', 'search_name_count'])
353 def _remove_special_phrases(cursor, new_phrases, existing_phrases):
354 """ Remove all phrases from the databse that are no longer in the
357 to_delete = existing_phrases - new_phrases
360 cursor.execute_values(
361 """ DELETE FROM word USING (VALUES %s) as v(name, in_class, in_type, op)
362 WHERE word = name and class = in_class and type = in_type
363 and ((op = '-' and operator is null) or op = operator)""",
366 return len(to_delete)
369 def add_country_names(self, country_code, names):
370 """ Add names for the given country to the search index.
373 for name in self._compute_full_names(names):
374 norm_name = self.name_processor.get_search_normalized(name)
376 word_tokens.add(norm_name)
378 with self.conn.cursor() as cur:
380 cur.execute("""SELECT word_token FROM word
381 WHERE type = 'C' and info->>'cc'= %s""",
383 word_tokens.difference_update((t[0] for t in cur))
385 # Only add those names that are not yet in the list.
387 cur.execute("""INSERT INTO word (word_token, type, info)
388 (SELECT token, 'C', json_build_object('cc', %s)
389 FROM unnest(%s) as token)
390 """, (country_code, list(word_tokens)))
392 # No names are deleted at the moment.
393 # If deletion is made possible, then the static names from the
394 # initial 'country_name' table should be kept.
397 def process_place(self, place):
398 """ Determine tokenizer information about the given place.
400 Returns a JSON-serialisable structure that will be handed into
401 the database via the token_info field.
403 token_info = _TokenInfo(self._cache)
405 names = place.get('name')
408 fulls, partials = self._compute_name_tokens(names)
410 token_info.add_names(fulls, partials)
412 country_feature = place.get('country_feature')
413 if country_feature and re.fullmatch(r'[A-Za-z][A-Za-z]', country_feature):
414 self.add_country_names(country_feature.lower(), names)
416 address = place.get('address')
418 self._process_place_address(token_info, address)
420 return token_info.data
423 def _process_place_address(self, token_info, address):
426 for key, value in address.items():
427 if key == 'postcode':
428 self._add_postcode(value)
429 elif key in ('housenumber', 'streetnumber', 'conscriptionnumber'):
431 elif key == 'street':
432 token_info.add_street(*self._compute_name_tokens({'name': value}))
434 token_info.add_place(*self._compute_name_tokens({'name': value}))
435 elif not key.startswith('_') and \
436 key not in ('country', 'full'):
437 addr_terms.append((key, *self._compute_name_tokens({'name': value})))
440 hnrs = self._split_housenumbers(hnrs)
441 token_info.add_housenumbers(self.conn, [self._make_standard_hnr(n) for n in hnrs])
444 token_info.add_address_terms(addr_terms)
447 def _compute_name_tokens(self, names):
448 """ Computes the full name and partial name tokens for the given
451 full_names = self._compute_full_names(names)
453 partial_tokens = set()
455 for name in full_names:
456 norm_name = self.name_processor.get_normalized(name)
457 full, part = self._cache.names.get(norm_name, (None, None))
459 variants = self.name_processor.get_variants_ascii(norm_name)
463 with self.conn.cursor() as cur:
464 cur.execute("SELECT (getorcreate_full_word(%s, %s)).*",
465 (norm_name, variants))
466 full, part = cur.fetchone()
468 self._cache.names[norm_name] = (full, part)
470 full_tokens.add(full)
471 partial_tokens.update(part)
473 return full_tokens, partial_tokens
477 def _compute_full_names(names):
478 """ Return the set of all full name word ids to be used with the
479 given dictionary of names.
482 for name in (n.strip() for ns in names.values() for n in re.split('[;,]', ns)):
486 brace_idx = name.find('(')
488 full_names.add(name[:brace_idx].strip())
493 def _add_postcode(self, postcode):
494 """ Make sure the normalized postcode is present in the word table.
496 if re.search(r'[:,;]', postcode) is None:
497 postcode = self.normalize_postcode(postcode)
499 if postcode not in self._cache.postcodes:
500 term = self.name_processor.get_search_normalized(postcode)
504 with self.conn.cursor() as cur:
505 # no word_id needed for postcodes
506 cur.execute("""INSERT INTO word (word, word_token, class, type,
508 (SELECT pc, %s, 'place', 'postcode', 0
509 FROM (VALUES (%s)) as v(pc)
512 WHERE word = pc and class='place' and type='postcode'))
513 """, (' ' + term, postcode))
514 self._cache.postcodes.add(postcode)
518 def _split_housenumbers(hnrs):
519 if len(hnrs) > 1 or ',' in hnrs[0] or ';' in hnrs[0]:
520 # split numbers if necessary
523 simple_list.extend((x.strip() for x in re.split(r'[;,]', hnr)))
525 if len(simple_list) > 1:
526 hnrs = list(set(simple_list))
536 """ Collect token information to be sent back to the database.
538 def __init__(self, cache):
543 def _mk_array(tokens):
544 return '{%s}' % ','.join((str(s) for s in tokens))
547 def add_names(self, fulls, partials):
548 """ Adds token information for the normalised names.
550 self.data['names'] = self._mk_array(itertools.chain(fulls, partials))
553 def add_housenumbers(self, conn, hnrs):
554 """ Extract housenumber information from a list of normalised
557 self.data['hnr_tokens'] = self._mk_array(self._cache.get_hnr_tokens(conn, hnrs))
558 self.data['hnr'] = ';'.join(hnrs)
561 def add_street(self, fulls, _):
562 """ Add addr:street match terms.
565 self.data['street'] = self._mk_array(fulls)
568 def add_place(self, fulls, partials):
569 """ Add addr:place search and match terms.
572 self.data['place_search'] = self._mk_array(itertools.chain(fulls, partials))
573 self.data['place_match'] = self._mk_array(fulls)
576 def add_address_terms(self, terms):
577 """ Add additional address terms.
581 for key, fulls, partials in terms:
583 tokens[key] = [self._mk_array(itertools.chain(fulls, partials)),
584 self._mk_array(fulls)]
587 self.data['addr'] = tokens
591 """ Cache for token information to avoid repeated database queries.
593 This cache is not thread-safe and needs to be instantiated per
598 self.postcodes = set()
599 self.housenumbers = {}
602 def get_hnr_tokens(self, conn, terms):
603 """ Get token ids for a list of housenumbers, looking them up in the
604 database if necessary. `terms` is an iterable of normalized
611 token = self.housenumbers.get(term)
618 with conn.cursor() as cur:
619 cur.execute("SELECT nr, getorcreate_hnr_id(nr) FROM unnest(%s) as nr",
621 for term, tid in cur:
622 self.housenumbers[term] = tid