-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectGUI.py
More file actions
204 lines (146 loc) · 6.15 KB
/
Copy pathprojectGUI.py
File metadata and controls
204 lines (146 loc) · 6.15 KB
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
# Description: This program creates a graphical
# user interface that takes in the temperature in
# fahrenheit and the windspeed and displays the
# Windchill.
# Author: Michael Paluda
# imports the tkinter and tkinter.messagebox modules.
import tkinter
import tkinter.messagebox
import re
import wikipedia
import win32com.client
import pickle
import random
import threading
#
class SpeakerGUI:
summary = ''
entry = ''
temp = ''
def __init__(self):
# Creates the main window.
self.main_window = tkinter.Tk()
self.main_window.title("Speaker")
# Creates 5 frames to group the widgets.
self.titleframe = tkinter.Frame()
self.tempframe = tkinter.Frame()
self.buttonframe = tkinter.Frame()
self.outputframe = tkinter.Frame()
# Creates the label for the top frame.
self.title_label = tkinter.Label(self.titleframe, text = "Welcome to SummarAIze, the summary text to speak app!",font = ('verdana', 12))
# Packs the widgets for the top frame.
self.title_label.pack(side = 'top')
# Creates a label and a data entry widget for the temperature frame.
self.temp_label = tkinter.Label(self.tempframe, text = 'What do you want to hear about?: ')
self.temp_entry = tkinter.Entry(self.tempframe, width = 10)
# Packs the widgets for the temperature frame.
self.temp_label.pack(side = 'left')
self.temp_entry.pack(side = 'left')
entry = self.temp_entry.get()
# Creates a button widget for the button frame.
self.speakbutton = tkinter.Button(self.buttonframe, text = 'Speak', command = self.local)
self.printbutton = tkinter.Button(self.buttonframe, text = 'Print', command = self.change_value)
# Packs the widgets for the button frame.
self.speakbutton.pack(side = 'left')
self.printbutton.pack(side = 'right')
self.value = tkinter.StringVar()
self.outputlabel = tkinter.Label(self.outputframe, textvariable = self.value)
self.outputlabel.pack(side = 'left')
# Packs all of the frames.
self.titleframe.pack()
self.tempframe.pack()
self.buttonframe.pack()
self.outputframe.pack()
# Enters the tkinter main loop.
tkinter.mainloop()
# Callback function for the calc_button
def local(self):
# Gets the values entered by the user
# from the two entry widgets.
try:
temp = SpeakerGUI.entry
SpeakerGUI.entry = self.temp_entry.get()
page = wikipedia.page(SpeakerGUI.entry)
content = page.content
contentlist = content.split('== See also ==')
content = ''.join(contentlist[0])
content = re.sub('==', '', content)
content = re.sub('===', '', content)
content = re.sub('=', '', content)
self.train_markov(content)
if SpeakerGUI.summary == '' or SpeakerGUI.temp != SpeakerGUI.entry:
SpeakerGUI.summary = self.summarizer()
self.speak(SpeakerGUI.summary)
except:
tkinter.messagebox.showinfo("Error", "I could not find a wikipedia article " + self.temp_entry.get())
def train_markov(self, article):
text = article.split()
chain = {}
for i in range(len(text) - 3):
try:
'''
if text[i + 2] not in chain[text[i] + " " + text[i + 1]]:
'''
chain[text[i] + " " + text[i + 1]] += [text[i + 2]]
except:
chain[text[i] + " " + text[i + 1]] = [text[i + 2]]
if text[i] == "the":
try:
chain['the'] += [text[i + 1]]
except:
chain['the'] = [text[i + 1]]
stored = open('stored.txt', 'wb')
pickle.dump(chain, stored, 2)
stored.close()
def summarizer(self):
stored = open('stored.txt', 'rb')
chain = pickle.load(stored)
def nextWord(word):
if word not in chain.keys():
return 'the'
else:
return random.choice(chain[word])
numS = 10
# numS = int(input("How many sentences? "))
string = random.choice(list(chain.keys()))
response = string
# print(string)
# print(chain[string])
lastWord = string.split()[len(string.split()) - 1]
lastWord2 = string.split()[0]
while numS > 0:
# print("TEST: " + lastWord + " " + lastWord2)
temp = nextWord(lastWord2 + " " + lastWord)
# print("TEMP: " + temp)
response += " " + temp
string = temp
lastWord2, lastWord = lastWord, temp
if temp[:-1] in "?.!,":
numS -= 1
response += "\n"
# numS -=1
# print(response)
return response
def speak(self, string):
speaker = win32com.client.Dispatch("SAPI.SpVoice")
speaker.Speak(string)
def change_value(self):
try:
SpeakerGUI.temp = SpeakerGUI.entry
SpeakerGUI.entry = self.temp_entry.get()
page = wikipedia.page(SpeakerGUI.entry)
content = page.content
contentlist = content.split('== See also ==')
content = ''.join(contentlist[0])
content = re.sub('==', '', content)
content = re.sub('===', '', content)
content = re.sub('=', '', content)
self.train_markov(content)
if SpeakerGUI.summary == '' or SpeakerGUI.temp != SpeakerGUI.entry:
SpeakerGUI.temp = SpeakerGUI.entry
SpeakerGUI.summary = self.summarizer()
self.value.set(SpeakerGUI.summary)
except:
tkinter.messagebox.showinfo("Error", "I could not find a wikipedia article " + self.temp_entry.get())
# Creates an instance of the WindchillCalculatorGui class.
mygui = SpeakerGUI()