-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhappiest_state.py
More file actions
280 lines (268 loc) · 7.09 KB
/
happiest_state.py
File metadata and controls
280 lines (268 loc) · 7.09 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
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
import sys
import json
import re
import time
def json_parse():
#parses json and takes only english tweets
#tfile = open('C:\Users\Dex\Documents\IPython Notebooks\cert\practweet.txt')
tfile=open(sys.argv[2])
tweet={}
#tweets={}
tweets=[]
num=0
#below goes through each line of the twitter json and then checks if its english
for t in tfile:
tweet=json.loads(t)
#num=num+1
try:
if tweet['lang']=='en':
#tweets[num]=tweet
tweets.append(tweet)
else:
pass
except KeyError:
pass
#print 'json'
#print time.time()
return tweets
def tweet_txt(tweets):
location=[]
nid=0
ldict={}
states = {
'AK': 'Alaska',
'AL': 'Alabama',
'AR': 'Arkansas',
'AS': 'American Samoa',
'AZ': 'Arizona',
'CA': 'California',
'CO': 'Colorado',
'CT': 'Connecticut',
'DC': 'District of Columbia',
'DE': 'Delaware',
'FL': 'Florida',
'GA': 'Georgia',
'GU': 'Guam',
'HI': 'Hawaii',
'IA': 'Iowa',
'ID': 'Idaho',
'IL': 'Illinois',
'IN': 'Indiana',
'KS': 'Kansas',
'KY': 'Kentucky',
'LA': 'Louisiana',
'MA': 'Massachusetts',
'MD': 'Maryland',
'ME': 'Maine',
'MI': 'Michigan',
'MN': 'Minnesota',
'MO': 'Missouri',
'MP': 'Northern Mariana Islands',
'MS': 'Mississippi',
'MT': 'Montana',
'NA': 'National',
'NC': 'North Carolina',
'ND': 'North Dakota',
'NE': 'Nebraska',
'NH': 'New Hampshire',
'NJ': 'New Jersey',
'NM': 'New Mexico',
'NV': 'Nevada',
'NY': 'New York',
'OH': 'Ohio',
'OK': 'Oklahoma',
'OR': 'Oregon',
'PA': 'Pennsylvania',
'PR': 'Puerto Rico',
'RI': 'Rhode Island',
'SC': 'South Carolina',
'SD': 'South Dakota',
'TN': 'Tennessee',
'TX': 'Texas',
'UT': 'Utah',
'VA': 'Virginia',
'VI': 'Virgin Islands',
'VT': 'Vermont',
'WA': 'Washington',
'WI': 'Wisconsin',
'WV': 'West Virginia',
'WY': 'Wyoming'
}
states={v:k for k,v in states.items()}
#takes the text of each tweet and converts it to utf8. creates a list with every status in there
status=[]
state=[]
for t in tweets:
status.append(t['text'].encode('utf-8'))
try:
if t['place']['country_code']=='US':
state.append(t['place']['full_name'])
else:
pass
except TypeError:
pass
nid=nid+1
for s in state:
if s.split(', ')[1]=='USA':
ldict[nid]=str(states[s.split(', ')[0]])
else:
ldict[nid]=s.split(', ')[1]
nid+=1
#print 'tweet_status'
#print time.time()
return (status,ldict)
def tweet_state(tweets,score):
location=[]
nid=0
ldict={}
states = {
'AK': 'Alaska',
'AL': 'Alabama',
'AR': 'Arkansas',
'AS': 'American Samoa',
'AZ': 'Arizona',
'CA': 'California',
'CO': 'Colorado',
'CT': 'Connecticut',
'DC': 'District of Columbia',
'DE': 'Delaware',
'FL': 'Florida',
'GA': 'Georgia',
'GU': 'Guam',
'HI': 'Hawaii',
'IA': 'Iowa',
'ID': 'Idaho',
'IL': 'Illinois',
'IN': 'Indiana',
'KS': 'Kansas',
'KY': 'Kentucky',
'LA': 'Louisiana',
'MA': 'Massachusetts',
'MD': 'Maryland',
'ME': 'Maine',
'MI': 'Michigan',
'MN': 'Minnesota',
'MO': 'Missouri',
'MP': 'Northern Mariana Islands',
'MS': 'Mississippi',
'MT': 'Montana',
'NA': 'National',
'NC': 'North Carolina',
'ND': 'North Dakota',
'NE': 'Nebraska',
'NH': 'New Hampshire',
'NJ': 'New Jersey',
'NM': 'New Mexico',
'NV': 'Nevada',
'NY': 'New York',
'OH': 'Ohio',
'OK': 'Oklahoma',
'OR': 'Oregon',
'PA': 'Pennsylvania',
'PR': 'Puerto Rico',
'RI': 'Rhode Island',
'SC': 'South Carolina',
'SD': 'South Dakota',
'TN': 'Tennessee',
'TX': 'Texas',
'UT': 'Utah',
'VA': 'Virginia',
'VI': 'Virgin Islands',
'VT': 'Vermont',
'WA': 'Washington',
'WI': 'Wisconsin',
'WV': 'West Virginia',
'WY': 'Wyoming'
}
states={v:k for k,v in states.items()}
for t in tweets:
#location.append(t['user']['location'])
try:
if t['place']['country_code']=='US':
state=t['place']['full_name']
if state.split(', ')[1]=='USA':
ldict[nid]=str(states[state.split(', ')[0]])
#print state.split(', ')[1]
else:
ldict[nid]=state.split(', ')[1]
else:
pass
except TypeError:
pass
nid=nid+1
#for s in score:
#print ldict
return ldict
def scorebystate(ldict, score):
nid=0
sstate={}
for l in ldict:
if ldict[l] in sstate:
sstate[ldict[l]]=float(score[nid]+sstate[ldict[l]])
else:
sstate[ldict[l]]=float(score[nid])
nid+=1
ans=[]
sstate=sorted(sstate, key=sstate.get, reverse=True)
#for s in sorted(sstate, key=sstate.get, reverse=True):
#print 'scorebystate'
#print time.time()
print sstate[0]
def sentiment(afindict,status):
#iterates over statuses then iterates over afindictionary to see if the entry in afindictionary is in the status. Resets score after each new status
statusdict={}
sid=0
for s in status:
score=0
ss=s.split(' ')
for sss in ss:
if sss in afindict.keys():
score=score+afindict[sss]
else:
pass
#for a in afindict.keys():
# aa='\\b'+a+'\\b'
# match=re.search((aa),s)
# if match:
# score=score+afindict[a]
# #print a
# else:
#s pass
#print s+'----->'+str(score)
# print score
#print '\n'
statusdict[sid]=score
sid=sid+1
#print 'sentiment'
#print time.time()
return statusdict
#print statusdict
def afin(file):
#parses afin file
#file = open('C:\Users\Dex\Documents\IPython Notebooks\cert\AFINN-111.txt')
file =open(sys.argv[1])
scores={} # initialize an empty dictionary
ascores={}
for line in file:
#note that when you have a multiple variable set. it will iterate over the variable for each line
term, score = line.split("\t") # The file is tab-delimted. "\t" means "tab character".
scores[term]=int(score) #convert the score to an integer.
ascores=scores.items() # print every (term,score) pair in the dictionary
#print 'afin'
#print time.time()
return scores
def main():
#finds sentiment score for the entire tweet. Divides the sentiment score by the amount of words, then assigns the weighted score to the non-afinn words
#file = open(sys.argv[1])
tweet_file = open(sys.argv[2])
afindict=afin(file)
tweets={}
tweets=json_parse()
status, ldict=tweet_txt(tweets)
score=sentiment(afindict,status)
#nonsentscore(afindict,status)
#freq=frequency(status)
#ldict=tweet_state(tweets,score)
scorebystate(ldict,score)
if __name__ == '__main__':
main()