-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainer.py
executable file
·94 lines (69 loc) · 3.41 KB
/
trainer.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
import os
import re
import operator
import data
# Map flu tweet to if it contains word [1 or 0] / number of flu tweets
# Reduce by adding
# This is P(W|F)
# Do map reduce again for healthy tweets to get P(W|H)
# P(F) and P(H) from total tweets
class Trainer():
# Categorize tweets as flu or healthy from the tweets that exists in tweets.txt
def categorizeTweets(self):
print("\n********************\nEntering categorization mode\n********************\n")
print("You will be prompted with tweets and asked to enter y if it is a flu tweet or n if it is not\n")
print("Enter q to quit and store the latest categorization at any time!\n")
# Loop from the last tweet categorized to the end
for i in range(data.tweetsCategorized, len(data.tweets)):
try:
print("\n" + data.tweets[i])
except UnicodeEncodeError:
continue
goodinput = 0
while(not goodinput):
inputstring = raw_input("Does this user have the Flu? (y/n/q): ")
if(inputstring == "y"):
data.categorization[i] = 1
goodinput = 1
elif(inputstring == "n"):
data.categorization[i] = 0
goodinput = 1
elif(inputstring == "q"):
print("Categorization saved to file!")
return
else:
print("Please enter y to for Flu, n for not Flu, or q to save and quit")
data.tweetsCategorized = len(data.tweets)
print("All tweets have been categorized!!\n")
def calculateProbs(self):
# Start with P(F) and P(H)
fluTweets = list()
healthTweets = list()
# Split tweets into two lists. Flu and not Flu
data.numFluTweets = 0
for tweetNum, cat in data.categorization.iteritems():
data.numFluTweets += cat
if(cat == 0):
healthTweets.append(data.tweets[tweetNum])
else:
fluTweets.append(data.tweets[tweetNum])
data.numHealthyTweets = data.tweetsStored - data.numFluTweets
data.probOfFlu = float(data.numFluTweets) / float(data.tweetsStored)
print(data.probOfFlu)
# Create a list of all words that occur in the training data
for tweet in data.tweets:
words = re.findall(r'\w+', tweet)
for word in words:
if(not word in data.words):
data.words.append(word)
# For each word:
for word in data.words:
# Get prob word given flu
isWordInFluTweets = map(lambda x: operator.contains(x, word)/float(data.numFluTweets), fluTweets)
probWordGivenFlu = reduce(operator.add, isWordInFluTweets)
# Get prob word given healthy
isWordInHealthTweets = map(lambda x: operator.contains(x, word)/float(data.numHealthyTweets), healthTweets)
probWordGivenHealth = reduce(operator.add, isWordInHealthTweets)
# Updata data in data
data.probabilities[word] = probWordGivenFlu, probWordGivenHealth
print("Probabilities calculated!!!")