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):
50 tok = tokenizer_factory()
51 tok.init_new_db(test_config)
55 def analyzer(tokenizer_factory, test_config, monkeypatch,
56 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)
78 SELECT nextval('seq_word')::INTEGER; $$ LANGUAGE SQL""")
82 def getorcreate_hnr_id(temp_db_cursor):
83 temp_db_cursor.execute("""CREATE OR REPLACE FUNCTION getorcreate_hnr_id(lookup_term TEXT)
85 SELECT -nextval('seq_word')::INTEGER; $$ LANGUAGE SQL""")
88 def test_init_new(tokenizer_factory, test_config, monkeypatch, db_prop):
89 monkeypatch.setenv('NOMINATIM_TERM_NORMALIZATION', ':: lower();')
91 tok = tokenizer_factory()
92 tok.init_new_db(test_config)
94 assert db_prop('DBCFG_NORMALIZATION') == ':: lower();'
95 assert db_prop('DBCFG_TRANSLITERATION') is not None
96 assert db_prop('DBCFG_ABBREVIATIONS') is not None
99 def test_init_from_project(tokenizer_setup, tokenizer_factory):
100 tok = tokenizer_factory()
102 tok.init_from_project()
104 assert tok.normalization is not None
105 assert tok.transliteration is not None
106 assert tok.abbreviations is not None
109 def test_update_sql_functions(db_prop, temp_db_cursor,
110 tokenizer_factory, test_config, table_factory,
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 anl:
132 assert anl.make_standard_word('tiny street') == 'TINY ST'
134 with analyzer(abbr=(('STRASSE', 'STR'), ('STR', 'ST'))) as anl:
135 assert anl.make_standard_word('Hauptstrasse') == 'HAUPTST'
138 def test_make_standard_hnr(analyzer):
139 with analyzer(abbr=(('IV', '4'),)) as anl:
140 assert anl._make_standard_hnr('345') == '345'
141 assert anl._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 anl:
149 anl.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 anl:
162 anl.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):
169 with analyzer() as anl:
170 anl.update_special_phrases([
171 ("König bei", "amenity", "royal", "near"),
172 ("Könige", "amenity", "royal", "-"),
173 ("street", "highway", "primary", "in")
176 assert word_table.get_special() \
177 == {(' KÖNIG BEI', 'könig bei', 'amenity', 'royal', 'near'),
178 (' KÖNIGE', 'könige', 'amenity', 'royal', None),
179 (' ST', 'street', 'highway', 'primary', 'in')}
182 def test_update_special_phrase_delete_all(analyzer, word_table):
183 word_table.add_special(' FOO', 'foo', 'amenity', 'prison', 'in')
184 word_table.add_special(' BAR', 'bar', 'highway', 'road', None)
186 assert word_table.count_special() == 2
188 with analyzer() as anl:
189 anl.update_special_phrases([], True)
191 assert word_table.count_special() == 0
194 def test_update_special_phrases_no_replace(analyzer, word_table):
195 word_table.add_special(' FOO', 'foo', 'amenity', 'prison', 'in')
196 word_table.add_special(' BAR', 'bar', 'highway', 'road', None)
198 assert word_table.count_special() == 2
200 with analyzer() as anl:
201 anl.update_special_phrases([], False)
203 assert word_table.count_special() == 2
206 def test_update_special_phrase_modify(analyzer, word_table):
207 word_table.add_special(' FOO', 'foo', 'amenity', 'prison', 'in')
208 word_table.add_special(' BAR', 'bar', 'highway', 'road', None)
210 assert word_table.count_special() == 2
212 with analyzer() as anl:
213 anl.update_special_phrases([
214 ('prison', 'amenity', 'prison', 'in'),
215 ('bar', 'highway', 'road', '-'),
216 ('garden', 'leisure', 'garden', 'near')
219 assert word_table.get_special() \
220 == {(' PRISON', 'prison', 'amenity', 'prison', 'in'),
221 (' BAR', 'bar', 'highway', 'road', None),
222 (' GARDEN', 'garden', 'leisure', 'garden', 'near')}
225 def test_process_place_names(analyzer, getorcreate_term_id):
226 with analyzer() as anl:
227 info = anl.process_place({'name' : {'name' : 'Soft bAr', 'ref': '34'}})
229 assert info['names'] == '{1,2,3,4,5}'
232 @pytest.mark.parametrize('sep', [',' , ';'])
233 def test_full_names_with_separator(analyzer, getorcreate_term_id, sep):
234 with analyzer() as anl:
235 names = anl._compute_full_names({'name' : sep.join(('New York', 'Big Apple'))})
237 assert names == set(('NEW YORK', 'BIG APPLE'))
240 def test_full_names_with_bracket(analyzer, getorcreate_term_id):
241 with analyzer() as anl:
242 names = anl._compute_full_names({'name' : 'Houseboat (left)'})
244 assert names == set(('HOUSEBOAT (LEFT)', 'HOUSEBOAT'))
247 @pytest.mark.parametrize('pcode', ['12345', 'AB 123', '34-345'])
248 def test_process_place_postcode(analyzer, word_table, pcode):
249 with analyzer() as anl:
250 anl.process_place({'address': {'postcode' : pcode}})
252 assert word_table.get_postcodes() == {pcode, }
255 @pytest.mark.parametrize('pcode', ['12:23', 'ab;cd;f', '123;836'])
256 def test_process_place_bad_postcode(analyzer, word_table, pcode):
257 with analyzer() as anl:
258 anl.process_place({'address': {'postcode' : pcode}})
260 assert not word_table.get_postcodes()
263 @pytest.mark.parametrize('hnr', ['123a', '1', '101'])
264 def test_process_place_housenumbers_simple(analyzer, hnr, getorcreate_hnr_id):
265 with analyzer() as anl:
266 info = anl.process_place({'address': {'housenumber' : hnr}})
268 assert info['hnr'] == hnr.upper()
269 assert info['hnr_tokens'] == "{-1}"
272 def test_process_place_housenumbers_lists(analyzer, getorcreate_hnr_id):
273 with analyzer() as anl:
274 info = anl.process_place({'address': {'conscriptionnumber' : '1; 2;3'}})
276 assert set(info['hnr'].split(';')) == set(('1', '2', '3'))
277 assert info['hnr_tokens'] == "{-1,-2,-3}"
280 def test_process_place_housenumbers_duplicates(analyzer, getorcreate_hnr_id):
281 with analyzer() as anl:
282 info = anl.process_place({'address': {'housenumber' : '134',
283 'conscriptionnumber' : '134',
284 'streetnumber' : '99a'}})
286 assert set(info['hnr'].split(';')) == set(('134', '99A'))
287 assert info['hnr_tokens'] == "{-1,-2}"