Commit b1897d83 authored by Dos Santos David's avatar Dos Santos David

add boolean queries

parent b72b5236
from gogole import query
def run(collection, args):
if args.search_query_type == 'boolean':
print('boolean')
else:
print('vectorial')
query_cls = query.QUERY_MAP[args.search_query_type]
query_browser = query_cls(collection)
query_browser.search(args.query)
from gogole.query.query import Query
from gogole.query.boolean_query import BooleanQuery
QUERY_TYPE_BOOLEAN = 'boolean'
QUERY_TYPE_VECTORIAL = 'vectorial'
QUERY_MAP = {
QUERY_TYPE_BOOLEAN: BooleanQuery
}
class BooleanQuery:
OPERATOR_AND = ' and '
OPERATOR_OR = ' or '
OPERATOR_NOT = ' not '
def __init__(self, collection):
self.collection = collection
def search_documents(self, query):
# Assume the expression
# is in the conjunctive normal form
and_queries = query.split(self.OPERATOR_AND)
doc_ids_by_conjunction = list()
for and_query in and_queries:
doc_ids = set.union(*(self.collection.indexer.token_lookup(token) for token in and_query.split(self.OPERATOR_OR)))
doc_ids_by_conjunction.append(doc_ids)
return set.intersection(*doc_ids_by_conjunction)
def search(self, query_lst):
"""
Parse a boolean query
and return a list of documents relevant for this query
"""
doc_ids = self.search_documents(" ".join(x.lower() for x in query_lst))
print("Document ids : {}".format(", ".join(str(x) for x in doc_ids)))
class Query:
def __init__(self, collection):
self.collection = collection
def search(self, query):
raise Exception('search not implemented')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment