-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtwitterScroller.py
56 lines (44 loc) · 1.51 KB
/
twitterScroller.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
from tweepy import Stream
from tweepy import OAuthHandler
import tweepy
from tweepy.streaming import StreamListener
from textblob import TextBlob
import pandas as pd
import re
consumer_key=""
consumer_secret=""
access_token=""
access_secret=""
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
pd = pd.DataFrame( columns=('Tweets', 'Polarity', 'Subjectivity'))
def CollectingTweets(query):
public_tweets = api.search(query)
tw = []
pl = []
su = []
pattern1 = re.compile('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+')
pattern2 = re.compile('RT @:?(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+')
for tweet in public_tweets:
tweet.text = pattern1.sub('', tweet.text)
tweet.text = pattern2.sub('', tweet.text)
str(tweet.text)
sent = TextBlob(tweet.text)
tw.append(tweet.text)
pl.append(sent.sentiment.polarity)
su.append(sent.sentiment.subjectivity)
pd['Tweets'] = tw
pd['Polarity'] = pl
pd['Subjectivity'] = su
return pd
def GetPolarityMean():
return pd['Polarity'].mean()
def GetSubjectivityMean():
return pd['Subjectivity'].mean()
def GetPolarityCount():
return pd[pd['Polarity']<=0].count().Polarity
def GetSubjectivityCount():
return pd[pd['Subjectivity']<=0].count().Subjectivity
def GetTweetCount():
return pd.count().Tweets