@@ -104,19 +104,27 @@ async def execute_searches(self, query: QueryStruct,
104
104
return SearchResults (results .values ())
105
105
106
106
107
+ def pre_filter_results (self , results : SearchResults ) -> SearchResults :
108
+ """ Remove results that are significantly worse than the
109
+ best match.
110
+ """
111
+ if results :
112
+ max_ranking = min (r .ranking for r in results ) + 0.5
113
+ results = SearchResults (r for r in results if r .ranking < max_ranking )
114
+
115
+ return results
116
+
117
+
107
118
def sort_and_cut_results (self , results : SearchResults ) -> SearchResults :
108
119
""" Remove badly matching results, sort by ranking and
109
120
limit to the configured number of results.
110
121
"""
111
122
if results :
112
- min_ranking = min (r .ranking for r in results )
113
- results = SearchResults (r for r in results if r .ranking < min_ranking + 0.5 )
114
123
results .sort (key = lambda r : r .ranking )
115
-
116
- if results :
117
124
min_rank = results [0 ].rank_search
125
+ min_ranking = results [0 ].ranking
118
126
results = SearchResults (r for r in results
119
- if r .ranking + 0.05 * (r .rank_search - min_rank )
127
+ if r .ranking + 0.03 * (r .rank_search - min_rank )
120
128
< min_ranking + 0.5 )
121
129
122
130
results = SearchResults (results [:self .limit ])
@@ -174,6 +182,7 @@ async def lookup_pois(self, categories: List[Tuple[str, str]],
174
182
if query :
175
183
searches = [wrap_near_search (categories , s ) for s in searches [:50 ]]
176
184
results = await self .execute_searches (query , searches )
185
+ results = self .pre_filter_results (results )
177
186
await add_result_details (self .conn , results , self .params )
178
187
log ().result_dump ('Preliminary Results' , ((r .accuracy , r ) for r in results ))
179
188
results = self .sort_and_cut_results (results )
@@ -203,6 +212,7 @@ async def lookup(self, phrases: List[Phrase]) -> SearchResults:
203
212
if searches :
204
213
# Execute SQL until an appropriate result is found.
205
214
results = await self .execute_searches (query , searches [:50 ])
215
+ results = self .pre_filter_results (results )
206
216
await add_result_details (self .conn , results , self .params )
207
217
log ().result_dump ('Preliminary Results' , ((r .accuracy , r ) for r in results ))
208
218
self .rerank_by_query (query , results )
0 commit comments