-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
129 lines (105 loc) · 5.38 KB
/
main.py
File metadata and controls
129 lines (105 loc) · 5.38 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
from flask import Flask,render_template, redirect, request
import numpy as np
import tweepy
import pandas as pd
from textblob import TextBlob
from wordcloud import WordCloud
import re
app = Flask(__name__)
@app.route('/sentiment', methods = ['GET','POST'])
def sentiment():
userid = request.form.get('userid')
hashtag = request.form.get('hashtag')
userenterinput = request.form.get('userenterinput')
if userid == "" and hashtag == "" and userenterinput=="":
error = "Please Enter any one value"
return render_template('index.html', error=error)
if not userid == "" and not hashtag == "" and not userenterinput == "":
error = "Both entry not allowed"
return render_template('index.html', error=error)
#======================Insert Twitter API Here==========================
consumerKey = "HL6hwVc6FmDBrxfGsj8hzmL4f"
consumerSecret = "EyojTeJcIKVpQFjCqFdI7UA59SL0EjzWrftDhtIxxTSid0NBJ3"
accessToken = "819227164423979009-mNeOGk1KqFsXO3XrBwbAopbk3D6v41P"
accessTokenSecret = "Dn8sQ4WZQF56MwLdEaPXdItyZ20Mlqyir0jysraixYykX"
#======================Insert Twitter API End===========================
authenticate = tweepy.OAuthHandler(consumerKey, consumerSecret)
authenticate.set_access_token(accessToken, accessTokenSecret)
api = tweepy.API(authenticate, wait_on_rate_limit = True)
def cleanTxt(text):
text = re.sub('@[A-Za-z0–9]+', '', text) #Removing @mentions
text = re.sub('#', '', text) # Removing '#' hash tag
text = re.sub('RT[\s]+', '', text) # Removing RT
text = re.sub('https?:\/\/\S+', '', text) # Removing hyperlink
return text
def getSubjectivity(text):
return TextBlob(text).sentiment.subjectivity
def getPolarity(text):
return TextBlob(text).sentiment.polarity
def getAnalysis(score):
if score < 0:
return 'Negative'
elif score == 0:
return 'Neutral'
else:
return 'Positive'
if hashtag:
# hash tag coding
msgs = []
msg =[]
for tweet in tweepy.Cursor(api.search, q=hashtag).items(500):
msg = [tweet.text]
msg = tuple(msg)
msgs.append(msg)
df = pd.DataFrame(msgs)
print(df)
df['Tweets'] = df[0].apply(cleanTxt)
df.drop(0, axis=1, inplace=True)
df['Subjectivity'] = df['Tweets'].apply(getSubjectivity)
df['Polarity'] = df['Tweets'].apply(getPolarity)
df['Analysis'] = df['Polarity'].apply(getAnalysis)
positive = df.loc[df['Analysis'].str.contains('Positive')]
negative = df.loc[df['Analysis'].str.contains('Negative')]
neutral = df.loc[df['Analysis'].str.contains('Neutral')]
positive_per = round((positive.shape[0]/df.shape[0])*100, 1)
negative_per = round((negative.shape[0]/df.shape[0])*100, 1)
neutral_per = round((neutral.shape[0]/df.shape[0])*100, 1)
return render_template('sentiment.html', name=hashtag,positive=positive_per,negative=negative_per,neutral=neutral_per)
elif userid:
# user coding
username = "@"+userid
post = api.user_timeline(screen_name=userid, count = 500, lang ="en", tweet_mode="extended")
twitter = pd.DataFrame([tweet.full_text for tweet in post], columns=['Tweets'])
print(twitter)
twitter['Tweets'] = twitter['Tweets'].apply(cleanTxt)
twitter['Subjectivity'] = twitter['Tweets'].apply(getSubjectivity)
twitter['Polarity'] = twitter['Tweets'].apply(getPolarity)
twitter['Analysis'] = twitter['Polarity'].apply(getAnalysis)
positive = twitter.loc[twitter['Analysis'].str.contains('Positive')]
negative = twitter.loc[twitter['Analysis'].str.contains('Negative')]
neutral = twitter.loc[twitter['Analysis'].str.contains('Neutral')]
positive_per = round((positive.shape[0]/twitter.shape[0])*100, 1)
negative_per = round((negative.shape[0]/twitter.shape[0])*100, 1)
neutral_per = round((neutral.shape[0]/twitter.shape[0])*100, 1)
return render_template('sentiment.html', name=username,positive=positive_per,negative=negative_per,neutral=neutral_per)
elif userenterinput:
username = "manualInput"
sentence = userenterinput.split(",")
data = {"Tweets":sentence}
df_user = pd.DataFrame(data,columns = ['Tweets'])
print(df_user)
df_user['Tweets'] = df_user['Tweets'].apply(cleanTxt)
df_user['Subjectivity'] = df_user['Tweets'].apply(getSubjectivity)
df_user['Polarity'] = df_user['Tweets'].apply(getPolarity)
df_user['Analysis'] = df_user['Polarity'].apply(getAnalysis)
positive = df_user.loc[df_user['Analysis'].str.contains('Positive')]
negative = df_user.loc[df_user['Analysis'].str.contains('Negative')]
neutral = df_user.loc[df_user['Analysis'].str.contains('Neutral')]
positive_per = round((positive.shape[0]/df_user.shape[0])*100, 1)
negative_per = round((negative.shape[0]/df_user.shape[0])*100, 1)
neutral_per = round((neutral.shape[0]/df_user.shape[0])*100, 1)
return render_template('sentiment.html', name=username,positive=positive_per,negative=negative_per,neutral=neutral_per)
@app.route('/')
def home():
return render_template('index.html')
app.run(debug=True)