-
Notifications
You must be signed in to change notification settings - Fork 0
/
SentimentAnalyzer.py
51 lines (43 loc) · 1.75 KB
/
SentimentAnalyzer.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
import pandas as pd
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
def convert_labels_to_vader_compound_scores(labels):
""" VADER scores are valued from -1 to 1 with -1 being negative and 1 being positive. """
labels_as_scores = []
for label in labels:
if label == 'positive':
labels_as_scores.append(0.66)
elif label == 'neutral':
labels_as_scores.append(0)
elif label == 'negative':
labels_as_scores.append(-0.66)
return labels_as_scores
def convert_vader_scores_to_classes(vader_scores):
"""
VADER scores are valued from -1 to 1 with -1 being negative and 1 being positive.
This method maps these to negative, neutral and positive.
"""
scores_as_classes = []
for score in vader_scores['compound']:
if score <= -0.05:
scores_as_classes.append('negative')
elif score >= 0.05:
scores_as_classes.append('positive')
else:
scores_as_classes.append('neutral')
return scores_as_classes
class SentimentAnalyzer:
def __init__(self):
# VADER
self.analyzer = SentimentIntensityAnalyzer()
def analyze(self, sentences):
vader_scores = pd.DataFrame([self.analyzer.polarity_scores(sentence) for sentence in sentences])
vader_scores_as_classes = convert_vader_scores_to_classes(vader_scores)
return vader_scores, vader_scores_as_classes
# print(f'VADER: {accuracy_score(vader_scores_as_classes, labels)}')
#
# # R2 calculation
# vader_compound_scores = [score['compound'] for score in vader_scores]
# labels_as_scores = convert_labels_to_vader_compound_scores(labels)
# print(f'VADER R2: {r2_score(labels_as_scores, vader_compound_scores)}')
#
# print('done')