-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoolean_Model.py
110 lines (100 loc) · 2.64 KB
/
Boolean_Model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import pickle
posting_list = pickle.load(open("posting_list.dict", "rb"))
documents = sorted(pickle.load(open("documents.dict", "rb")))
query = input("Masukkan Query: ")
query = query.lower()
stack_list = []
stack_op = []
def operasi_and(list1, list2):
result = []
i = 0
j = 0
l1 = len(list1)
l2 = len(list2)
while i<l1 and j<l2:
if list1[i] == list2[j]:
result.append(list1[i])
i = i+1
j = j+1
elif list1[i] < list2[j]:
i = i+1
else:
j = j+1
return result
def operasi_or(list1, list2):
result = []
i = 0
j = 0
l1 = len(list1)
l2 = len(list2)
while i<l1 and j<l2:
if list1[i] == list2[j]:
result.append(list1[i])
i = i+1
j = j+1
elif list1[i] < list2[j]:
result.append(list1[i])
i = i+1
else:
result.append(list2[j])
j = j+1
while i < l1:
result.append(list1[i])
i = i+1
while j < l2:
result.append(list2[j])
j = j+1
return result
def operasi_not(list1):
global documents
result = []
i = 0
j = 0
l1 = len(documents)
l2 = len(list1)
while j < l2:
if documents[i] == list1[j]:
j = j+1
elif documents[i] < list1[j]:
result.append(documents[i])
i = i+1
while i < l1:
result.append(documents[i])
i = i+1
return result
def operasi():
global stack_list
global stack_op
operator = stack_op.pop()
list1 = stack_list.pop()
if operator == 'not':
hasil = operasi_not(list1)
elif operator == 'and':
list2 = stack_list.pop()
hasil = operasi_and(list1,list2)
elif operator == 'or':
list2 = stack_list.pop()
hasil = operasi_or(list1,list2)
return hasil
for word in query.split():
if word == '(':
stack_op.append('(')
elif word == 'not':
stack_op.append(word)
elif word == 'and' or word == 'or':
while len(stack_op) != 0 and stack_op[len(stack_op)-1] == 'not':
hasil = operasi()
stack_list.append(hasil)
stack_op.append(word)
elif word == ')':
while stack_op[len(stack_op)-1] != '(':
hasil = operasi()
stack_list.append(hasil)
stack_op.pop()
else:
stack_list.append(posting_list[word])
while len(stack_op):
hasil = operasi()
stack_list.append(hasil)
print("Hasil Pencarian")
print(stack_list[0])