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 Tests for query analyzer for legacy tokenizer.
10 from pathlib import Path
15 from nominatim_api import NominatimAPIAsync
16 from nominatim_api.search.query import Phrase, PhraseType, TokenType, BreakType
17 import nominatim_api.search.legacy_tokenizer as tok
18 from nominatim_api.logging import set_log_output, get_and_disable
21 async def add_word(conn, word_id, word_token, word, count):
22 t = conn.t.meta.tables['word']
23 await conn.execute(t.insert(), {'word_id': word_id,
24 'word_token': word_token,
25 'search_name_count': count,
29 async def add_housenumber(conn, word_id, hnr):
30 t = conn.t.meta.tables['word']
31 await conn.execute(t.insert(), {'word_id': word_id,
32 'word_token': ' ' + hnr,
38 async def add_postcode(conn, word_id, postcode):
39 t = conn.t.meta.tables['word']
40 await conn.execute(t.insert(), {'word_id': word_id,
41 'word_token': ' ' + postcode,
47 async def add_special_term(conn, word_id, word_token, cls, typ, op):
48 t = conn.t.meta.tables['word']
49 await conn.execute(t.insert(), {'word_id': word_id,
50 'word_token': word_token,
57 def make_phrase(query):
58 return [Phrase(PhraseType.NONE, s) for s in query.split(',')]
61 @pytest_asyncio.fixture
62 async def conn(table_factory, temp_db_cursor):
63 """ Create an asynchronous SQLAlchemy engine for the test DB.
65 table_factory('nominatim_properties',
66 definition='property TEXT, value TEXT',
67 content=(('tokenizer_maxwordfreq', '10000'), ))
69 definition="""word_id INT, word_token TEXT, word TEXT,
70 class TEXT, type TEXT, country_code TEXT,
71 search_name_count INT, operator TEXT
74 temp_db_cursor.execute("""CREATE OR REPLACE FUNCTION make_standard_name(name TEXT)
75 RETURNS TEXT AS $$ SELECT lower(name); $$ LANGUAGE SQL;""")
77 async with NominatimAPIAsync(Path('/invalid'), {}) as api:
78 async with api.begin() as conn:
83 async def test_empty_phrase(conn):
84 ana = await tok.create_query_analyzer(conn)
86 query = await ana.analyze_query([])
88 assert len(query.source) == 0
89 assert query.num_token_slots() == 0
93 async def test_single_phrase_with_unknown_terms(conn):
94 ana = await tok.create_query_analyzer(conn)
96 await add_word(conn, 1, 'foo', 'FOO', 3)
98 query = await ana.analyze_query(make_phrase('foo BAR'))
100 assert len(query.source) == 1
101 assert query.source[0].ptype == PhraseType.NONE
102 assert query.source[0].text == 'foo bar'
104 assert query.num_token_slots() == 2
105 assert len(query.nodes[0].starting) == 1
106 assert not query.nodes[1].starting
110 async def test_multiple_phrases(conn):
111 ana = await tok.create_query_analyzer(conn)
113 await add_word(conn, 1, 'one', 'one', 13)
114 await add_word(conn, 2, 'two', 'two', 45)
115 await add_word(conn, 100, 'one two', 'one two', 3)
116 await add_word(conn, 3, 'three', 'three', 4584)
118 query = await ana.analyze_query(make_phrase('one two,three'))
120 assert len(query.source) == 2
124 async def test_housenumber_token(conn):
125 ana = await tok.create_query_analyzer(conn)
127 await add_housenumber(conn, 556, '45 a')
129 query = await ana.analyze_query(make_phrase('45 A'))
131 assert query.num_token_slots() == 2
132 assert len(query.nodes[0].starting) == 2
134 query.nodes[0].starting.sort(key=lambda tl: tl.end)
136 hn1 = query.nodes[0].starting[0]
137 assert hn1.ttype == TokenType.HOUSENUMBER
139 assert hn1.tokens[0].token == 0
141 hn2 = query.nodes[0].starting[1]
142 assert hn2.ttype == TokenType.HOUSENUMBER
144 assert hn2.tokens[0].token == 556
148 async def test_postcode_token(conn):
149 ana = await tok.create_query_analyzer(conn)
151 await add_postcode(conn, 34, '45ax')
153 query = await ana.analyze_query(make_phrase('45AX'))
155 assert query.num_token_slots() == 1
156 assert [tl.ttype for tl in query.nodes[0].starting] == [TokenType.POSTCODE]
160 async def test_partial_tokens(conn):
161 ana = await tok.create_query_analyzer(conn)
163 await add_word(conn, 1, ' foo', 'foo', 99)
164 await add_word(conn, 1, 'foo', 'FOO', 99)
165 await add_word(conn, 1, 'bar', 'FOO', 990000)
167 query = await ana.analyze_query(make_phrase('foo bar'))
169 assert query.num_token_slots() == 2
171 first = query.nodes[0].starting
172 first.sort(key=lambda tl: tl.tokens[0].penalty)
173 assert [tl.ttype for tl in first] == [TokenType.WORD, TokenType.PARTIAL]
174 assert all(tl.tokens[0].lookup_word == 'foo' for tl in first)
176 second = query.nodes[1].starting
177 assert [tl.ttype for tl in second] == [TokenType.PARTIAL]
178 assert not second[0].tokens[0].is_indexed
182 @pytest.mark.parametrize('term,order', [('23456', ['POSTCODE', 'HOUSENUMBER', 'WORD', 'PARTIAL']),
183 ('3', ['HOUSENUMBER', 'POSTCODE', 'WORD', 'PARTIAL'])
185 async def test_penalty_postcodes_and_housenumbers(conn, term, order):
186 ana = await tok.create_query_analyzer(conn)
188 await add_postcode(conn, 1, term)
189 await add_housenumber(conn, 2, term)
190 await add_word(conn, 3, term, term, 5)
191 await add_word(conn, 4, ' ' + term, term, 1)
193 query = await ana.analyze_query(make_phrase(term))
195 assert query.num_token_slots() == 1
197 torder = [(tl.tokens[0].penalty, tl.ttype.name) for tl in query.nodes[0].starting]
200 assert [t[1] for t in torder] == order
204 async def test_category_words_only_at_beginning(conn):
205 ana = await tok.create_query_analyzer(conn)
207 await add_special_term(conn, 1, 'foo', 'amenity', 'restaurant', 'in')
208 await add_word(conn, 2, ' bar', 'BAR', 1)
210 query = await ana.analyze_query(make_phrase('foo BAR foo'))
212 assert query.num_token_slots() == 3
213 assert len(query.nodes[0].starting) == 1
214 assert query.nodes[0].starting[0].ttype == TokenType.NEAR_ITEM
215 assert not query.nodes[2].starting
219 async def test_qualifier_words(conn):
220 ana = await tok.create_query_analyzer(conn)
222 await add_special_term(conn, 1, 'foo', 'amenity', 'restaurant', '-')
223 await add_word(conn, 2, ' bar', 'w', None)
225 query = await ana.analyze_query(make_phrase('foo BAR foo BAR foo'))
227 assert query.num_token_slots() == 5
228 assert set(t.ttype for t in query.nodes[0].starting) == {TokenType.NEAR_ITEM, TokenType.QUALIFIER}
229 assert set(t.ttype for t in query.nodes[2].starting) == {TokenType.QUALIFIER}
230 assert set(t.ttype for t in query.nodes[4].starting) == {TokenType.NEAR_ITEM, TokenType.QUALIFIER}
234 @pytest.mark.parametrize('logtype', ['text', 'html'])
235 async def test_log_output(conn, logtype):
236 ana = await tok.create_query_analyzer(conn)
238 await add_word(conn, 1, 'foo', 'FOO', 99)
240 set_log_output(logtype)
241 await ana.analyze_query(make_phrase('foo'))
243 assert get_and_disable()