class QueryDocument(Document): def __init__(self, field): Document.__init__(self) self.insidedoc = Document() self.field = field def addeq(self, value): self.add(self.field, value) def addgt(self, value): self.addnormaloperator("$gt", value) def addgte(self, value): self.addnormaloperator("$gte", value) def addlt(self, value): self.addnormaloperator("$lt", value) def addlte(self, value): self.addnormaloperator("$lte", value) def addne(self, value): self.addnormaloperator("$ne", value) def addin(self, value): self.addnormaloperator("$in", value) def addnin(self, value): self.addnormaloperator("$nin", value) def negate(self): self.insidedoc = {"$not": self.insidedoc.getdoc()} def addexists(self): self.addnormaloperator("$exists", True) def addnotexists(self): self.addnormaloperator("$exists", False) def addnormaloperator(self, operator, value): self.insidedoc.add(operator, value) def getdoc(self): try: if self.insidedoc: self.add(self.field, self.insidedoc) return self.doc except AttributeError: return self.insidedoc.getdoc() def __str__(self): return str(self.getdoc())
class SortDocument(Document): def __init__(self): Document.__init__(self) self.insidedoc = Document() def addfield(self, field): self.insidedoc.add(field, 1) def addfieldreverse(self, field): self.insidedoc.add(field, -1) def __str__(self): self.add("$sort", self.insidedoc) return str(self.getdoc()) def getdoc(self): self.add("$sort", self.insidedoc) return self.doc
class Category: def __init__(self, name, documents=[]): self.name = name self.corpus = Document(name) #Document to track word counts for all documents self.documents = documents for document in self.documents: self.corpus.add(document.word_counts) def update(self, new_document): self.documents.append(new_document) self.corpus.add(new_document.word_counts) def remove(self, document): self.corpus.remove(document.word_counts) self.documents.remove(document) def listDocuments(self): i = 0 for doc in self.documents: print('[{0}] {1}'.format(i, doc.name)) i += 1