-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGui.py
119 lines (96 loc) · 3.41 KB
/
Gui.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
111
112
113
114
115
116
117
118
119
import pickle
import time
import webbrowser
from tkinter import *
from tkinter import filedialog
from tkinter import messagebox
from tkHyperLinkManager import HyperlinkManager
import Searcher
import Indexer
from functools import partial
def load_files():
global lexicon
global docids
global filestreams
global accumulativefreq
# load lexicon
a_file = open("Lexicon.pkl", "rb")
lexicon = pickle.load(a_file)
a_file.close()
# load accumulative frequency of words
a_file = open("arr.pkl", "rb")
accumulativefreq = pickle.load(a_file)
a_file.close()
# load docid and its url
a_file = open("docid.pkl", "rb")
docids = pickle.load(a_file)
a_file.close()
filestreams = []
# open file streams for all inverted index barrels
for i in range(len(accumulativefreq)):
filestreams.append(open("InvertedIndex/" + str(i) + ".txt", "r"))
def file_close():
global filestreams
for i in range(len(accumulativefreq)):
filestreams[i].close()
def onClickSearch():
i = 0
searched_text = textBox.get()
s = time.time()
results = Searcher.WordSearch(searched_text, filestreams, lexicon, docids, accumulativefreq)
timetaken.delete(0.0, END)
timetaken.insert(END, "Time taken:" + str(time.time() - s))
textView.delete(0.0, END)
hyperLink = HyperlinkManager(textView)
while i < len(results) - 1:
textView.insert(END, results[i])
textView.insert(END, "\n")
i += 1
textView.insert(END, results[i], hyperLink.add(partial(webbrowser.open, results[i])))
textView.insert(END, "\n")
i += 1
def onClickInsert():
file_close()
filepath = filedialog.askopenfilename(initialdir="/",
title="Select a File",
filetypes=(("Json files",
"*.json*"),
("all files",
"*.*")))
try:
Indexer.update_invertedindex(filepath)
load_files()
messagebox.showinfo("Update", "Inverted Index Updated")
except FileNotFoundError:
print("File Not Found")
filestreams = []
lexicon = {}
accumulativefreq = [[]]
docids = [[]]
load_files()
# open file streams for all inverted index barrels
window = Tk()
window.title('Search')
window.configure(background="white")
window.geometry("1920x1080")
insertButton = Button(window, text="Insert file", font=("Terminal", 10), width=11, command=onClickInsert)
insertButton.place(relx=0.9, rely=0.05, anchor="ne")
frame = Frame(window)
frame.pack()
textBox = Entry(frame, width=60, font=("Terminal", 14), bg="white")
textBox.pack(side=RIGHT)
searchButton = Button(frame, text="Search", font=("Terminal", 10), width=6, command=onClickSearch)
searchButton.pack(side=RIGHT)
frame.place(relx=0.25, rely=0.07, anchor="center")
scroll = Scrollbar(window)
scroll.pack(side=RIGHT, fill=Y)
timetaken = Text(window, width=20, height=1, background="white", font=("Terminal", 14))
timetaken.place(relx=0.1, rely=0.13, anchor='center')
textView = Text(window, width=120, height=25, background="white", font=("Segoe UI Semibold", 14),
yscrollcommand=scroll.set)
textView.place(relx=0.465, rely=0.55, anchor='center')
scroll.config(command=textView.yview)
# to show the window
window.mainloop()
# close files
file_close()