]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/tokenizer/legacy_tokenizer.py
introduce tokenizer modules
[nominatim.git] / nominatim / tokenizer / legacy_tokenizer.py
1 """
2 Tokenizer implementing normalisation as used before Nominatim 4.
3 """
4 from nominatim.db.connection import connect
5 from nominatim.db import properties
6
7 DBCFG_NORMALIZATION = "tokenizer_normalization"
8
9 def create(dsn, data_dir):
10     """ Create a new instance of the tokenizer provided by this module.
11     """
12     return LegacyTokenizer(dsn, data_dir)
13
14 class LegacyTokenizer:
15     """ The legacy tokenizer uses a special PostgreSQL module to normalize
16         names and queries. The tokenizer thus implements normalization through
17         calls to the database.
18     """
19
20     def __init__(self, dsn, data_dir):
21         self.dsn = dsn
22         self.data_dir = data_dir
23         self.normalization = None
24
25
26     def init_new_db(self, config):
27         """ Set up a new tokenizer for the database.
28
29             This copies all necessary data in the project directory to make
30             sure the tokenizer remains stable even over updates.
31         """
32         self.normalization = config.TERM_NORMALIZATION
33
34         # Stable configuration is saved in the database.
35         with connect(self.dsn) as conn:
36             properties.set_property(conn, DBCFG_NORMALIZATION,
37                                     self.normalization)
38
39
40     def init_from_project(self):
41         """ Initialise the tokenizer from the project directory.
42         """
43         with connect(self.dsn) as conn:
44             self.normalization = properties.get_property(conn, DBCFG_NORMALIZATION)