X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/31412e06740727695c5d9512e0cd59c0dd683322..e4295dba10bdb05045e35c772b3d8ca3cb042fd1:/src/nominatim_api/search/token_assignment.py
diff --git a/src/nominatim_api/search/token_assignment.py b/src/nominatim_api/search/token_assignment.py
index cfd9efe5..8d25aa8f 100644
--- a/src/nominatim_api/search/token_assignment.py
+++ b/src/nominatim_api/search/token_assignment.py
@@ -269,10 +269,9 @@ class _TokenSequence:
#
, should give preference to address search
if base.postcode.start == 0:
penalty = self.penalty
- self.direction = -1 # name searches are only possible backwards
else:
penalty = self.penalty + 0.1
- self.direction = 1 # name searches are only possible forwards
+ penalty += 0.1 * max(0, len(base.address) - 1)
yield dataclasses.replace(base, penalty=penalty)
def _get_assignments_address_forward(self, base: TokenAssignment,
@@ -282,6 +281,11 @@ class _TokenSequence:
"""
first = base.address[0]
+ # The postcode must come after the name.
+ if base.postcode and base.postcode < first:
+ log().var_dump('skip forward', (base.postcode, first))
+ return
+
log().comment('first word = name')
yield dataclasses.replace(base, penalty=self.penalty,
name=first, address=base.address[1:])
@@ -293,7 +297,7 @@ class _TokenSequence:
# * the containing phrase is strictly typed
if (base.housenumber and first.end < base.housenumber.start)\
or (base.qualifier and base.qualifier > first)\
- or (query.nodes[first.start].ptype != qmod.PhraseType.NONE):
+ or (query.nodes[first.start].ptype != qmod.PHRASE_ANY):
return
penalty = self.penalty
@@ -317,7 +321,12 @@ class _TokenSequence:
"""
last = base.address[-1]
- if self.direction == -1 or len(base.address) > 1:
+ # The postcode must come before the name for backward direction.
+ if base.postcode and base.postcode > last:
+ log().var_dump('skip backward', (base.postcode, last))
+ return
+
+ if self.direction == -1 or len(base.address) > 1 or base.postcode:
log().comment('last word = name')
yield dataclasses.replace(base, penalty=self.penalty,
name=last, address=base.address[:-1])
@@ -329,7 +338,7 @@ class _TokenSequence:
# * the containing phrase is strictly typed
if (base.housenumber and last.start > base.housenumber.end)\
or (base.qualifier and base.qualifier < last)\
- or (query.nodes[last.start].ptype != qmod.PhraseType.NONE):
+ or (query.nodes[last.start].ptype != qmod.PHRASE_ANY):
return
penalty = self.penalty
@@ -393,7 +402,7 @@ def yield_token_assignments(query: qmod.QueryStruct) -> Iterator[TokenAssignment
another. It does not include penalties for transitions within a
type.
"""
- todo = [_TokenSequence([], direction=0 if query.source[0].ptype == qmod.PhraseType.NONE else 1)]
+ todo = [_TokenSequence([], direction=0 if query.source[0].ptype == qmod.PHRASE_ANY else 1)]
while todo:
state = todo.pop()