-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrequency.py
More file actions
158 lines (144 loc) · 4.02 KB
/
frequency.py
File metadata and controls
158 lines (144 loc) · 4.02 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
import sys
import json
import re
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[1])
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
return tweets
def tweet_txt(tweets):
#takes the text of each tweet and converts it to utf8. creates a list with every status in there
status=[]
for t in tweets:
status.append(t['text'].encode('utf-8'))
return status
def tweet_state(tweets):
location=[]
for t in tweets:
location.append(t['user'])
print location
return location
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={}
for s in status:
score=0
for a in afindict.keys():
aa='\\b'+a+'\\b'
match=re.search((aa),s)
if match:
score=score+afindict[a]
#print a
else:
pass
#print s+'----->'+str(score)
# print score
#print '\n'
return score
#print statusdict
def frequency(status):
freqdict={}
count=0
for s in status:
ssplit=s.split(' ')
for s in ssplit:
s=s.strip()
if s in freqdict:
freqdict[s]=1+freqdict[s]
else:
freqdict[s]=1
totfreq=0
for f in freqdict:
# print f, freqdict[f]
totfreq=totfreq+freqdict[f]
for f in freqdict:
perc=float(freqdict[f])/float(totfreq)
print f, perc
return freqdict
def nonsentscore(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
scoredict={}
nid=0
nafin={}
nid2=0
for s in status:
score=float(0)
ssplit=s.split(' ')
nword=[]
for s in ssplit:
num=float(0.0)
if s in afindict.keys():
score=score+afindict[s]
elif s not in afindict.keys():
nword.append(s)
#nword=s
#nafin[nword]=float(score/2)
else:
pass
num=num+1
nscore=score/(len(nword))
for n in nword:
nafin[n]=nscore
scoredict[nid]=score
#nscore=scoredict[nid]
#ssplit=s.split(' ')
#nlen=float(len(ssplit))
#wscore=float(nscore/nlen)
nid=nid+1
# print score
#print '\n'
for n in nafin:
print n, nafin[n]
return scoredict
#print statusdict
def afin():
#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
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
#sent_file = open('C:\Users\Dex\Documents\IPython Notebooks\cert\AFINN-111.txt')
#sent_file = open(sys.argv[1])
#sent_file=open('C:\dex\datascience\cert\AFINN-111.txt')
#tweet_file = open('C:\Users\Dex\Documents\IPython Notebooks\cert\practweet.txt')
#tweet_file=open('C:\dex\datascience\cert\practweet.txt')
tweet_file = open(sys.argv[1])
#hw()
#lines(sent_file)
#lines(tweet_file)
#afindict={}
#afindict=afin()
#print afindict.()
tweets={}
tweets=json_parse()
status=tweet_txt(tweets)
# sentiment(afindict,status)
#nonsentscore(afindict,status)
freq=frequency(status)
#location(tweets)
if __name__ == '__main__':
main()