-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvances_search.py
61 lines (53 loc) · 1.73 KB
/
advances_search.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
from stack import Stack
import re
from rank import rank
from paragraph import print_result
def advanced_search(expression, trie, hashmap, graph):
stack = Stack()
tokens = tokenize(expression)
expression_words = []
word_list = []
for token in tokens:
if token == "(" or token in ["AND", "OR", "NOT"]:
stack.push(token)
elif token == ")":
while stack.top() != "(":
word_list.append(stack.pop())
stack.pop()
else:
word_list.append(token)
while not stack.is_empty():
word_list.append(stack.pop())
for word in word_list:
if word not in ["AND", "OR", "NOT"]:
expression_words.append(word)
stack.push(trie.count(word.lower(), hashmap))
else:
second = stack.pop()
first = stack.pop()
stack.push(advanced_operations(first, second, word))
result = stack.pop()
if len(result) > 0:
sorted_pages = rank(result, graph)
print_result(sorted_pages, hashmap, expression_words, 1)
else:
print("No results found")
def advanced_operations(list1, list2, operation):
return_value = []
if operation == "AND":
for page in list1:
if page in list2:
return_value.append(page)
for page in list2:
if page in list1:
return_value.append(page)
elif operation == "OR":
return_value = list1 + list2
elif operation == "NOT":
for page in list1:
if page not in list2:
return_value.append(page)
return return_value
def tokenize(expression):
pattern = r'\(|\)|AND|OR|NOT|\b\w+\b'
return re.findall(pattern, expression)