X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/004883bdb1cfdfea053cb59fe32792c4e368e88c..513504140512d17ddbe0769b2ec3887ee16f7e31:/nominatim/api/search/query.py diff --git a/nominatim/api/search/query.py b/nominatim/api/search/query.py index 4e28d365..f2b18f87 100644 --- a/nominatim/api/search/query.py +++ b/nominatim/api/search/query.py @@ -114,6 +114,24 @@ class TokenRange(NamedTuple): start: int end: int + def replace_start(self, new_start: int) -> 'TokenRange': + """ Return a new token range with the new start. + """ + return TokenRange(new_start, self.end) + + + def replace_end(self, new_end: int) -> 'TokenRange': + """ Return a new token range with the new end. + """ + return TokenRange(self.start, new_end) + + + def split(self, index: int) -> Tuple['TokenRange', 'TokenRange']: + """ Split the span into two spans at the given index. + The index must be within the span. + """ + return self.replace_end(index), self.replace_start(index) + @dataclasses.dataclass class TokenList: @@ -151,7 +169,10 @@ class QueryNode: and ending at the node 'end'. Returns 'None' if no such tokens exist. """ - return next((t.tokens for t in self.starting if t.end == end and t.ttype == ttype), None) + for tlist in self.starting: + if tlist.end == end and tlist.ttype == ttype: + return tlist.tokens + return None @dataclasses.dataclass