-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
386 lines (327 loc) · 10.6 KB
/
main.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#from tkinter import *
from Tkinter import *
#from Tkinter import messagebox
import tkMessageBox
#from Tkinter.filedialog import askopenfilename
import tkFileDialog
from reduction import Reduction
from nltk.stem import PorterStemmer
import wikipedia
import shortForm
import time
import pytesseract
from PIL import Image, ImageEnhance, ImageFilter
import modelVC,cosine
import requests #for reading web data
import random
opTA=""
opPA=""
endTime1=0
endTime2=0
def freshWindow(): #clear GUI
ipEntry.delete(1.0, END)
ipEntry.pack_forget()
ipLabel.pack_forget()
opLabel.pack_forget()
buttonSummary.pack_forget()
buttonSave.pack_forget()
buttonSave.pack_forget()
opEntry.pack_forget()
redRatio.pack_forget()
updateRR.pack_forget()
wikiQLabel.pack_forget()
wSearchEntry.pack_forget()
buttonWiki.pack_forget()
buttonWeb.pack_forget()
opEntry.delete(1.0,END)
logLabel.pack_forget()
buttonClear.pack_forget()
ipLabel.pack()
ipEntry.pack()
buttonSummary.pack()
opLabel.pack()
opEntry.pack()
buttonClear.pack()
opEntry.insert(INSERT, "Enter input first.")
opEntry.config(state='disabled', fg='#404247', bg='#c8cace')
def packIP(): #pack input area
ipLabel.pack()
ipEntry.pack()
buttonSummary.pack()
ipEntry.delete(1.0, END)
def openLog(): #display Log
freshWindow()
logLabel.pack()
ipEntry.pack()
ipEntry.delete(1.0,END)
opLabel.pack_forget()
opEntry.pack_forget()
with open("log.txt") as f:
ipEntry.insert(INSERT,f.read())
def evaluate():
count = 0
stop_words=0
swFile=open("stopWords.txt").read()
global opPA,opTA
if len(opPA)<len(opTA):
for word in opPA:
#if word not in swFile:
if word in opTA:
count+=1
else:
stop_words+=1
else:
for word in opTA:
#if word not in swFile:
if word in opPA:
count+=1
else:
stop_words+=1
den=float(len(opPA)+len(opTA))/2
value = ((count)/den)*100.0
tkMessageBox.showinfo("Closeness","Similarity of PA and Text Rank is: %.2f percent\n" %(value))
text = ipEntry.get("1.0", END)
lenText=len(text)
with open("perfLog.text", "a+") as f:
f.write("\n\nInput length: %d" %lenText)
f.write("\nReduction Ratio: %d" %redRatio.get() )
f.write("\nSimilarity: %.2f" %value)
f.write("\nTime(for TR): %.2f " %endTime1)
f.write("\nTime(for PA): %.2f " %endTime2)
return 1
def close(): #exit
res=tkMessageBox.askquestion("Exit", "Are you sure to exit?")
if res == 'yes':
exit()
else:
freshWindow()
def getFile(): #import text file
freshWindow()
packIP()
filename = tkFileDialog.askopenfilename()
text1 = open(filename).read()
ipEntry.insert(INSERT, text1.rstrip(''))
def wikiSearch(): #search wiki
freshWindow()
searchTerm = wSearchEntry.get("1.0", END)
result = wikipedia.page(searchTerm)
packIP()
ipEntry.insert(INSERT, result.summary)
buttonWiki.pack_forget()
def getInput2(): #wikipedia search entry
freshWindow()
wikiQLabel.pack()
wSearchEntry.delete(1.0,END)
wSearchEntry.pack()
buttonWiki.pack()
def getPText(): #paste text
freshWindow()
packIP()
def getImage(): #open image file
freshWindow()
packIP()
ipEntry.insert(INSERT,"Loading text from image...")
filename = tkFileDialog.askopenfilename()
im = Image.open(filename)
im = im.filter(ImageFilter.MedianFilter())
enhancer = ImageEnhance.Contrast(im)
im = enhancer.enhance(2)
im = im.convert('1')
im.save('temp2.jpeg')
text = pytesseract.image_to_string(Image.open('temp2.jpeg'))
text=processText(text)
ipEntry.delete(1.0, END)
ipEntry.insert(INSERT, text)
def processText(text):
text=text.lower()
text=text.replace("(","c")
text=text.replace("8","b")
text=text.replace("0","o")
return text
def getInput5(): #web search
freshWindow()
wikiQLabel.pack()
wSearchEntry.delete(1.0,END)
wSearchEntry.pack()
buttonWeb.pack()
def displayWeb(): #paste web search to ip box
packIP()
searchTerm = wSearchEntry.get("1.0", END)
text = requests.get(searchTerm.strip()).text
#print text
ipEntry.insert(INSERT,"Loading...")
ipEntry.delete(1.0, END)
ipEntry.insert(INSERT, text.strip())
buttonWiki.pack_forget()
def getSummary(): #actually calculate summary
opLabel.pack()
opEntry.pack()
opEntry.config(state='normal' , bg="white" , fg="black")
opEntry.delete(1.0,END)
opEntry.insert(INSERT,"Generating Summary...")
global endTime1,endTime2
startTime = time.time()
text = ipEntry.get("1.0", END)
ps = PorterStemmer()
red_ra=redRatio.get()/100.00
if var1.get():
words = text.split()
for w in words:
text += ps.stem(w)
text += " "
if var6.get()==1:
r = Reduction() #object of class Red..
reduced_text = r.reduce(text, red_ra)
op=' '.join(reduced_text)
global opTA
opTA=op
elif var6.get()==2:
m = modelVC.summary()
op = m.summarize(text,red_ra)
global opPA
opPA=op
if var2.get()==1:
op=shortForm.shortF(op)
opEntry.delete(1.0,END)
opEntry.insert(INSERT, op)
if var6.get()==1:
endTime1 = time.time() - startTime
print("Time taken: %f secs" %endTime1)
if var4.get():
tkMessageBox.showinfo("Statistics", "Time taken to complete: %f secs" %endTime)
if var6.get()==2:
endTime2 = time.time() - startTime
print("Time taken: %f secs" %endTime2)
if var4.get():
tkMessageBox.showinfo("Statistics", "Time taken to complete: %f secs" %endTime)
if var3.get() == 1:
file_f = open("log.txt", "a")
lenText=len(text)
file_f.write("\nInput length: %d" %lenText)
lenText = len(op)
file_f.write("\tSummary length: %d" % lenText)
file_f.write('\nStemming: %d' %var1.get())
file_f.write('\tShort Forms: %d' %var2.get())
file_f.write('\tReduction Ratio: %d percent' % redRatio.get())
file_f.write('\tModel: %d ' % var6.get())
if var6.get()==1:
file_f.write("\nTime taken: %f secs\n" %endTime1)
if var6.get()==2:
file_f.write("\nTime taken: %f secs\n" %endTime2)
file_f.close()
def getRR(): #set RR
ipLabel.pack_forget()
ipEntry.pack_forget()
opLabel.pack_forget()
opEntry.pack_forget()
buttonSummary.pack_forget()
buttonClear.pack_forget()
redRatio.pack()
updateRR.pack()
def updateSW(): #stopWords.txt editing
with open("stopWords.txt", 'w+') as f:
text=ipEntry.get("1.0", END)
f.write(text)
f.close()
def showSW(): #display Stop Words in ip box
buttonSummary.pack_forget()
ipEntry.pack()
opLabel.pack_forget()
opEntry.pack_forget()
buttonClear.pack_forget()
with open("stopWords.txt") as f:
ipEntry.delete(1.0, END)
text=f.read()
text=text.strip('/n')
ipEntry.insert(INSERT,text)
buttonSave.pack()
f.close()
def showSF(): #to be done
return 1
def showHelp():
tkMessageBox.showinfo("Help", "Instructions:\n"
"1. Select input method. \n2. Select reduction ratio(% summary for text)\n 3."
"Generate Summary.")
def contact():
tkMessageBox.showinfo("About us", "All rights reserved\nUse of application comes with no guarantee."
"\nInfo at: https://github.com/varunchitale")
#Main GUI begins
root = Tk()
root.title("Content Summary Generator")
width = root.winfo_screenwidth()
height = root.winfo_screenheight()
x = (width / 2) - (800 / 2)
y = (height / 2) - (650 / 2)
root.resizable(width=False, height=False)
root.geometry('%dx%d+%d+%d' % (800, 600, x, y))
#Menu Bar with all its elements
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
menubar.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="New", command=freshWindow)
filemenu.add_command(label="Open Log", command=openLog)
filemenu.add_command(label="Evaluation", command=evaluate)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=close)
sourceMenu = Menu(menubar, tearoff=0)
menubar.add_cascade(label="Input", menu=sourceMenu)
sourceMenu.add_command(label="Import text file", command=getFile)
wSearchEntry=Text(root)
sourceMenu.add_separator()
sourceMenu.add_command(label="Web Search", command=getInput5)
sourceMenu.add_command(label="Wikipedia Search", command=getInput2)
sourceMenu.add_separator()
sourceMenu.add_command(label="Paste Text", command=getPText)
sourceMenu.add_command(label="Read image(JPG/JPEG)*", command=getImage)
var1 = IntVar() #stem
var2 = IntVar() #SF
var3 = IntVar(value=1) #log
var4 = IntVar() #time display
#var5 = IntVar() #conc
var6 = IntVar(value=2) #textRank #personal algo
prefMenu = Menu(menubar, tearoff=0)
menubar.add_cascade(label="Preferences", menu=prefMenu)
prefMenu.add_radiobutton(label="Text Rank Algorithm", var=var6, value=1)
prefMenu.add_radiobutton(label="Algorithm 2", var=var6, value=2)
#prefMenu.add_radiobutton(label="Algorithm 3", var=var6, value=3)
prefMenu.add_separator()
prefMenu.add_command(label="Set Reduction ratio", command=getRR)
prefMenu.add_separator()
#prefMenu.add_checkbutton(label="Concurrency*", var=var5)
prefMenu.add_checkbutton(label="Use Stemming", var=var1)
prefMenu.add_checkbutton(label="Use Short Forms", var=var2)
prefMenu.add_separator()
prefMenu.add_checkbutton(label="Display time stats", var=var4)
prefMenu.add_checkbutton(label="Write to file", var=var3)
editmenu = Menu(menubar, tearoff=0)
menubar.add_cascade(label="Personalize", menu=editmenu)
editmenu.add_command(label="Edit Stop Words", command=showSW)
editmenu.add_command(label="Edit Short Forms*")
helpmenu = Menu(menubar, tearoff=0)
menubar.add_cascade(label="Help", menu=helpmenu)
helpmenu.add_command(label="Help", command=showHelp)
helpmenu.add_command(label="About", command=contact)
helpmenu.add_separator()
helpmenu.add_command(label="Feedback", command=contact)
#Menu Bar ends
#define elements
redRatio = Scale(root, from_=1, to=99, length=300, orient=HORIZONTAL)
redRatio.set(random.randint(1,99))
logLabel = Label(root, text="Log: ")
ipLabel = Label(root, text="Input Text: ")
ipEntry = Text(root, wrap=WORD, width=80, height=15)
opLabel = Label(root, text="Summary: ")
opEntry = Text(root, wrap=WORD, width=80, height=15, fg='#404247', bg='#c8cace')
wSearchEntry= Text(root, width=80, height=1)
wikiQLabel = Label(root, text="Enter Search Query")
buttonWiki = Button(root, text="Search", command=wikiSearch)
buttonWeb = Button(root, text="Search", command=displayWeb)
buttonSummary = Button(root, height=2, width=15, text="Generate Summary", command=getSummary, relief="groove")
buttonSummary.config()
buttonSave = Button(root,text="Save to File", command= updateSW)
buttonClear = Button(root, text="Clear screen", command=freshWindow, relief='groove')
updateRR= Button(root, text="Update Reduction Ratio", command=freshWindow)
#end of definitions
freshWindow()
root.config(menu=menubar)
root.mainloop()