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 ICU tokenizer.
13 from nominatim_api import NominatimAPIAsync
14 from nominatim_api.search.query import Phrase, PhraseType, TokenType, BreakType
15 import nominatim_api.search.icu_tokenizer as tok
16 from nominatim_api.logging import set_log_output, get_and_disable
18 async def add_word(conn, word_id, word_token, wtype, word, info = None):
19 t = conn.t.meta.tables['word']
20 await conn.execute(t.insert(), {'word_id': word_id,
21 'word_token': word_token,
27 def make_phrase(query):
28 return [Phrase(PhraseType.NONE, s) for s in query.split(',')]
30 @pytest_asyncio.fixture
31 async def conn(table_factory):
32 """ Create an asynchronous SQLAlchemy engine for the test DB.
34 table_factory('nominatim_properties',
35 definition='property TEXT, value TEXT',
36 content=(('tokenizer_import_normalisation', ':: lower();'),
37 ('tokenizer_import_transliteration', "'1' > '/1/'; 'ä' > 'ä '")))
39 definition='word_id INT, word_token TEXT, type TEXT, word TEXT, info JSONB')
41 async with NominatimAPIAsync() as api:
42 async with api.begin() as conn:
47 async def test_empty_phrase(conn):
48 ana = await tok.create_query_analyzer(conn)
50 query = await ana.analyze_query([])
52 assert len(query.source) == 0
53 assert query.num_token_slots() == 0
57 async def test_single_phrase_with_unknown_terms(conn):
58 ana = await tok.create_query_analyzer(conn)
60 await add_word(conn, 1, 'foo', 'w', 'FOO')
62 query = await ana.analyze_query(make_phrase('foo BAR'))
64 assert len(query.source) == 1
65 assert query.source[0].ptype == PhraseType.NONE
66 assert query.source[0].text == 'foo bar'
68 assert query.num_token_slots() == 2
69 assert len(query.nodes[0].starting) == 1
70 assert not query.nodes[1].starting
74 async def test_multiple_phrases(conn):
75 ana = await tok.create_query_analyzer(conn)
77 await add_word(conn, 1, 'one', 'w', 'one')
78 await add_word(conn, 2, 'two', 'w', 'two')
79 await add_word(conn, 100, 'one two', 'W', 'one two')
80 await add_word(conn, 3, 'three', 'w', 'three')
82 query = await ana.analyze_query(make_phrase('one two,three'))
84 assert len(query.source) == 2
88 async def test_splitting_in_transliteration(conn):
89 ana = await tok.create_query_analyzer(conn)
91 await add_word(conn, 1, 'mä', 'W', 'ma')
92 await add_word(conn, 2, 'fo', 'W', 'fo')
94 query = await ana.analyze_query(make_phrase('mäfo'))
96 assert query.num_token_slots() == 2
97 assert query.nodes[0].starting
98 assert query.nodes[1].starting
99 assert query.nodes[1].btype == BreakType.TOKEN
103 @pytest.mark.parametrize('term,order', [('23456', ['POSTCODE', 'HOUSENUMBER', 'WORD', 'PARTIAL']),
104 ('3', ['HOUSENUMBER', 'POSTCODE', 'WORD', 'PARTIAL'])
106 async def test_penalty_postcodes_and_housenumbers(conn, term, order):
107 ana = await tok.create_query_analyzer(conn)
109 await add_word(conn, 1, term, 'P', None)
110 await add_word(conn, 2, term, 'H', term)
111 await add_word(conn, 3, term, 'w', term)
112 await add_word(conn, 4, term, 'W', term)
114 query = await ana.analyze_query(make_phrase(term))
116 assert query.num_token_slots() == 1
118 torder = [(tl.tokens[0].penalty, tl.ttype.name) for tl in query.nodes[0].starting]
121 assert [t[1] for t in torder] == order
124 async def test_category_words_only_at_beginning(conn):
125 ana = await tok.create_query_analyzer(conn)
127 await add_word(conn, 1, 'foo', 'S', 'FOO', {'op': 'in'})
128 await add_word(conn, 2, 'bar', 'w', 'BAR')
130 query = await ana.analyze_query(make_phrase('foo BAR foo'))
132 assert query.num_token_slots() == 3
133 assert len(query.nodes[0].starting) == 1
134 assert query.nodes[0].starting[0].ttype == TokenType.NEAR_ITEM
135 assert not query.nodes[2].starting
139 async def test_freestanding_qualifier_words_become_category(conn):
140 ana = await tok.create_query_analyzer(conn)
142 await add_word(conn, 1, 'foo', 'S', 'FOO', {'op': '-'})
144 query = await ana.analyze_query(make_phrase('foo'))
146 assert query.num_token_slots() == 1
147 assert len(query.nodes[0].starting) == 1
148 assert query.nodes[0].starting[0].ttype == TokenType.NEAR_ITEM
152 async def test_qualifier_words(conn):
153 ana = await tok.create_query_analyzer(conn)
155 await add_word(conn, 1, 'foo', 'S', None, {'op': '-'})
156 await add_word(conn, 2, 'bar', 'w', None)
158 query = await ana.analyze_query(make_phrase('foo BAR foo BAR foo'))
160 assert query.num_token_slots() == 5
161 assert set(t.ttype for t in query.nodes[0].starting) == {TokenType.QUALIFIER}
162 assert set(t.ttype for t in query.nodes[2].starting) == {TokenType.QUALIFIER}
163 assert set(t.ttype for t in query.nodes[4].starting) == {TokenType.QUALIFIER}
167 async def test_add_unknown_housenumbers(conn):
168 ana = await tok.create_query_analyzer(conn)
170 await add_word(conn, 1, '23', 'H', '23')
172 query = await ana.analyze_query(make_phrase('466 23 99834 34a'))
174 assert query.num_token_slots() == 4
175 assert query.nodes[0].starting[0].ttype == TokenType.HOUSENUMBER
176 assert len(query.nodes[0].starting[0].tokens) == 1
177 assert query.nodes[0].starting[0].tokens[0].token == 0
178 assert query.nodes[1].starting[0].ttype == TokenType.HOUSENUMBER
179 assert len(query.nodes[1].starting[0].tokens) == 1
180 assert query.nodes[1].starting[0].tokens[0].token == 1
181 assert not query.nodes[2].starting
182 assert not query.nodes[3].starting
186 @pytest.mark.parametrize('logtype', ['text', 'html'])
187 async def test_log_output(conn, logtype):
189 ana = await tok.create_query_analyzer(conn)
191 await add_word(conn, 1, 'foo', 'w', 'FOO')
193 set_log_output(logtype)
194 await ana.analyze_query(make_phrase('foo'))
196 assert get_and_disable()