1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2023 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Test for creation of token assignments from tokenized queries.
12 from nominatim.api.search.query import QueryStruct, Phrase, PhraseType, BreakType, TokenType, TokenRange, Token
13 from nominatim.api.search.token_assignment import yield_token_assignments, TokenAssignment, PENALTY_TOKENCHANGE
16 def get_category(self):
20 def make_query(*args):
22 dummy = MyToken(3.0, 45, 1, 'foo', True)
24 for btype, ptype, tlist in args:
26 q = QueryStruct([Phrase(ptype, '')])
28 q.add_node(btype, ptype)
30 start = len(q.nodes) - 1
31 for end, ttype in tlist:
32 q.add_token(TokenRange(start, end), ttype, [dummy])
34 q.add_node(BreakType.END, PhraseType.NONE)
39 def check_assignments(actual, *expected):
41 for assignment in actual:
42 assert assignment in todo, f"Unexpected assignment: {assignment}"
43 todo.remove(assignment)
45 assert not todo, f"Missing assignments: {expected}"
48 def test_query_with_missing_tokens():
49 q = QueryStruct([Phrase(PhraseType.NONE, '')])
50 q.add_node(BreakType.END, PhraseType.NONE)
52 assert list(yield_token_assignments(q)) == []
55 def test_one_word_query():
56 q = make_query((BreakType.START, PhraseType.NONE,
57 [(1, TokenType.PARTIAL),
59 (1, TokenType.HOUSENUMBER)]))
61 res = list(yield_token_assignments(q))
62 assert res == [TokenAssignment(name=TokenRange(0, 1))]
65 def test_single_postcode():
66 q = make_query((BreakType.START, PhraseType.NONE,
67 [(1, TokenType.POSTCODE)]))
69 res = list(yield_token_assignments(q))
70 assert res == [TokenAssignment(postcode=TokenRange(0, 1))]
73 def test_single_country_name():
74 q = make_query((BreakType.START, PhraseType.NONE,
75 [(1, TokenType.COUNTRY)]))
77 res = list(yield_token_assignments(q))
78 assert res == [TokenAssignment(country=TokenRange(0, 1))]
81 def test_single_word_poi_search():
82 q = make_query((BreakType.START, PhraseType.NONE,
83 [(1, TokenType.CATEGORY),
84 (1, TokenType.QUALIFIER)]))
86 res = list(yield_token_assignments(q))
87 assert res == [TokenAssignment(category=TokenRange(0, 1))]
90 @pytest.mark.parametrize('btype', [BreakType.WORD, BreakType.PART, BreakType.TOKEN])
91 def test_multiple_simple_words(btype):
92 q = make_query((BreakType.START, PhraseType.NONE, [(1, TokenType.PARTIAL)]),
93 (btype, PhraseType.NONE, [(2, TokenType.PARTIAL)]),
94 (btype, PhraseType.NONE, [(3, TokenType.PARTIAL)]))
96 penalty = PENALTY_TOKENCHANGE[btype]
98 check_assignments(yield_token_assignments(q),
99 TokenAssignment(name=TokenRange(0, 3)),
100 TokenAssignment(penalty=penalty, name=TokenRange(0, 2),
101 address=[TokenRange(2, 3)]),
102 TokenAssignment(penalty=penalty, name=TokenRange(0, 1),
103 address=[TokenRange(1, 3)]),
104 TokenAssignment(penalty=penalty, name=TokenRange(1, 3),
105 address=[TokenRange(0, 1)]),
106 TokenAssignment(penalty=penalty, name=TokenRange(2, 3),
107 address=[TokenRange(0, 2)])
111 def test_multiple_words_respect_phrase_break():
112 q = make_query((BreakType.START, PhraseType.NONE, [(1, TokenType.PARTIAL)]),
113 (BreakType.PHRASE, PhraseType.NONE, [(2, TokenType.PARTIAL)]))
115 check_assignments(yield_token_assignments(q),
116 TokenAssignment(name=TokenRange(0, 1),
117 address=[TokenRange(1, 2)]),
118 TokenAssignment(name=TokenRange(1, 2),
119 address=[TokenRange(0, 1)]))
122 def test_housenumber_and_street():
123 q = make_query((BreakType.START, PhraseType.NONE, [(1, TokenType.HOUSENUMBER)]),
124 (BreakType.PHRASE, PhraseType.NONE, [(2, TokenType.PARTIAL)]))
126 check_assignments(yield_token_assignments(q),
127 TokenAssignment(name=TokenRange(1, 2),
128 housenumber=TokenRange(0, 1)))
131 def test_housenumber_and_street_backwards():
132 q = make_query((BreakType.START, PhraseType.NONE, [(1, TokenType.PARTIAL)]),
133 (BreakType.PHRASE, PhraseType.NONE, [(2, TokenType.HOUSENUMBER)]))
135 check_assignments(yield_token_assignments(q),
136 TokenAssignment(name=TokenRange(0, 1),
137 housenumber=TokenRange(1, 2)))
140 def test_housenumber_and_postcode():
141 q = make_query((BreakType.START, PhraseType.NONE, [(1, TokenType.PARTIAL)]),
142 (BreakType.WORD, PhraseType.NONE, [(2, TokenType.HOUSENUMBER)]),
143 (BreakType.WORD, PhraseType.NONE, [(3, TokenType.PARTIAL)]),
144 (BreakType.WORD, PhraseType.NONE, [(4, TokenType.POSTCODE)]))
146 check_assignments(yield_token_assignments(q),
147 TokenAssignment(penalty=pytest.approx(0.3),
148 name=TokenRange(0, 1),
149 housenumber=TokenRange(1, 2),
150 address=[TokenRange(2, 3)],
151 postcode=TokenRange(3, 4)))
153 def test_postcode_and_housenumber():
154 q = make_query((BreakType.START, PhraseType.NONE, [(1, TokenType.PARTIAL)]),
155 (BreakType.WORD, PhraseType.NONE, [(2, TokenType.POSTCODE)]),
156 (BreakType.WORD, PhraseType.NONE, [(3, TokenType.PARTIAL)]),
157 (BreakType.WORD, PhraseType.NONE, [(4, TokenType.HOUSENUMBER)]))
159 check_assignments(yield_token_assignments(q),
160 TokenAssignment(penalty=pytest.approx(0.3),
161 name=TokenRange(2, 3),
162 housenumber=TokenRange(3, 4),
163 address=[TokenRange(0, 1)],
164 postcode=TokenRange(1, 2)))
167 def test_country_housenumber_postcode():
168 q = make_query((BreakType.START, PhraseType.NONE, [(1, TokenType.COUNTRY)]),
169 (BreakType.WORD, PhraseType.NONE, [(2, TokenType.PARTIAL)]),
170 (BreakType.WORD, PhraseType.NONE, [(3, TokenType.HOUSENUMBER)]),
171 (BreakType.WORD, PhraseType.NONE, [(4, TokenType.POSTCODE)]))
173 check_assignments(yield_token_assignments(q))
176 @pytest.mark.parametrize('ttype', [TokenType.POSTCODE, TokenType.COUNTRY,
177 TokenType.CATEGORY, TokenType.QUALIFIER])
178 def test_housenumber_with_only_special_terms(ttype):
179 q = make_query((BreakType.START, PhraseType.NONE, [(1, TokenType.HOUSENUMBER)]),
180 (BreakType.WORD, PhraseType.NONE, [(2, ttype)]))
182 check_assignments(yield_token_assignments(q))
185 @pytest.mark.parametrize('ttype', [TokenType.POSTCODE, TokenType.HOUSENUMBER, TokenType.COUNTRY])
186 def test_multiple_special_tokens(ttype):
187 q = make_query((BreakType.START, PhraseType.NONE, [(1, ttype)]),
188 (BreakType.PHRASE, PhraseType.NONE, [(2, TokenType.PARTIAL)]),
189 (BreakType.PHRASE, PhraseType.NONE, [(3, ttype)]))
191 check_assignments(yield_token_assignments(q))
194 def test_housenumber_many_phrases():
195 q = make_query((BreakType.START, PhraseType.NONE, [(1, TokenType.PARTIAL)]),
196 (BreakType.PHRASE, PhraseType.NONE, [(2, TokenType.PARTIAL)]),
197 (BreakType.PHRASE, PhraseType.NONE, [(3, TokenType.PARTIAL)]),
198 (BreakType.PHRASE, PhraseType.NONE, [(4, TokenType.HOUSENUMBER)]),
199 (BreakType.WORD, PhraseType.NONE, [(5, TokenType.PARTIAL)]))
201 check_assignments(yield_token_assignments(q),
202 TokenAssignment(penalty=0.1,
203 name=TokenRange(4, 5),
204 housenumber=TokenRange(3, 4),\
205 address=[TokenRange(0, 1), TokenRange(1, 2),
209 def test_country_at_beginning():
210 q = make_query((BreakType.START, PhraseType.NONE, [(1, TokenType.COUNTRY)]),
211 (BreakType.WORD, PhraseType.NONE, [(2, TokenType.PARTIAL)]))
213 check_assignments(yield_token_assignments(q),
214 TokenAssignment(penalty=0.1, name=TokenRange(1, 2),
215 country=TokenRange(0, 1)))
218 def test_country_at_end():
219 q = make_query((BreakType.START, PhraseType.NONE, [(1, TokenType.PARTIAL)]),
220 (BreakType.WORD, PhraseType.NONE, [(2, TokenType.COUNTRY)]))
222 check_assignments(yield_token_assignments(q),
223 TokenAssignment(penalty=0.1, name=TokenRange(0, 1),
224 country=TokenRange(1, 2)))
227 def test_country_in_middle():
228 q = make_query((BreakType.START, PhraseType.NONE, [(1, TokenType.PARTIAL)]),
229 (BreakType.WORD, PhraseType.NONE, [(2, TokenType.COUNTRY)]),
230 (BreakType.WORD, PhraseType.NONE, [(3, TokenType.PARTIAL)]))
232 check_assignments(yield_token_assignments(q))
235 def test_postcode_with_designation():
236 q = make_query((BreakType.START, PhraseType.NONE, [(1, TokenType.POSTCODE)]),
237 (BreakType.PHRASE, PhraseType.NONE, [(2, TokenType.PARTIAL)]))
239 check_assignments(yield_token_assignments(q),
240 TokenAssignment(name=TokenRange(1, 2),
241 postcode=TokenRange(0, 1)),
242 TokenAssignment(postcode=TokenRange(0, 1),
243 address=[TokenRange(1, 2)]))
246 def test_postcode_with_designation_backwards():
247 q = make_query((BreakType.START, PhraseType.NONE, [(1, TokenType.PARTIAL)]),
248 (BreakType.PHRASE, PhraseType.NONE, [(2, TokenType.POSTCODE)]))
250 check_assignments(yield_token_assignments(q),
251 TokenAssignment(name=TokenRange(0, 1),
252 postcode=TokenRange(1, 2)),
253 TokenAssignment(postcode=TokenRange(1, 2),
254 address=[TokenRange(0, 1)]))
257 def test_category_at_beginning():
258 q = make_query((BreakType.START, PhraseType.NONE, [(1, TokenType.CATEGORY)]),
259 (BreakType.WORD, PhraseType.NONE, [(2, TokenType.PARTIAL)]))
261 check_assignments(yield_token_assignments(q),
262 TokenAssignment(penalty=0.1, name=TokenRange(1, 2),
263 category=TokenRange(0, 1)))
266 def test_category_at_end():
267 q = make_query((BreakType.START, PhraseType.NONE, [(1, TokenType.PARTIAL)]),
268 (BreakType.WORD, PhraseType.NONE, [(2, TokenType.CATEGORY)]))
270 check_assignments(yield_token_assignments(q),
271 TokenAssignment(penalty=0.1, name=TokenRange(0, 1),
272 category=TokenRange(1, 2)))
275 def test_category_in_middle():
276 q = make_query((BreakType.START, PhraseType.NONE, [(1, TokenType.PARTIAL)]),
277 (BreakType.WORD, PhraseType.NONE, [(2, TokenType.CATEGORY)]),
278 (BreakType.WORD, PhraseType.NONE, [(3, TokenType.PARTIAL)]))
280 check_assignments(yield_token_assignments(q))
283 def test_qualifier_at_beginning():
284 q = make_query((BreakType.START, PhraseType.NONE, [(1, TokenType.QUALIFIER)]),
285 (BreakType.WORD, PhraseType.NONE, [(2, TokenType.PARTIAL)]),
286 (BreakType.WORD, PhraseType.NONE, [(3, TokenType.PARTIAL)]))
289 check_assignments(yield_token_assignments(q),
290 TokenAssignment(penalty=0.1, name=TokenRange(1, 3),
291 qualifier=TokenRange(0, 1)),
292 TokenAssignment(penalty=0.2, name=TokenRange(1, 2),
293 qualifier=TokenRange(0, 1),
294 address=[TokenRange(2, 3)]))
297 def test_qualifier_after_name():
298 q = make_query((BreakType.START, PhraseType.NONE, [(1, TokenType.PARTIAL)]),
299 (BreakType.WORD, PhraseType.NONE, [(2, TokenType.PARTIAL)]),
300 (BreakType.WORD, PhraseType.NONE, [(3, TokenType.QUALIFIER)]),
301 (BreakType.WORD, PhraseType.NONE, [(4, TokenType.PARTIAL)]),
302 (BreakType.WORD, PhraseType.NONE, [(5, TokenType.PARTIAL)]))
305 check_assignments(yield_token_assignments(q),
306 TokenAssignment(penalty=0.2, name=TokenRange(0, 2),
307 qualifier=TokenRange(2, 3),
308 address=[TokenRange(3, 5)]),
309 TokenAssignment(penalty=0.2, name=TokenRange(3, 5),
310 qualifier=TokenRange(2, 3),
311 address=[TokenRange(0, 2)]))
314 def test_qualifier_before_housenumber():
315 q = make_query((BreakType.START, PhraseType.NONE, [(1, TokenType.QUALIFIER)]),
316 (BreakType.WORD, PhraseType.NONE, [(2, TokenType.HOUSENUMBER)]),
317 (BreakType.WORD, PhraseType.NONE, [(3, TokenType.PARTIAL)]))
319 check_assignments(yield_token_assignments(q))
322 def test_qualifier_after_housenumber():
323 q = make_query((BreakType.START, PhraseType.NONE, [(1, TokenType.HOUSENUMBER)]),
324 (BreakType.WORD, PhraseType.NONE, [(2, TokenType.QUALIFIER)]),
325 (BreakType.WORD, PhraseType.NONE, [(3, TokenType.PARTIAL)]))
327 check_assignments(yield_token_assignments(q))