-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·346 lines (266 loc) · 11.4 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
# This is a final project for CS 4701 by Alex Rablau and Harry Davis
# Using twitter API
import os
import tweetstreamer
import data
import re
from trainer import Trainer
from learner import Learner
# Trainer class to train the model
trainer = Trainer()
# Learner class to learn from new data
learner = Learner()
def main():
# Welcome the user and ask them for what they want to do
print("************************************")
print("WELCOME TO THE TWITTER FLU DETECTOR!")
print("************************************")
# Read existing model into memory
readExistingModel()
# Program main loop
while(True):
print("\nOptions:")
print("1: Train Model\n2: Run Model\n3: Quit\n")
inputstring = raw_input("Enter option: ")
if(inputstring == "1"):
trainModel()
saveEverythingToFile()
elif(inputstring == "2"):
runModel()
elif(inputstring == "3"):
print("Stay Healthy!")
quit()
else:
print("\nThat is not an option!\n")
# Read in existing data
def readExistingModel():
if(os.path.exists("./probabilities.txt")):
probsFile = open("./probabilities.txt","r")
data.probOfFlu = float(probsFile.readline())
probsFile.close()
print("Trained model found!\n")
readProbabilitiesFromFile()
else:
print("No trained model found!")
pass
# Train the model
def trainModel():
# Check if probabilities already exist and ask user if they want to use these probs
if(len(data.probabilities) != 0):
print("Saved model already exists.\nUse this model?\n")
while(True):
inputstring = raw_input("(y/n): ")
if(inputstring == "y"):
print("Model trained!")
return
elif(inputstring == "n"):
data.probabilities.clear()
break
elif(inputstring == "q"):
print("Good bye!")
quit()
## TODO EXIT ##
else:
print("Please enter y, n, or q to quit")
# Read tweets and categorization data from files
if(os.path.exists("./tweets.txt")):
tweetsFile = open("./tweets.txt","r")
data.tweetsStored = int(tweetsFile.readline())
tweetsFile.close()
print("Found tweets.txt with " + str(data.tweetsStored) + " tweets in it!")
# Load tweets into memory
readTweetsFromFile()
# Check if training data already exists for these tweets
if(os.path.exists("./trainingdata.txt")):
trainingDataFile = open("./trainingdata.txt")
data.tweetsCategorized = int(trainingDataFile.readline())
trainingDataFile.close()
print("Found trainingdata.txt with " + str(data.tweetsCategorized) + " categorizations in it!")
if(data.tweetsStored < data.tweetsCategorized):
# We have more categorized tweets than tweets themselves. Error
print("Mismatch: more tweets are categorized than exist! Disregarding categorization data.")
data.tweetsCategorized = 0
else:
readCategorizationFromFile()
else:
print("No categorization data exists for these tweets!\n")
# Ask the user how many tweets they would like in the training set
print("Please enter the number of tweets you would like in the training set")
inputstring = raw_input(">> ")
tweetsWanted = int(inputstring)
# If we do not have enough tweets as the user requested
if(data.tweetsStored < tweetsWanted):
print(str(data.tweetsStored) + " tweets already stored.")
print(str(tweetsWanted - data.tweetsStored) + " more tweets needed.")
print("Append more tweets or overwrite and collect new tweets?")
while(True):
inputstring = raw_input("(a/o): ")
if(inputstring == "a"):
data.tweets.append(tweetstreamer.getTweets(tweetsWanted - data.tweetsStored, data.searchterms))
trainer.categorizeTweets()
break
elif(inputstring == "o"):
data.tweetsStored = 0
data.tweetsCategorized = 0
data.tweets = tweetstreamer.getTweets(tweetsWanted - data.tweetsStored, data.searchterms)
trainer.categorizeTweets()
break
elif(inputstring == "q"):
print("Good bye!")
## TODO EXIT ##
else:
print("Please enter a to append, o to overwrite, or q to quit")
# We have more than enough tweets
elif(data.tweetsStored >= tweetsWanted):
print(str(data.tweetsStored) + " tweets already stored.")
print("Use these tweets or overwrite and collect new tweets?")
while(True):
inputstring = raw_input("(u/o): ")
if(inputstring == "u"):
readCategorizationFromFile()
break
elif(inputstring == "o"):
data.tweetsStored = 0
data.tweetsCategorized = 0
data.tweets = tweetstreamer.getTweets(tweetsWanted - data.tweetsStored, data.searchterms)
trainer.categorizeTweets()
break
elif(inputstring == "q"):
print("Good bye!")
quit()
else:
print("Please enter a to append, o to overwrite, or q to quit")
# Now we have the tweets and some categorization or not
# Model is now trained
# Model is stored in probabilities
data.tweetsStored = len(data.tweets)
trainer.calculateProbs()
# Run the model
def runModel():
print("Model running mode!\n")
print("Collecting 10 tweets to iterate through!")
data.testTweets = tweetstreamer.getTweets(10)
print("Use learning?")
goodinput = 0
while(not goodinput):
inputstring = raw_input("(y/n): ")
if(inputstring == "y"):
useLearning = True
goodinput = 1
elif(inputstring == "n"):
useLearning = False
goodinput = 1
else:
print("Please enter y or n")
# runModel main loop
while(len(data.testTweets) != 0):
print("\n1: Evaluate new tweet\n2: Exit running mode")
inputstring = raw_input("Enter option: ")
if(inputstring == "1"):
newTweet = data.testTweets.pop()
wordsInTweet = re.findall(r'\w+', newTweet.lower())
probTweetFlu = probOfFlu(wordsInTweet)
isTweetFlu = probTweetFlu > data.THRESHOLD
if(isTweetFlu):
data.numFluTweets += 1
try:
print("\nNew tweet: " + str(newTweet))
print("Probability the user has flu: " + str(probTweetFlu))
except UnicodeEncodeError:
print("Codec cannot encode characters in this tweet!")
if(useLearning):
learner.updateProbs(wordsInTweet, isTweetFlu)
elif(inputstring == "2"):
return
else:
print("\nThat is not an option!\n")
print("All test tweets evaluated!!!")
# Calculate if this list of words indicates the flu
def probOfFlu(words):
print("Enter prob of flu")
probs = {}
productOfProbs = 1
productOfOneMinusProbs = 1
# Calculate P(Flu|Word) for each word
for w in words:
probFluGivenWord = fluGivenWord(w)
probs[w] = probFluGivenWord
# Calculate components of the final probability calculation
for k in probs:
productOfProbs *= probs[k]
productOfOneMinusProbs *= (1 - probs[k])
# Calculate probability
probHasFlu = (productOfProbs / (productOfProbs + productOfOneMinusProbs))
return probHasFlu
# Calculate P(Flu|Word)
def fluGivenWord(word):
probOfHealth = 1 - data.probOfFlu
if word not in data.probabilities:
data.probabilities[word] = (0.4, 0.4)
(probWordGivenFlu, probWordGivenHealth) = data.probabilities[word]
num = data.probOfFlu * probWordGivenFlu
denom = probWordGivenFlu * data.probOfFlu + probWordGivenHealth * probOfHealth
ans = num / denom
if(ans == 0.0):
ans = 0.01
elif(ans == 1.0):
ans = 0.99
return ans
def readTweetsFromFile():
tweetsFile = open("./tweets.txt", "r")
data.tweetsStored = int(tweetsFile.readline())
for line in tweetsFile:
data.tweets.append(line)
tweetsFile.close()
def readCategorizationFromFile():
trainingDataFile = open("./trainingdata.txt", "r")
data.tweetsCategorized = int(trainingDataFile.readline())
for line in trainingDataFile:
try:
tweetnumber, isFlu = line.split()
except ValueError:
print("trainingdata.txt is not properly formatted!")
return
data.categorization[int(tweetnumber)] = int(isFlu)
trainingDataFile.close()
def readProbabilitiesFromFile():
probsFile = open("./probabilities.txt", "r")
data.tweetsStored = float(probsFile.readline())
data.probOfFlu = float(probsFile.readline())
data.numFluTweets = data.tweetsStored * data.probOfFlu
data.numHealthyTweets = data.tweetsStored * (1 - data.probOfFlu)
for line in probsFile:
try:
word, probWordGivenFlu, probWordGivenHealthy = line.split()
except ValueError:
print("probabilities.txt is not properly formatted!")
return
data.probabilities[word.lower()] = float(probWordGivenFlu), float(probWordGivenHealthy)
probsFile.close()
def saveEverythingToFile():
saveTweetsToFile()
saveCategorizationToFile()
saveProbabilitiesToFile()
def saveTweetsToFile():
tweetsFile = open("./tweets.txt", "w")
tweetsFile.write(str(data.tweetsStored) + "\n")
for tweet in data.tweets:
tweetsFile.write(tweet + "\n")
tweetsFile.close()
def saveCategorizationToFile():
trainingDataFile = open("./trainingdata.txt", "w")
trainingDataFile.write(str(data.tweetsCategorized) + "\n")
for tweetNum, cat in data.categorization.iteritems():
trainingDataFile.write(str(tweetNum) + " " + str(cat) + "\n")
trainingDataFile.close()
def saveProbabilitiesToFile():
probabilitiesFile = open("./probabilities.txt", "w")
probabilitiesFile.write(str(data.tweetsStored) + "\n")
probabilitiesFile.write(str(data.probOfFlu) + "\n")
for word in data.words:
probWordGivenFlu, probWordGivenHealthy = data.probabilities[word]
probabilitiesFile.write(word + " " + str(probWordGivenFlu) + " " + str(probWordGivenHealthy) + "\n")
probabilitiesFile.close()
# Start the program
main()
print("DONE!")