-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSentimentBubble.py
57 lines (40 loc) · 1.77 KB
/
SentimentBubble.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
from textblob import TextBlob
import plotly
import orca
from dash import Dash, dcc, html, Input, Output
import plotly.express as px
from base64 import b64encode
import os
def subjectivity(x):
return TextBlob(x).sentiment.subjectivity
def polarity(x):
return TextBlob(x).sentiment.polarity
def sentimentbubble(data):
#Create two new columns
data['SUBJECTIVITY'] = data['REVIEW'].apply(subjectivity)
data['POLARITY'] = data['REVIEW'].apply(polarity)
#Create a function to compute the negative, neutral and positive analysis
def getAnalysis(score):
if score <0:
return 'Negative'
elif score == 0:
return 'Neutral'
else:
return 'Positive'
data['ANALYSIS'] = data['POLARITY'].apply(getAnalysis)
# plot the polarity and subjectivity
figure = px.scatter(data,x='POLARITY',y='SUBJECTIVITY',color = 'ANALYSIS',size='SUBJECTIVITY',width = 800, height = 400)
#add a vertical line at x=0 for Netural Reviews
figure.update_layout(title='Sentiment Analysis',
shapes=[dict(type= 'line',
yref= 'paper', y0= 0, y1= 1,
xref= 'x', x0= 0, x1= 0)])
# direc="static/bubble.png"
direc="templates/bubble.html"
#must download orca.exe from https://github.com/plotly/orca/releases
plotly.io.orca.config.executable= r"C:\Users\glady\AppData\Local\Programs\orca\orca.exe"
plotly.io.orca.config.save()
figure.write_html(direc)
# figure.write_image(direc,"png",engine="orca") if want to save as static image use this else writehtml as interactive
# figure.write_image(direc,"png",engine='orca')
# return '/static/bubble.png'