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_add_postcodes_from_db(analyzer, word_table, table_factory, temp_db_cursor):
145 table_factory('location_postcode', 'postcode TEXT',
146 content=(('1234',), ('12 34',), ('AB23',), ('1234',)))
148 with analyzer() as a:
149 a.add_postcodes_from_db()
151 assert temp_db_cursor.row_set("""SELECT word, word_token from word
153 == set((('1234', ' 1234'), ('12 34', ' 12 34'), ('AB23', ' AB23')))
156 def test_update_special_phrase_empty_table(analyzer, word_table, temp_db_cursor):
157 with analyzer() as a:
158 a.update_special_phrases([
159 ("König bei", "amenity", "royal", "near"),
160 ("Könige", "amenity", "royal", "-"),
161 ("street", "highway", "primary", "in")
164 assert temp_db_cursor.row_set("""SELECT word_token, word, class, type, operator
165 FROM word WHERE class != 'place'""") \
166 == set(((' KÖNIG BEI', 'könig bei', 'amenity', 'royal', 'near'),
167 (' KÖNIGE', 'könige', 'amenity', 'royal', None),
168 (' ST', 'street', 'highway', 'primary', 'in')))
171 def test_update_special_phrase_delete_all(analyzer, word_table, temp_db_cursor):
172 temp_db_cursor.execute("""INSERT INTO word (word_token, word, class, type, operator)
173 VALUES (' FOO', 'foo', 'amenity', 'prison', 'in'),
174 (' BAR', 'bar', 'highway', 'road', null)""")
176 assert 2 == temp_db_cursor.scalar("SELECT count(*) FROM word WHERE class != 'place'""")
178 with analyzer() as a:
179 a.update_special_phrases([])
181 assert 0 == temp_db_cursor.scalar("SELECT count(*) FROM word WHERE class != 'place'""")
184 def test_update_special_phrase_modify(analyzer, word_table, temp_db_cursor):
185 temp_db_cursor.execute("""INSERT INTO word (word_token, word, class, type, operator)
186 VALUES (' FOO', 'foo', 'amenity', 'prison', 'in'),
187 (' BAR', 'bar', 'highway', 'road', null)""")
189 assert 2 == temp_db_cursor.scalar("SELECT count(*) FROM word WHERE class != 'place'""")
191 with analyzer() as a:
192 a.update_special_phrases([
193 ('prison', 'amenity', 'prison', 'in'),
194 ('bar', 'highway', 'road', '-'),
195 ('garden', 'leisure', 'garden', 'near')
198 assert temp_db_cursor.row_set("""SELECT word_token, word, class, type, operator
199 FROM word WHERE class != 'place'""") \
200 == set(((' PRISON', 'prison', 'amenity', 'prison', 'in'),
201 (' BAR', 'bar', 'highway', 'road', None),
202 (' GARDEN', 'garden', 'leisure', 'garden', 'near')))
205 def test_process_place_names(analyzer, getorcreate_term_id):
207 with analyzer() as a:
208 info = a.process_place({'name' : {'name' : 'Soft bAr', 'ref': '34'}})
210 assert info['names'] == '{1,2,3,4,5,6}'
213 @pytest.mark.parametrize('pc', ['12345', 'AB 123', '34-345'])
214 def test_process_place_postcode(analyzer, temp_db_cursor, pc):
215 with analyzer() as a:
216 info = a.process_place({'address': {'postcode' : pc}})
218 assert temp_db_cursor.row_set("""SELECT word FROM word
219 WHERE class = 'place' and type = 'postcode'""") \
223 @pytest.mark.parametrize('pc', ['12:23', 'ab;cd;f', '123;836'])
224 def test_process_place_bad_postcode(analyzer, temp_db_cursor, pc):
225 with analyzer() as a:
226 info = a.process_place({'address': {'postcode' : pc}})
228 assert 0 == temp_db_cursor.scalar("""SELECT count(*) FROM word
229 WHERE class = 'place' and type = 'postcode'""")
232 @pytest.mark.parametrize('hnr', ['123a', '1', '101'])
233 def test_process_place_housenumbers_simple(analyzer, hnr, getorcreate_hnr_id):
234 with analyzer() as a:
235 info = a.process_place({'address': {'housenumber' : hnr}})
237 assert info['hnr'] == hnr.upper()
238 assert info['hnr_tokens'] == "{-1}"
241 def test_process_place_housenumbers_lists(analyzer, getorcreate_hnr_id):
242 with analyzer() as a:
243 info = a.process_place({'address': {'conscriptionnumber' : '1; 2;3'}})
245 assert set(info['hnr'].split(';')) == set(('1', '2', '3'))
246 assert info['hnr_tokens'] == "{-1,-2,-3}"
249 def test_process_place_housenumbers_duplicates(analyzer, getorcreate_hnr_id):
250 with analyzer() as a:
251 info = a.process_place({'address': {'housenumber' : '134',
252 'conscriptionnumber' : '134',
253 'streetnumber' : '99a'}})
255 assert set(info['hnr'].split(';')) == set(('134', '99A'))
256 assert info['hnr_tokens'] == "{-1,-2}"