2 Tests for Legacy ICU tokenizer.
8 from nominatim.tokenizer import legacy_icu_tokenizer
9 from nominatim.db import properties
13 def test_config(def_config, tmp_path):
14 def_config.project_dir = tmp_path / 'project'
15 def_config.project_dir.mkdir()
17 sqldir = tmp_path / 'sql'
19 (sqldir / 'tokenizer').mkdir()
20 (sqldir / 'tokenizer' / 'legacy_icu_tokenizer.sql').write_text("SELECT 'a'")
21 shutil.copy(str(def_config.lib_dir.sql / 'tokenizer' / 'legacy_tokenizer_tables.sql'),
22 str(sqldir / 'tokenizer' / 'legacy_tokenizer_tables.sql'))
24 def_config.lib_dir.sql = sqldir
30 def tokenizer_factory(dsn, tmp_path, property_table,
31 sql_preprocessor, place_table, word_table):
32 (tmp_path / 'tokenizer').mkdir()
35 return legacy_icu_tokenizer.create(dsn, tmp_path / 'tokenizer')
41 def db_prop(temp_db_conn):
42 def _get_db_property(name):
43 return properties.get_property(temp_db_conn,
44 getattr(legacy_icu_tokenizer, name))
46 return _get_db_property
49 def tokenizer_setup(tokenizer_factory, test_config, monkeypatch, sql_preprocessor):
50 tok = tokenizer_factory()
51 tok.init_new_db(test_config)
55 def analyzer(tokenizer_factory, test_config, monkeypatch, sql_preprocessor,
56 word_table, temp_db_with_extensions, tmp_path):
57 sql = tmp_path / 'sql' / 'tokenizer' / 'legacy_icu_tokenizer.sql'
58 sql.write_text("SELECT 'a';")
60 monkeypatch.setenv('NOMINATIM_TERM_NORMALIZATION', ':: lower();')
61 tok = tokenizer_factory()
62 tok.init_new_db(test_config)
65 def _mk_analyser(trans=':: upper();', abbr=(('STREET', 'ST'), )):
66 tok.transliteration = trans
67 tok.abbreviations = abbr
69 return tok.name_analyzer()
75 def getorcreate_term_id(temp_db_cursor):
76 temp_db_cursor.execute("""CREATE OR REPLACE FUNCTION getorcreate_term_id(lookup_term TEXT)
77 RETURNS INTEGER AS $$ SELECT nextval('seq_word')::INTEGER; $$ LANGUAGE SQL""")
81 def getorcreate_hnr_id(temp_db_cursor):
82 temp_db_cursor.execute("""CREATE OR REPLACE FUNCTION getorcreate_hnr_id(lookup_term TEXT)
83 RETURNS INTEGER AS $$ SELECT -nextval('seq_word')::INTEGER; $$ LANGUAGE SQL""")
86 def test_init_new(tokenizer_factory, test_config, monkeypatch, db_prop,
87 sql_preprocessor, place_table, word_table):
88 monkeypatch.setenv('NOMINATIM_TERM_NORMALIZATION', ':: lower();')
90 tok = tokenizer_factory()
91 tok.init_new_db(test_config)
93 assert db_prop('DBCFG_NORMALIZATION') == ':: lower();'
94 assert db_prop('DBCFG_TRANSLITERATION') is not None
95 assert db_prop('DBCFG_ABBREVIATIONS') is not None
98 def test_init_from_project(tokenizer_setup, tokenizer_factory):
99 tok = tokenizer_factory()
101 tok.init_from_project()
103 assert tok.normalization is not None
104 assert tok.transliteration is not None
105 assert tok.abbreviations is not None
108 def test_update_sql_functions(temp_db_conn, db_prop, temp_db_cursor,
109 tokenizer_factory, test_config, table_factory,
111 sql_preprocessor, place_table, word_table):
112 monkeypatch.setenv('NOMINATIM_MAX_WORD_FREQUENCY', '1133')
113 tok = tokenizer_factory()
114 tok.init_new_db(test_config)
117 assert db_prop('DBCFG_MAXWORDFREQ') == '1133'
119 table_factory('test', 'txt TEXT')
121 func_file = test_config.lib_dir.sql / 'tokenizer' / 'legacy_icu_tokenizer.sql'
122 func_file.write_text("""INSERT INTO test VALUES ('{{max_word_freq}}')""")
124 tok.update_sql_functions(test_config)
126 test_content = temp_db_cursor.row_set('SELECT * FROM test')
127 assert test_content == set((('1133', ), ))
130 def test_make_standard_word(analyzer):
131 with analyzer(abbr=(('STREET', 'ST'), ('tiny', 't'))) as a:
132 assert a.make_standard_word('tiny street') == 'TINY ST'
134 with analyzer(abbr=(('STRASSE', 'STR'), ('STR', 'ST'))) as a:
135 assert a.make_standard_word('Hauptstrasse') == 'HAUPTST'
138 def test_make_standard_hnr(analyzer):
139 with analyzer(abbr=(('IV', '4'),)) as a:
140 assert a._make_standard_hnr('345') == '345'
141 assert a._make_standard_hnr('iv') == 'IV'
144 def test_update_postcodes_from_db_empty(analyzer, table_factory, word_table):
145 table_factory('location_postcode', 'postcode TEXT',
146 content=(('1234',), ('12 34',), ('AB23',), ('1234',)))
148 with analyzer() as a:
149 a.update_postcodes_from_db()
151 assert word_table.count() == 3
152 assert word_table.get_postcodes() == {'1234', '12 34', 'AB23'}
155 def test_update_postcodes_from_db_add_and_remove(analyzer, table_factory, word_table):
156 table_factory('location_postcode', 'postcode TEXT',
157 content=(('1234',), ('45BC', ), ('XX45', )))
158 word_table.add_postcode(' 1234', '1234')
159 word_table.add_postcode(' 5678', '5678')
161 with analyzer() as a:
162 a.update_postcodes_from_db()
164 assert word_table.count() == 3
165 assert word_table.get_postcodes() == {'1234', '45BC', 'XX45'}
168 def test_update_special_phrase_empty_table(analyzer, word_table, temp_db_cursor):
169 with analyzer() as a:
170 a.update_special_phrases([
171 ("König bei", "amenity", "royal", "near"),
172 ("Könige", "amenity", "royal", "-"),
173 ("street", "highway", "primary", "in")
176 assert temp_db_cursor.row_set("""SELECT word_token, word, class, type, operator
177 FROM word WHERE class != 'place'""") \
178 == set(((' KÖNIG BEI', 'könig bei', 'amenity', 'royal', 'near'),
179 (' KÖNIGE', 'könige', 'amenity', 'royal', None),
180 (' ST', 'street', 'highway', 'primary', 'in')))
183 def test_update_special_phrase_delete_all(analyzer, word_table):
184 word_table.add_special(' FOO', 'foo', 'amenity', 'prison', 'in')
185 word_table.add_special(' BAR', 'bar', 'highway', 'road', None)
187 assert word_table.count_special() == 2
189 with analyzer() as a:
190 a.update_special_phrases([], True)
192 assert word_table.count_special() == 0
195 def test_update_special_phrases_no_replace(analyzer, word_table):
196 word_table.add_special(' FOO', 'foo', 'amenity', 'prison', 'in')
197 word_table.add_special(' BAR', 'bar', 'highway', 'road', None)
199 assert word_table.count_special() == 2
201 with analyzer() as a:
202 a.update_special_phrases([], False)
204 assert word_table.count_special() == 2
207 def test_update_special_phrase_modify(analyzer, word_table):
208 word_table.add_special(' FOO', 'foo', 'amenity', 'prison', 'in')
209 word_table.add_special(' BAR', 'bar', 'highway', 'road', None)
211 assert word_table.count_special() == 2
213 with analyzer() as a:
214 a.update_special_phrases([
215 ('prison', 'amenity', 'prison', 'in'),
216 ('bar', 'highway', 'road', '-'),
217 ('garden', 'leisure', 'garden', 'near')
220 assert word_table.get_special() \
221 == {(' PRISON', 'prison', 'amenity', 'prison', 'in'),
222 (' BAR', 'bar', 'highway', 'road', None),
223 (' GARDEN', 'garden', 'leisure', 'garden', 'near')}
226 def test_process_place_names(analyzer, getorcreate_term_id):
228 with analyzer() as a:
229 info = a.process_place({'name' : {'name' : 'Soft bAr', 'ref': '34'}})
231 assert info['names'] == '{1,2,3,4,5,6}'
234 @pytest.mark.parametrize('pc', ['12345', 'AB 123', '34-345'])
235 def test_process_place_postcode(analyzer, word_table, pc):
236 with analyzer() as a:
237 info = a.process_place({'address': {'postcode' : pc}})
239 assert word_table.get_postcodes() == {pc, }
242 @pytest.mark.parametrize('pc', ['12:23', 'ab;cd;f', '123;836'])
243 def test_process_place_bad_postcode(analyzer, word_table, pc):
244 with analyzer() as a:
245 info = a.process_place({'address': {'postcode' : pc}})
247 assert not word_table.get_postcodes()
250 @pytest.mark.parametrize('hnr', ['123a', '1', '101'])
251 def test_process_place_housenumbers_simple(analyzer, hnr, getorcreate_hnr_id):
252 with analyzer() as a:
253 info = a.process_place({'address': {'housenumber' : hnr}})
255 assert info['hnr'] == hnr.upper()
256 assert info['hnr_tokens'] == "{-1}"
259 def test_process_place_housenumbers_lists(analyzer, getorcreate_hnr_id):
260 with analyzer() as a:
261 info = a.process_place({'address': {'conscriptionnumber' : '1; 2;3'}})
263 assert set(info['hnr'].split(';')) == set(('1', '2', '3'))
264 assert info['hnr_tokens'] == "{-1,-2,-3}"
267 def test_process_place_housenumbers_duplicates(analyzer, getorcreate_hnr_id):
268 with analyzer() as a:
269 info = a.process_place({'address': {'housenumber' : '134',
270 'conscriptionnumber' : '134',
271 'streetnumber' : '99a'}})
273 assert set(info['hnr'].split(';')) == set(('134', '99A'))
274 assert info['hnr_tokens'] == "{-1,-2}"