You are on page 1of 2

class brm:

def __init__(self, doccon, docloc, doctitle):


self.doccon = doccon
self.docloc = docloc
self.doctitle = doctitle
self.maxdoc = len(doctitle)

def notlogic(self, listloc):


inverseloc=[]
for loc in range(1,self.maxdoc+1):
if loc not in listloc:
inverseloc.append(loc)
return inverseloc

def andlogic(self, list1, list2):


andlist=[]
while len(list1)!=0 and len(list2)!=0:
p1=list1[0]
p2=list2[0]

if p1==p2:
andlist.append(p1)
list1.pop(0)
list2.pop(0)
elif p1<p2:
list1.pop(0)
else:
list2.pop(0)
return andlist

def orlogic(self, list1, list2):


orlist=[]
while len(list1)!=0 and len(list2)!=0:
p1=list1[0]
p2=list2[0]

if p1==p2:
orlist.append(p1)
list1.pop(0)
list2.pop(0)
elif p1<p2:
orlist.append(p1)
list1.pop(0)
else:
orlist.append(p2)
list2.pop(0)
if len(list1)!=0:
orlist.extend(list1)
else:
orlist.extend(list2)
return orlist

def searchbrm(self, query):


term=[]
logic=[]

for w in query:
if w!='and' and w!='or' and w!='not':
term.append(w)
else:
logic.append(w)

if len(term)==1 and len(logic)==0:


q = self.docloc.get(term.pop(0))
elif len(term)==1 and logic[0]=='not':
q = self.notlogic(self.docloc.get(term.pop(0)))
else:
q=self.docloc.get(term.pop(0))
while (len(logic)!=0):
log=logic.pop(0)
if log=='not':
q = self.notlogic(q)
elif log=='and':
w2=self.docloc.get(term.pop(0))
if len(logic)!=0 and logic[0]=='not':
w2=self.notlogic(w2)
logic.pop(0)
q=self.andlogic(q,w2)
elif log=='or':
w2=self.docloc.get(term.pop(0))
if len(logic)!=0 and logic[0]=='not':
w2=self.notlogic(w2)
logic.pop(0)
q=self.orlogic(q,w2)
else:
print("Logic Tidak Ada!")
break

print("Hasil Pencarian:")
if len(q)==0:
print("Dokumen tidak ada!")
else:
for loc in q:
print("- "+self.doctitle.get(loc))

You might also like