1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2024 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Functions for creating a tokenizer or initialising the right one for an
11 A tokenizer is something that is bound to the lifetime of a database. It
12 can be chosen and configured before the initial import but then needs to
13 be used consistently when querying and updating the database.
15 This module provides the functions to create and configure a new tokenizer
16 as well as instantiating the appropriate tokenizer for updating an existing
19 A tokenizer usually also includes PHP code for querying. The appropriate PHP
20 normalizer module is installed, when the tokenizer is created.
22 from typing import Optional
25 from pathlib import Path
27 from nominatim_core.errors import UsageError
28 from nominatim_core.db import properties
29 from nominatim_core.db.connection import connect
30 from nominatim_core.config import Configuration
31 from ..tokenizer.base import AbstractTokenizer, TokenizerModule
33 LOG = logging.getLogger()
35 def _import_tokenizer(name: str) -> TokenizerModule:
36 """ Load the tokenizer.py module from project directory.
38 src_file = Path(__file__).parent / (name + '_tokenizer.py')
39 if not src_file.is_file():
40 LOG.fatal("No tokenizer named '%s' available. "
41 "Check the setting of NOMINATIM_TOKENIZER.", name)
42 raise UsageError('Tokenizer not found')
44 return importlib.import_module('nominatim.tokenizer.' + name + '_tokenizer')
47 def create_tokenizer(config: Configuration, init_db: bool = True,
48 module_name: Optional[str] = None) -> AbstractTokenizer:
49 """ Create a new tokenizer as defined by the given configuration.
51 The tokenizer data and code is copied into the 'tokenizer' directory
52 of the project directory and the tokenizer loaded from its new location.
54 if module_name is None:
55 module_name = config.TOKENIZER
57 # Create the directory for the tokenizer data
58 assert config.project_dir is not None
59 basedir = config.project_dir / 'tokenizer'
60 if not basedir.exists():
62 elif not basedir.is_dir():
63 LOG.fatal("Tokenizer directory '%s' cannot be created.", basedir)
64 raise UsageError("Tokenizer setup failed.")
66 # Import and initialize the tokenizer.
67 tokenizer_module = _import_tokenizer(module_name)
69 tokenizer = tokenizer_module.create(config.get_libpq_dsn(), basedir)
70 tokenizer.init_new_db(config, init_db=init_db)
72 with connect(config.get_libpq_dsn()) as conn:
73 properties.set_property(conn, 'tokenizer', module_name)
78 def get_tokenizer_for_db(config: Configuration) -> AbstractTokenizer:
79 """ Instantiate a tokenizer for an existing database.
81 The function looks up the appropriate tokenizer in the database
84 assert config.project_dir is not None
85 basedir = config.project_dir / 'tokenizer'
86 if not basedir.is_dir():
87 # Directory will be repopulated by tokenizer below.
90 with connect(config.get_libpq_dsn()) as conn:
91 name = properties.get_property(conn, 'tokenizer')
94 LOG.fatal("Tokenizer was not set up properly. Database property missing.")
95 raise UsageError('Cannot initialize tokenizer.')
97 tokenizer_module = _import_tokenizer(name)
99 tokenizer = tokenizer_module.create(config.get_libpq_dsn(), basedir)
100 tokenizer.init_from_project(config)