-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsentiment_analysis.py
128 lines (111 loc) · 4.01 KB
/
sentiment_analysis.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
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
import sys
import os
from textblob import TextBlob
import csv
import nltk
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import plotly.io as pio
from plotly.subplots import make_subplots
nltk.download('punkt')
college = "yale"
college_color_1 = '#0f4d92'
college_color_2 = 'Black'
def resource_path(relative_path):
try:
base_path = sys._MEIPASS
except Exception:
base_path = os.path.dirname(__file__)
return os.path.join(base_path, relative_path)
num_rows = 0
total_polarity = 0
total_subjectivity = 0
polarities = []
subjectivities = []
with open(resource_path(college + "_comments_final.csv"), 'r', encoding="utf8") as csvfile:
rows = csv.reader(csvfile)
for row in rows:
try:
sentence = row[1]
blob = TextBlob(sentence)
polarities.append(blob.sentiment.polarity)
subjectivities.append(blob.sentiment.subjectivity)
num_rows += 1
total_polarity += blob.sentiment.polarity
total_subjectivity += blob.sentiment.subjectivity
# print(blob.word_counts)
# print(sentence)
# print(blob.sentiment.polarity, blob.sentiment.subjectivity)
except:
# print("This line is blank.")
pass
print("Average Polarity for " + college.upper() +
": " + str(total_polarity/num_rows))
print("Average Subjectivity for " + college.upper() +
": " + str(total_subjectivity/num_rows))
# colors
monochrome_colors = ['#251616', '#760000', '#C63F3F', '#E28073', '#F1D3CF']
primary_colors = ['#C63F3F', '#F4B436', '#83BFCC', '#455574', '#E2DDDB']
# template
theme_hodp = go.layout.Template(
layout=go.Layout(
title={'font': {'size': 24, 'family': "Helvetica",
'color': monochrome_colors[0]}, 'pad': {'t': 100, 'r': 0, 'b': 0, 'l': 0}},
font={'size': 18, 'family': 'Helvetica', 'color': '#717171'},
xaxis={'ticks': "outside",
'tickfont': {'size': 14, 'family': "Helvetica"},
'showticksuffix': 'all',
'showtickprefix': 'last',
'showline': True,
'linewidth': 5,
'title': {'font': {'size': 18, 'family': 'Helvetica'}, 'standoff': 20},
'automargin': True
},
yaxis={'ticks': "outside",
'tickfont': {'size': 14, 'family': "Helvetica"},
'showticksuffix': 'all',
'showtickprefix': 'last',
'title': {'font': {'size': 18, 'family': 'Helvetica'}, 'standoff': 20},
'showline': True,
'linewidth': 5,
'automargin': True
},
legend={'bgcolor': 'rgba(0,0,0,0)',
'title': {'font': {'size': 18, 'family': "Helvetica", 'color': monochrome_colors[0]}},
'font': {'size': 14, 'family': "Helvetica"},
'yanchor': 'bottom'
},
colorscale={'diverging': monochrome_colors},
coloraxis={'autocolorscale': True,
'cauto': True,
'colorbar': {'tickfont': {'size': 14, 'family': 'Helvetica'}, 'title': {'font': {'size': 18, 'family': 'Helvetica'}}},
}
)
)
# fig = px.scatter(x=polarities, y=subjectivities)
# fig.show()
fig = go.Figure()
fig.add_trace(go.Scatter(
x=subjectivities,
y=polarities,
mode='markers',
marker=dict(
color=college_color_1,
size=15,
line=dict(
color=college_color_2,
width=2
)
)
# marker_color=primary_colors[2],
))
fig.update_layout(title=college.upper() + " Sentiment Analysis",
xaxis={
'title': {'text': 'Subjectivity (0 is objective, 1 is subjective)'}},
yaxis={
'title': {'text': 'Polarity (-1 is Negative, 0 is Neutral, 1 is Positive)'}},
# legend={'title': {'text': 'Political Party'}},
template=theme_hodp)
fig.show()