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.
13 from nominatim_api import NominatimAPIAsync
14 from nominatim_api.search.query import Phrase, PhraseType, TokenType, BreakType
15 import nominatim_api.search.legacy_tokenizer as tok
16 from nominatim_api.logging import set_log_output, get_and_disable
19 async def add_word(conn, word_id, word_token, word, count):
20 t = conn.t.meta.tables['word']
21 await conn.execute(t.insert(), {'word_id': word_id,
22 'word_token': word_token,
23 'search_name_count': count,
27 async def add_housenumber(conn, word_id, hnr):
28 t = conn.t.meta.tables['word']
29 await conn.execute(t.insert(), {'word_id': word_id,
30 'word_token': ' ' + hnr,
36 async def add_postcode(conn, word_id, postcode):
37 t = conn.t.meta.tables['word']
38 await conn.execute(t.insert(), {'word_id': word_id,
39 'word_token': ' ' + postcode,
45 async def add_special_term(conn, word_id, word_token, cls, typ, op):
46 t = conn.t.meta.tables['word']
47 await conn.execute(t.insert(), {'word_id': word_id,
48 'word_token': word_token,
55 def make_phrase(query):
56 return [Phrase(PhraseType.NONE, s) for s in query.split(',')]
59 @pytest_asyncio.fixture
60 async def conn(table_factory, temp_db_cursor):
61 """ Create an asynchronous SQLAlchemy engine for the test DB.
63 table_factory('nominatim_properties',
64 definition='property TEXT, value TEXT',
65 content=(('tokenizer_maxwordfreq', '10000'), ))
67 definition="""word_id INT, word_token TEXT, word TEXT,
68 class TEXT, type TEXT, country_code TEXT,
69 search_name_count INT, operator TEXT
72 temp_db_cursor.execute("""CREATE OR REPLACE FUNCTION make_standard_name(name TEXT)
73 RETURNS TEXT AS $$ SELECT lower(name); $$ LANGUAGE SQL;""")
75 async with NominatimAPIAsync(environ={}) as api:
76 async with api.begin() as conn:
81 async def test_empty_phrase(conn):
82 ana = await tok.create_query_analyzer(conn)
84 query = await ana.analyze_query([])
86 assert len(query.source) == 0
87 assert query.num_token_slots() == 0
91 async def test_single_phrase_with_unknown_terms(conn):
92 ana = await tok.create_query_analyzer(conn)
94 await add_word(conn, 1, 'foo', 'FOO', 3)
96 query = await ana.analyze_query(make_phrase('foo BAR'))
98 assert len(query.source) == 1
99 assert query.source[0].ptype == PhraseType.NONE
100 assert query.source[0].text == 'foo bar'
102 assert query.num_token_slots() == 2
103 assert len(query.nodes[0].starting) == 1
104 assert not query.nodes[1].starting
108 async def test_multiple_phrases(conn):
109 ana = await tok.create_query_analyzer(conn)
111 await add_word(conn, 1, 'one', 'one', 13)
112 await add_word(conn, 2, 'two', 'two', 45)
113 await add_word(conn, 100, 'one two', 'one two', 3)
114 await add_word(conn, 3, 'three', 'three', 4584)
116 query = await ana.analyze_query(make_phrase('one two,three'))
118 assert len(query.source) == 2
122 async def test_housenumber_token(conn):
123 ana = await tok.create_query_analyzer(conn)
125 await add_housenumber(conn, 556, '45 a')
127 query = await ana.analyze_query(make_phrase('45 A'))
129 assert query.num_token_slots() == 2
130 assert len(query.nodes[0].starting) == 2
132 query.nodes[0].starting.sort(key=lambda tl: tl.end)
134 hn1 = query.nodes[0].starting[0]
135 assert hn1.ttype == TokenType.HOUSENUMBER
137 assert hn1.tokens[0].token == 0
139 hn2 = query.nodes[0].starting[1]
140 assert hn2.ttype == TokenType.HOUSENUMBER
142 assert hn2.tokens[0].token == 556
146 async def test_postcode_token(conn):
147 ana = await tok.create_query_analyzer(conn)
149 await add_postcode(conn, 34, '45ax')
151 query = await ana.analyze_query(make_phrase('45AX'))
153 assert query.num_token_slots() == 1
154 assert [tl.ttype for tl in query.nodes[0].starting] == [TokenType.POSTCODE]
158 async def test_partial_tokens(conn):
159 ana = await tok.create_query_analyzer(conn)
161 await add_word(conn, 1, ' foo', 'foo', 99)
162 await add_word(conn, 1, 'foo', 'FOO', 99)
163 await add_word(conn, 1, 'bar', 'FOO', 990000)
165 query = await ana.analyze_query(make_phrase('foo bar'))
167 assert query.num_token_slots() == 2
169 first = query.nodes[0].starting
170 first.sort(key=lambda tl: tl.tokens[0].penalty)
171 assert [tl.ttype for tl in first] == [TokenType.WORD, TokenType.PARTIAL]
172 assert all(tl.tokens[0].lookup_word == 'foo' for tl in first)
174 second = query.nodes[1].starting
175 assert [tl.ttype for tl in second] == [TokenType.PARTIAL]
176 assert not second[0].tokens[0].is_indexed
180 @pytest.mark.parametrize('term,order', [('23456', ['POSTCODE', 'HOUSENUMBER', 'WORD', 'PARTIAL']),
181 ('3', ['HOUSENUMBER', 'POSTCODE', 'WORD', 'PARTIAL'])
183 async def test_penalty_postcodes_and_housenumbers(conn, term, order):
184 ana = await tok.create_query_analyzer(conn)
186 await add_postcode(conn, 1, term)
187 await add_housenumber(conn, 2, term)
188 await add_word(conn, 3, term, term, 5)
189 await add_word(conn, 4, ' ' + term, term, 1)
191 query = await ana.analyze_query(make_phrase(term))
193 assert query.num_token_slots() == 1
195 torder = [(tl.tokens[0].penalty, tl.ttype.name) for tl in query.nodes[0].starting]
198 assert [t[1] for t in torder] == order
202 async def test_category_words_only_at_beginning(conn):
203 ana = await tok.create_query_analyzer(conn)
205 await add_special_term(conn, 1, 'foo', 'amenity', 'restaurant', 'in')
206 await add_word(conn, 2, ' bar', 'BAR', 1)
208 query = await ana.analyze_query(make_phrase('foo BAR foo'))
210 assert query.num_token_slots() == 3
211 assert len(query.nodes[0].starting) == 1
212 assert query.nodes[0].starting[0].ttype == TokenType.NEAR_ITEM
213 assert not query.nodes[2].starting
217 async def test_qualifier_words(conn):
218 ana = await tok.create_query_analyzer(conn)
220 await add_special_term(conn, 1, 'foo', 'amenity', 'restaurant', '-')
221 await add_word(conn, 2, ' bar', 'w', None)
223 query = await ana.analyze_query(make_phrase('foo BAR foo BAR foo'))
225 assert query.num_token_slots() == 5
226 assert set(t.ttype for t in query.nodes[0].starting) == {TokenType.NEAR_ITEM, TokenType.QUALIFIER}
227 assert set(t.ttype for t in query.nodes[2].starting) == {TokenType.QUALIFIER}
228 assert set(t.ttype for t in query.nodes[4].starting) == {TokenType.NEAR_ITEM, TokenType.QUALIFIER}
232 @pytest.mark.parametrize('logtype', ['text', 'html'])
233 async def test_log_output(conn, logtype):
234 ana = await tok.create_query_analyzer(conn)
236 await add_word(conn, 1, 'foo', 'FOO', 99)
238 set_log_output(logtype)
239 await ana.analyze_query(make_phrase('foo'))
241 assert get_and_disable()