-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
103 lines (83 loc) · 3.99 KB
/
main.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
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
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')
if userid == "" and hashtag == "":
error = "Please Enter any one value"
return render_template('index.html', error=error)
if not userid == "" and not hashtag == "":
error = "Both entry not allowed"
return render_template('index.html', error=error)
consumerKey = "dq6VCijZQHAN4MgDJx54bldUT"
consumerSecret = "9G0Lun6nWjVKoJdK158PSwt5Jwbg0Ej96RPBzXKINdIIS4XSHH"
accessToken = "1729477137751764992-upbotgoIetQHSqZkBkaoysyBPFgv4f"
accessTokenSecret = "wiZ6cqFdjQfjeVSLfrJ4DyXTUQhGakfFYALpxBcuVDBHD"
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 userid == "":
# hash tag coding
msgs = []
msg =[]
for tweet in tweepy.Cursor(api.search_tweets, q=hashtag).items(500):
msg = [tweet.text]
msg = tuple(msg)
msgs.append(msg)
df = pd.DataFrame(msgs)
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)
else:
# 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'])
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)
@app.route('/')
def home():
return render_template('index.html')
app.run(debug=True)