-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentiment_analysis.py
71 lines (53 loc) · 2.72 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
from matplotlib.pyplot import polar
from textblob import TextBlob
import re
import plotly.graph_objects as go
import plotly.express as px
def generate_polarity_graph(tweets, sliding_window_size=5):
graph_data = {"date":[], "sentiment":[]}
for tweet in tweets:
graph_data["date"].append(tweet["date"])
graph_data["sentiment"].append(get_polarity(tweet["content"]))
fig = go.Figure()
fig = px.scatter(x=graph_data["date"], y=graph_data["sentiment"], title="Positivity vs Time", color_continuous_scale='viridis', color = graph_data["sentiment"], template='plotly_dark')
smoothed_data = get_moving_average(graph_data["sentiment"], sliding_window_size)
fig.add_traces(list(px.line(x=graph_data["date"][sliding_window_size:], y=smoothed_data).select_traces()))
fig.update_layout(title_x=0.5, yaxis_title="Positivity", xaxis_title="Date", )
return fig
def generate_objectivity_graph(tweets, sliding_window_size=5):
graph_data = {"date":[], "objectivity":[]}
for tweet in tweets:
graph_data["date"].append(tweet["date"])
graph_data["objectivity"].append(get_objectivity(tweet["content"]))
fig = go.Figure()
fig = px.scatter(x=graph_data["date"], y=graph_data["objectivity"], title="Subjectivity vs Time", color_continuous_scale='viridis', color = graph_data["objectivity"], template='plotly_dark')
smoothed_data = get_moving_average(graph_data["objectivity"], sliding_window_size)
fig.add_traces(list(px.line(x=graph_data["date"][sliding_window_size:], y=smoothed_data).select_traces()))
fig.update_layout(title_x=0.5, yaxis_title="Subjectivity", xaxis_title="Date")
return fig
def get_polarity(tweet):
cleaned_tweet = ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)", " ", tweet).split())
polarity_val = TextBlob(cleaned_tweet).sentiment.polarity
return polarity_val
def get_objectivity(tweet):
cleaned_tweet = ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)", " ", tweet).split())
objectivity_val = TextBlob(cleaned_tweet).sentiment.subjectivity
return objectivity_val
def get_polarity_score(tweets):
polarity_sum = 0
for tweet in tweets:
polarity_sum += get_polarity(tweet["content"])
return polarity_sum/len(tweets)
def get_objectivity_score(tweets):
objectivity_sum = 0
for tweet in tweets:
objectivity_sum += get_objectivity(tweet["content"])
return objectivity_sum/len(tweets)
def get_moving_average(data, window_size=10):
i = window_size
moving_averages = []
while i < len(data):
window = data[i-window_size:i]
moving_averages.append(round(sum(window) / window_size, 2))
i+=1
return moving_averages