-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsentimentanalysis.py
More file actions
111 lines (89 loc) · 3.16 KB
/
Copy pathsentimentanalysis.py
File metadata and controls
111 lines (89 loc) · 3.16 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
# coding: utf-8
import tweepy
from tweepy import OAuthHandler
from textblob import TextBlob as tb
import pandas as pd
import json
from flask import jsonify
import datetime
from tweepy.error import TweepError
time = datetime.datetime.now()
consumerKey = 'SV8UUKCsWGbHB0fBG9xEWdDDl'
consumerSecret = 'gu7fZc75qzoZ20Cf1Y4FDSBGiX40H5L5dlMhmVqFecMZzUzBuo'
accessToken = '1074255938-ea90Lx6fUeZ6MW780cHpmmozr4XDmC47VH4cKpM'
accessTokenSecret = 'Npsz59fFPutq2aCbdfO6l0ylYjHZAgZxkojUoWHV8gsIh'
auth = tweepy.OAuthHandler(consumerKey, consumerSecret)
auth.set_access_token(accessToken, accessTokenSecret)
api = tweepy.API(auth)
def scrape_tweet(searchTweet, no_of_tweet):
try:
tweets = tweepy.Cursor(api.search, q=searchTweet).items((no_of_tweet))
except (TweepError, ValueError):
print("Too many requests")
data=[]
try:
for tweet in tweets:
text=tweet.text
tweet_time = tweet.created_at
textWords=text
analysis = tb(textWords)
polarity = 'Positive'
if(analysis.sentiment.polarity < 0):
polarity = 'Negative'
if(0<=analysis.sentiment.polarity <=0.2):
polarity = 'Neutral'
dic={}
dic['Sentiment']=polarity
dic['Tweet'] = textWords
dic['Tweettime'] = tweet_time
data.append(dic)
except UnboundLocalError:
pass
df=pd.DataFrame(data)
df.to_csv('devclan.csv')
train = pd.read_csv('devclan.csv', usecols = ['Sentiment', "Tweettime"])
# open('devclan.json', 'r') as f:
# train_json = json.loatrain.to_json('devclan.json')
# withd(f)
# positive = train['Sentiment'] == 'Positive'
# negative = train['Sentiment'] == 'Negative'
# neutral = train['Sentiment'] == 'Neutral'
Sentiment = train["Sentiment"]
size = Sentiment.tolist()
total = len(size)
pos = size.count("Positive")
neg = size.count("Negative")
neu = size.count("Neutral")
per_pos = float(pos/total* 100)
per_neg = float(neg/total* 100)
per_neu = float(neu/total* 100)
train_json ={"percentages":
{
"positive": round(per_pos, 3),
"negative":round(per_neg, 3),
"neutral":round(per_neu, 3)
},
"time" : time
}
train_json = jsonify(train_json)
# time = train['Tweettime'] .
# Sentiment = train['Sentiment']
# size = train['Sentiment'].value_counts().tolist()
# sice = Sentiment.tolist()
# labels = list(set(sice))
# colors = ['yellow', 'blue', 'red']
# explode = (0.1, 0.1, 0.1)
# explode_list = list(explode)
# if len(labels) == 2:
# colors = colors[:2]
# explode = explode[:2]
# elif len(labels) == 3:
# colors = colors
# explode = explode
# elif len(labels) == 1:
# colors = colors[:1]
# explode = explode[:1]
# plt.pie(size, colors=colors, labels=labels, shadow=True, startangle= 90, autopct='%1.1f%%', explode = explode)
# plt.legend(labels)
# return plt.show()
return train_json