From 6d74a02f3f7aa33724a675a85aacead4f6c75be5 Mon Sep 17 00:00:00 2001 From: John Schember Date: Tue, 8 Mar 2011 07:55:33 -0500 Subject: [PATCH] MobileRead: more accurate and faster search. --- src/calibre/gui2/store/mobileread_plugin.py | 29 ++++++++++++--------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/calibre/gui2/store/mobileread_plugin.py b/src/calibre/gui2/store/mobileread_plugin.py index b8b681b6b9..6e63ff054d 100644 --- a/src/calibre/gui2/store/mobileread_plugin.py +++ b/src/calibre/gui2/store/mobileread_plugin.py @@ -35,24 +35,29 @@ class MobileReadStore(StorePlugin): def search(self, query, max_results=10, timeout=60): books = self.get_book_list(timeout=timeout) + query = query.lower() + query_parts = query.split(' ') matches = [] - s = difflib.SequenceMatcher(lambda x: x in ' \t,.') - s.set_seq2(query.lower()) + s = difflib.SequenceMatcher() for x in books: - # Find the match ratio for each part of the book. - s.set_seq1(x.author.lower()) - a_ratio = s.ratio() - s.set_seq1(x.title.lower()) - t_ratio = s.ratio() - s.set_seq1(x.format.lower()) - f_ratio = s.ratio() - ratio = sorted([a_ratio, t_ratio, f_ratio])[-1] - # Store the highest match ratio with the book. - matches.append((ratio, x)) + ratio = 0 + t_string = '%s %s' % (x.author.lower(), x.title.lower()) + query_matches = [] + for q in query_parts: + if q in t_string: + query_matches.append(q) + for q in query_matches: + s.set_seq2(q) + for p in t_string.split(' '): + s.set_seq1(p) + ratio += s.ratio() + if ratio > 0: + matches.append((ratio, x)) # Move the best scorers to head of list. matches = heapq.nlargest(max_results, matches) for score, book in matches: + print('%s %s' % (score, book.title)) s = SearchResult() s.title = book.title s.author = book.author