mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
MobileRead: more accurate and faster search.
This commit is contained in:
parent
4ddc69e0a6
commit
6d74a02f3f
@ -35,24 +35,29 @@ class MobileReadStore(StorePlugin):
|
|||||||
def search(self, query, max_results=10, timeout=60):
|
def search(self, query, max_results=10, timeout=60):
|
||||||
books = self.get_book_list(timeout=timeout)
|
books = self.get_book_list(timeout=timeout)
|
||||||
|
|
||||||
|
query = query.lower()
|
||||||
|
query_parts = query.split(' ')
|
||||||
matches = []
|
matches = []
|
||||||
s = difflib.SequenceMatcher(lambda x: x in ' \t,.')
|
s = difflib.SequenceMatcher()
|
||||||
s.set_seq2(query.lower())
|
|
||||||
for x in books:
|
for x in books:
|
||||||
# Find the match ratio for each part of the book.
|
ratio = 0
|
||||||
s.set_seq1(x.author.lower())
|
t_string = '%s %s' % (x.author.lower(), x.title.lower())
|
||||||
a_ratio = s.ratio()
|
query_matches = []
|
||||||
s.set_seq1(x.title.lower())
|
for q in query_parts:
|
||||||
t_ratio = s.ratio()
|
if q in t_string:
|
||||||
s.set_seq1(x.format.lower())
|
query_matches.append(q)
|
||||||
f_ratio = s.ratio()
|
for q in query_matches:
|
||||||
ratio = sorted([a_ratio, t_ratio, f_ratio])[-1]
|
s.set_seq2(q)
|
||||||
# Store the highest match ratio with the book.
|
for p in t_string.split(' '):
|
||||||
matches.append((ratio, x))
|
s.set_seq1(p)
|
||||||
|
ratio += s.ratio()
|
||||||
|
if ratio > 0:
|
||||||
|
matches.append((ratio, x))
|
||||||
|
|
||||||
# Move the best scorers to head of list.
|
# Move the best scorers to head of list.
|
||||||
matches = heapq.nlargest(max_results, matches)
|
matches = heapq.nlargest(max_results, matches)
|
||||||
for score, book in matches:
|
for score, book in matches:
|
||||||
|
print('%s %s' % (score, book.title))
|
||||||
s = SearchResult()
|
s = SearchResult()
|
||||||
s.title = book.title
|
s.title = book.title
|
||||||
s.author = book.author
|
s.author = book.author
|
||||||
|
Loading…
x
Reference in New Issue
Block a user