Fix #4951 (Seach Interface History - Keyword Missmatch)

This commit is contained in:
Kovid Goyal 2010-02-20 09:57:35 -07:00
parent 7ca92a591c
commit e4b0e9f518

View File

@ -6,14 +6,14 @@ __docformat__ = 'restructuredtext en'
''' '''
A parser for search queries with a syntax very similar to that used by A parser for search queries with a syntax very similar to that used by
the Google search engine. the Google search engine.
For details on the search query syntax see :class:`SearchQueryParser`. For details on the search query syntax see :class:`SearchQueryParser`.
To use the parser, subclass :class:`SearchQueryParser` and implement the To use the parser, subclass :class:`SearchQueryParser` and implement the
methods :method:`SearchQueryParser.universal_set` and methods :method:`SearchQueryParser.universal_set` and
:method:`SearchQueryParser.get_matches`. See for example :class:`Tester`. :method:`SearchQueryParser.get_matches`. See for example :class:`Tester`.
If this module is run, it will perform a series of unit tests. If this module is run, it will perform a series of unit tests.
''' '''
import sys, string, operator import sys, string, operator
@ -24,26 +24,26 @@ from calibre.utils.pyparsing import Keyword, Group, Forward, CharsNotIn, Suppres
class SearchQueryParser(object): class SearchQueryParser(object):
''' '''
Parses a search query. Parses a search query.
A search query consists of tokens. The tokens can be combined using A search query consists of tokens. The tokens can be combined using
the `or`, `and` and `not` operators as well as grouped using parentheses. the `or`, `and` and `not` operators as well as grouped using parentheses.
When no operator is specified between two tokens, `and` is assumed. When no operator is specified between two tokens, `and` is assumed.
Each token is a string of the form `location:query`. `location` is a string Each token is a string of the form `location:query`. `location` is a string
from :member:`LOCATIONS`. It is optional. If it is omitted, it is assumed to from :member:`LOCATIONS`. It is optional. If it is omitted, it is assumed to
be `all`. `query` is an arbitrary string that must not contain parentheses. be `all`. `query` is an arbitrary string that must not contain parentheses.
If it contains whitespace, it should be quoted by enclosing it in `"` marks. If it contains whitespace, it should be quoted by enclosing it in `"` marks.
Examples:: Examples::
* `Asimov` [search for the string "Asimov" in location `all`] * `Asimov` [search for the string "Asimov" in location `all`]
* `comments:"This is a good book"` [search for "This is a good book" in `comments`] * `comments:"This is a good book"` [search for "This is a good book" in `comments`]
* `author:Asimov tag:unread` [search for books by Asimov that have been tagged as unread] * `author:Asimov tag:unread` [search for books by Asimov that have been tagged as unread]
* `author:Asimov or author:Hardy` [search for books by Asimov or Hardy] * `author:Asimov or author:Hardy` [search for books by Asimov or Hardy]
* `(author:Asimov or author:Hardy) and not tag:read` [search for unread books by Asimov or Hardy] * `(author:Asimov or author:Hardy) and not tag:read` [search for unread books by Asimov or Hardy]
''' '''
LOCATIONS = [ LOCATIONS = [
'tag', 'tag',
'title', 'title',
@ -57,12 +57,12 @@ class SearchQueryParser(object):
'isbn', 'isbn',
'all', 'all',
] ]
@staticmethod @staticmethod
def run_tests(parser, result, tests): def run_tests(parser, result, tests):
failed = [] failed = []
for test in tests: for test in tests:
print '\tTesting:', test[0], print '\tTesting:', test[0],
res = parser.parseString(test[0]) res = parser.parseString(test[0])
if list(res.get(result, None)) == test[1]: if list(res.get(result, None)) == test[1]:
print 'OK' print 'OK'
@ -70,7 +70,7 @@ class SearchQueryParser(object):
print 'FAILED:', 'Expected:', test[1], 'Got:', list(res.get(result, None)) print 'FAILED:', 'Expected:', test[1], 'Got:', list(res.get(result, None))
failed.append(test[0]) failed.append(test[0])
return failed return failed
def __init__(self, test=False): def __init__(self, test=False):
self._tests_failed = False self._tests_failed = False
# Define a token # Define a token
@ -95,50 +95,50 @@ class SearchQueryParser(object):
('title:"one two"', ['title', 'one two']), ('title:"one two"', ['title', 'one two']),
) )
) )
Or = Forward() Or = Forward()
Parenthesis = Group( Parenthesis = Group(
Suppress('(') + Or + Suppress(')') Suppress('(') + Or + Suppress(')')
).setResultsName('parenthesis') | Token ).setResultsName('parenthesis') | Token
Not = Forward() Not = Forward()
Not << (Group( Not << (Group(
Suppress(Keyword("not", caseless=True)) + Not Suppress(Keyword("not", caseless=True)) + Not
).setResultsName("not") | Parenthesis) ).setResultsName("not") | Parenthesis)
And = Forward() And = Forward()
And << (Group( And << (Group(
Not + Suppress(Keyword("and", caseless=True)) + And Not + Suppress(Keyword("and", caseless=True)) + And
).setResultsName("and") | Group( ).setResultsName("and") | Group(
Not + OneOrMore(~oneOf("and or") + And) Not + OneOrMore(~oneOf("and or", caseless=True) + And)
).setResultsName("and") | Not) ).setResultsName("and") | Not)
Or << (Group( Or << (Group(
And + Suppress(Keyword("or", caseless=True)) + Or And + Suppress(Keyword("or", caseless=True)) + Or
).setResultsName("or") | And) ).setResultsName("or") | And)
if test: if test:
Or.validate() Or.validate()
self._tests_failed = bool(failed) self._tests_failed = bool(failed)
self._parser = Or self._parser = Or
#self._parser.setDebug(True) #self._parser.setDebug(True)
#self.parse('(tolstoy)') #self.parse('(tolstoy)')
self._parser.setDebug(False) self._parser.setDebug(False)
def parse(self, query): def parse(self, query):
res = self._parser.parseString(query)[0] res = self._parser.parseString(query)[0]
return self.evaluate(res) return self.evaluate(res)
def method(self, group_name): def method(self, group_name):
return getattr(self, 'evaluate_'+group_name) return getattr(self, 'evaluate_'+group_name)
def evaluate(self, parse_result): def evaluate(self, parse_result):
return self.method(parse_result.getName())(parse_result) return self.method(parse_result.getName())(parse_result)
def evaluate_and(self, argument): def evaluate_and(self, argument):
return self.evaluate(argument[0]).intersection(self.evaluate(argument[1])) return self.evaluate(argument[0]).intersection(self.evaluate(argument[1]))
@ -150,27 +150,27 @@ class SearchQueryParser(object):
def evaluate_parenthesis(self, argument): def evaluate_parenthesis(self, argument):
return self.evaluate(argument[0]) return self.evaluate(argument[0])
def evaluate_token(self, argument): def evaluate_token(self, argument):
return self.get_matches(argument[0], argument[1]) return self.get_matches(argument[0], argument[1])
def get_matches(self, location, query): def get_matches(self, location, query):
''' '''
Should return the set of matches for :param:'location` and :param:`query`. Should return the set of matches for :param:'location` and :param:`query`.
:param:`location` is one of the items in :member:`SearchQueryParser.LOCATIONS`. :param:`location` is one of the items in :member:`SearchQueryParser.LOCATIONS`.
:param:`query` is a string literal. :param:`query` is a string literal.
''' '''
return set([]) return set([])
def universal_set(self): def universal_set(self):
''' '''
Should return the set of all matches. Should return the set of all matches.
''' '''
return set([]) return set([])
class Tester(SearchQueryParser): class Tester(SearchQueryParser):
texts = { texts = {
1: [u'Eugenie Grandet', u'Honor\xe9 de Balzac', u'manybooks.net', u'lrf'], 1: [u'Eugenie Grandet', u'Honor\xe9 de Balzac', u'manybooks.net', u'lrf'],
2: [u'Fanny Hill', u'John Cleland', u'manybooks.net', u'lrf'], 2: [u'Fanny Hill', u'John Cleland', u'manybooks.net', u'lrf'],
@ -459,30 +459,30 @@ class Tester(SearchQueryParser):
u'Washington Square Press', u'Washington Square Press',
u'lrf,rar'] u'lrf,rar']
} }
tests = { tests = {
'Dysfunction' : set([348]), 'Dysfunction' : set([348]),
'title:Dysfunction' : set([348]), 'title:Dysfunction' : set([348]),
'title:Dysfunction or author:Laurie': set([348, 444]), 'title:Dysfunction OR author:Laurie': set([348, 444]),
'(tag:txt or tag:pdf)': set([33, 258, 354, 305, 242, 51, 55, 56, 154]), '(tag:txt or tag:pdf)': set([33, 258, 354, 305, 242, 51, 55, 56, 154]),
'(tag:txt or tag:pdf) and author:Tolstoy': set([55, 56]), '(tag:txt OR tag:pdf) and author:Tolstoy': set([55, 56]),
'Tolstoy txt': set([55, 56]), 'Tolstoy txt': set([55, 56]),
'Hamilton Amsterdam' : set([]), 'Hamilton Amsterdam' : set([]),
u'Beär' : set([91]), u'Beär' : set([91]),
'dysfunc or tolstoy': set([348, 55, 56]), 'dysfunc or tolstoy': set([348, 55, 56]),
'tag:txt and not tolstoy': set([33, 258, 354, 305, 242, 154]), 'tag:txt AND NOT tolstoy': set([33, 258, 354, 305, 242, 154]),
'not tag:lrf' : set([305]), 'not tag:lrf' : set([305]),
'london:thames': set([13]), 'london:thames': set([13]),
'publisher:london:thames': set([13]), 'publisher:london:thames': set([13]),
'"(1977)"': set([13]), '"(1977)"': set([13]),
} }
fields = {'title':0, 'author':1, 'publisher':2, 'tag':3} fields = {'title':0, 'author':1, 'publisher':2, 'tag':3}
_universal_set = set(texts.keys()) _universal_set = set(texts.keys())
def universal_set(self): def universal_set(self):
return self._universal_set return self._universal_set
def get_matches(self, location, query): def get_matches(self, location, query):
location = location.lower() location = location.lower()
if location in self.fields.keys(): if location in self.fields.keys():
@ -491,19 +491,19 @@ class Tester(SearchQueryParser):
getter = lambda y: ''.join(x if x else '' for x in y) getter = lambda y: ''.join(x if x else '' for x in y)
else: else:
getter = lambda x: '' getter = lambda x: ''
if not query: if not query:
return set([]) return set([])
query = query.lower() query = query.lower()
return set(key for key, val in self.texts.items() \ return set(key for key, val in self.texts.items() \
if query and query in getattr(getter(val), 'lower', lambda : '')()) if query and query in getattr(getter(val), 'lower', lambda : '')())
def run_tests(self): def run_tests(self):
failed = [] failed = []
for query in self.tests.keys(): for query in self.tests.keys():
print 'Testing query:', query, print 'Testing query:', query,
res = self.parse(query) res = self.parse(query)
if res != self.tests[query]: if res != self.tests[query]:
print 'FAILED', 'Expected:', self.tests[query], 'Got:', res print 'FAILED', 'Expected:', self.tests[query], 'Got:', res
@ -511,7 +511,7 @@ class Tester(SearchQueryParser):
else: else:
print 'OK' print 'OK'
return failed return failed
def main(args=sys.argv): def main(args=sys.argv):
tester = Tester(test=True) tester = Tester(test=True)
@ -519,7 +519,7 @@ def main(args=sys.argv):
if tester._tests_failed or failed: if tester._tests_failed or failed:
print '>>>>>>>>>>>>>> Tests Failed <<<<<<<<<<<<<<<' print '>>>>>>>>>>>>>> Tests Failed <<<<<<<<<<<<<<<'
return 1 return 1
return 0 return 0
if __name__ == '__main__': if __name__ == '__main__':