-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab5.py
More file actions
142 lines (106 loc) · 3.03 KB
/
lab5.py
File metadata and controls
142 lines (106 loc) · 3.03 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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import json
import pip
pip.main(["install","twitter"])
pip.main(["install","textblob"])
from textblob import TextBlob
import twitter
from twitter import Twitter
from twitter import OAuth
from twitter import TwitterHTTPError
from twitter import TwitterStream
from pandas.io.json import json_normalize
from textblob import TextBlob
ck = "gwEdUQjvo9JDSLMpii205Q06O"
cs = "OlA4PIlLfpZ5HcEAGPOIJx4DwduhmACShoGQ66mJYt4ocxShwX"
at = "823996990329589760-1wW34frljiy7HrWp777qGFzOhEZksMW"
ats = "nzbKJCBUqlCsP6MNe5CFrOzZHo4wcvbubt1nJTqRkvFiB"
oauth = OAuth(at,ats,ck,cs)
api = Twitter(auth=oauth)
q1 = 'Trump'
q2 = 'Hillary'
q3 = 'BernieSanders'
search_results = api.search.tweets(q=q1,count = 100)
df1 = json_normalize(search_results, 'statuses')
search_results = api.search.tweets(q=q2,count = 100)
df2 = json_normalize(search_results, 'statuses')
search_results = api.search.tweets(q=q3,count = 100)
df3 = json_normalize(search_results, 'statuses')
s = np.empty(0)
p = np.empty(0)
for x in df1['text']:
w = TextBlob(x)
w.sentiment
s=np.append(s,w.subjectivity)
#print(w.subjectivity)
p=np.append(p,w.polarity)
s2 = np.empty(0)
p2 = np.empty(0)
for x in df2['text']:
w = TextBlob(x)
w.sentiment
s2=np.append(s2,w.subjectivity)
#print(w.subjectivity)
p2=np.append(p2,w.polarity)
s3 = np.empty(0)
p3 = np.empty(0)
for x in df3['text']:
w = TextBlob(x)
w.sentiment
s3=np.append(s3,w.subjectivity)
#print(w.subjectivity)
p3=np.append(p3,w.polarity)
subdf = pd.DataFrame({'Trump':s,'Hillary':s2,'Bernie':s3})
poldf = pd.DataFrame({'Trump':p,'Hillary':p2,'Bernie':p3})
poldf.plot()
subdf.plot()
lecture_5_problem_1.py
Long ago
Apr 22, 2018
You uploaded an item
Text
lecture_5_problem_1.py
import pip
pip.main(['install','numpy'])
pip.main(['install','pandas'])
import numpy as np
import pandas as pd
pip.main(['install','matplotlib'])
import matplotlib.pyplot as plt
import json
import pip
pip.main(["install","twitter"])
pip.main(["install","textblob"])
from textblob import TextBlob
import twitter
from twitter import Twitter
from twitter import OAuth
from twitter import TwitterHTTPError
from twitter import TwitterStream
from pandas.io.json import json_normalize
from textblob import TextBlob
ck = '4utyWgvDx6HziBbOOMrk9A8Zx'
cs = 'bOulyl2Uac0hGayZSDwrZm1o2ULkTZMLUYubOmEXSkwjPhYVJy'
at = '824423500991660032-6mBuL78cft4sIPx0dnEYrKiDNjIXZz1'
ats = 'fvg7ptaTu9cD5OqAYjCE4cIn3REDjOy6Fv8TP24aHCguD'
oauth = OAuth(at,ats,ck,cs)
api = Twitter(auth=oauth)
q = 'McMaster'
search_results = api.search.tweets(q=q,count = 100)
df = json_normalize(search_results, 'statuses')
s = np.empty(1)
p = np.empty(1)
#df = pd.DataFrame()
for x in df['text']:
w = TextBlob(x)
w.sentiment
s=np.append(s,w.subjectivity)
#print(w.subjectivity)
p=np.append(p,w.polarity)
df2 = pd.DataFrame({'subjectivity':s,'polarity':p})
df2.plot()
df['polarity']=p[1:101]
hidf = df[df['polarity']>=.7]
hisn=json_normalize(hidf['entities'],'user_mentions')