]> git.openstreetmap.org Git - nominatim.git/blobdiff - nominatim/api/search/token_assignment.py
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / nominatim / api / search / token_assignment.py
index d94d69039f0f602ab8c0aeda9e5c971a881353fd..95eb7f7059cd4c3339cbb75c903be6df536cd41d 100644 (file)
@@ -72,7 +72,7 @@ class TokenAssignment: # pylint: disable=too-many-instance-attributes
 
 
 class _TokenSequence:
-    """ Working state used to put together the token assignements.
+    """ Working state used to put together the token assignments.
 
         Represents an intermediate state while traversing the tokenized
         query.
@@ -132,6 +132,11 @@ class _TokenSequence:
 
         # Name tokens are always acceptable and don't change direction
         if ttype == qmod.TokenType.PARTIAL:
+            # qualifiers cannot appear in the middle of the query. They need
+            # to be near the next phrase.
+            if self.direction == -1 \
+               and any(t.ttype == qmod.TokenType.QUALIFIER for t in self.seq[:-1]):
+                return None
             return self.direction
 
         # Other tokens may only appear once
@@ -220,23 +225,24 @@ class _TokenSequence:
 
 
     def _adapt_penalty_from_priors(self, priors: int, new_dir: int) -> bool:
-        if priors == 2:
-            self.penalty += 1.0
-        elif priors > 2:
+        if priors >= 2:
             if self.direction == 0:
                 self.direction = new_dir
             else:
-                return False
+                if priors == 2:
+                    self.penalty += 0.8
+                else:
+                    return False
 
         return True
 
 
     def recheck_sequence(self) -> bool:
         """ Check that the sequence is a fully valid token assignment
-            and addapt direction and penalties further if necessary.
+            and adapt direction and penalties further if necessary.
 
             This function catches some impossible assignments that need
-            forward context and can therefore not be exluded when building
+            forward context and can therefore not be excluded when building
             the assignment.
         """
         # housenumbers may not be further than 2 words from the beginning.
@@ -272,10 +278,10 @@ class _TokenSequence:
             # <address>,<postcode> should give preference to address search
             if base.postcode.start == 0:
                 penalty = self.penalty
-                self.direction = -1 # name searches are only possbile backwards
+                self.direction = -1 # name searches are only possible backwards
             else:
                 penalty = self.penalty + 0.1
-                self.direction = 1 # name searches are only possbile forwards
+                self.direction = 1 # name searches are only possible forwards
             yield dataclasses.replace(base, penalty=penalty)
 
 
@@ -385,7 +391,7 @@ class _TokenSequence:
                 yield from self._get_assignments_address_backward(base, query)
 
             # variant for special housenumber searches
-            if base.housenumber:
+            if base.housenumber and not base.qualifier:
                 yield dataclasses.replace(base, penalty=self.penalty)