From: Sarah Hoffmann Date: Sun, 28 Jan 2024 10:35:30 +0000 (+0100) Subject: disallow category tokens in the middle of a query string X-Git-Tag: v4.4.0~23^2~3 X-Git-Url: https://git.openstreetmap.org/nominatim.git/commitdiff_plain/fed46240d5af3a2543e7cac930a862d6a0abde7b disallow category tokens in the middle of a query string This already worked for left-to-right readings and now is also implemented for right-to-left reading. A qualifier must always be before or after the name. --- diff --git a/nominatim/api/search/token_assignment.py b/nominatim/api/search/token_assignment.py index d94d6903..b874dcd7 100644 --- a/nominatim/api/search/token_assignment.py +++ b/nominatim/api/search/token_assignment.py @@ -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 qeury. 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 diff --git a/test/python/api/search/test_token_assignment.py b/test/python/api/search/test_token_assignment.py index 2ed55a0f..54e8af14 100644 --- a/test/python/api/search/test_token_assignment.py +++ b/test/python/api/search/test_token_assignment.py @@ -337,3 +337,14 @@ def test_qualifier_after_housenumber(): (BreakType.WORD, PhraseType.NONE, [(3, TokenType.PARTIAL)])) check_assignments(yield_token_assignments(q)) + + +def test_qualifier_in_middle_of_phrase(): + q = make_query((BreakType.START, PhraseType.NONE, [(1, TokenType.PARTIAL)]), + (BreakType.PHRASE, PhraseType.NONE, [(2, TokenType.PARTIAL)]), + (BreakType.WORD, PhraseType.NONE, [(3, TokenType.QUALIFIER)]), + (BreakType.WORD, PhraseType.NONE, [(4, TokenType.PARTIAL)]), + (BreakType.PHRASE, PhraseType.NONE, [(5, TokenType.PARTIAL)])) + + check_assignments(yield_token_assignments(q)) +