from .query_analyzer_factory import make_query_analyzer, AbstractQueryAnalyzer
from .query import Phrase, QueryStruct
+
class ForwardGeocoder:
""" Main class responsible for place search.
"""
self.timeout = dt.timedelta(seconds=timeout or 1000000)
self.query_analyzer: Optional[AbstractQueryAnalyzer] = None
-
@property
def limit(self) -> int:
""" Return the configured maximum number of search results.
"""
return self.params.max_results
-
async def build_searches(self,
phrases: List[Phrase]) -> Tuple[QueryStruct, List[AbstractSearch]]:
""" Analyse the query and return the tokenized query and list of
return query, searches
-
async def execute_searches(self, query: QueryStruct,
searches: List[AbstractSearch]) -> SearchResults:
""" Run the abstract searches against the database until a result
return SearchResults(results.values())
-
def pre_filter_results(self, results: SearchResults) -> SearchResults:
""" Remove results that are significantly worse than the
best match.
return results
-
def sort_and_cut_results(self, results: SearchResults) -> SearchResults:
""" Remove badly matching results, sort by ranking and
limit to the configured number of results.
"""
if results:
- results.sort(key=lambda r: r.ranking)
+ results.sort(key=lambda r: (r.ranking, 0 if r.bbox is None else -r.bbox.area))
min_rank = results[0].rank_search
min_ranking = results[0].ranking
results = SearchResults(r for r in results
- if r.ranking + 0.03 * (r.rank_search - min_rank)
- < min_ranking + 0.5)
+ if (r.ranking + 0.03 * (r.rank_search - min_rank)
+ < min_ranking + 0.5))
results = SearchResults(results[:self.limit])
return results
-
def rerank_by_query(self, query: QueryStruct, results: SearchResults) -> None:
""" Adjust the accuracy of the localized result according to how well
they match the original query.
"""
assert self.query_analyzer is not None
qwords = [word for phrase in query.source
- for word in re.split('[, ]+', phrase.text) if word]
+ for word in re.split('[, ]+', phrase.text) if word]
if not qwords:
return
distance *= 2
result.accuracy += distance * 0.4 / sum(len(w) for w in qwords)
-
async def lookup_pois(self, categories: List[Tuple[str, str]],
phrases: List[Phrase]) -> SearchResults:
""" Look up places by category. If phrase is given, a place search
return results
-
async def lookup(self, phrases: List[Phrase]) -> SearchResults:
""" Look up a single free-text query.
"""
return results
-# pylint: disable=invalid-name,too-many-locals
def _dump_searches(searches: List[AbstractSearch], query: QueryStruct,
start: int = 0) -> Iterator[Optional[List[Any]]]:
yield ['Penalty', 'Lookups', 'Housenr', 'Postcode', 'Countries',
ranks = ranks[:100] + '...'
return f"{f.column}({ranks},def={f.default:.3g})"
- def fmt_lookup(l: Any) -> str:
- if not l:
+ def fmt_lookup(lk: Any) -> str:
+ if not lk:
return ''
- return f"{l.lookup_type}({l.column}{tk(l.tokens)})"
-
+ return f"{lk.lookup_type}({lk.column}{tk(lk.tokens)})"
def fmt_cstr(c: Any) -> str:
if not c: