def nbest_ascending(self, docids, limit,
from_, until, raise_unsortable=False):
if limit is None: #pragma NO COVERAGE
raise RuntimeError('n-best used without limit')
# lifted from heapq.nsmallest
h = nsort(docids, self._rev_index, ASC, from_, until)
it = iter(h)
result = sorted(islice(it, 0, limit))
if not result: #pragma NO COVERAGE
raise StopIteration
insort = bisect.insort
pop = result.pop
los = result[-1] # los --> Largest of the nsmallest
for elem in it:
if los <= elem:
continue
insort(result, elem)
pop()
los = result[-1]
missing_docids = []
for value, docid in result:
if value is ASC:
missing_docids.append(docid)
else:
yield docid
if raise_unsortable and missing_docids:
raise Unsortable(missing_docids)
评论列表
文章目录