-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSentimentAnalyzer.py
55 lines (45 loc) · 1.88 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
52
53
54
55
from typing import Dict
from dostoevsky.models import FastTextSocialNetworkModel
from dostoevsky.tokenization import RegexTokenizer
tokenizer = RegexTokenizer()
model = FastTextSocialNetworkModel(tokenizer=tokenizer)
class SentimentData:
high_negative: float
low_negative: float
neutral: float
skip: float
speech: float
positive: float
def __init__(self, sentiment_data: Dict[str, float] = {'negative': 0,
'neutral': 0,
'positive': 0,
'speech': 0,
'skip': 0}):
if 'negative' in sentiment_data:
if sentiment_data['negative'] > 0.59:
self.high_negative = sentiment_data['negative']
self.low_negative = 0
else:
self.high_negative = 0
self.low_negative = sentiment_data['negative']
if 'neutral' in sentiment_data:
self.neutral = sentiment_data['neutral']
if 'positive' in sentiment_data:
self.positive = sentiment_data['positive']
if 'speech' in sentiment_data:
self.speech = sentiment_data['speech']
if 'skip' in sentiment_data:
self.skip = sentiment_data['skip']
def __add__(self, other):
self.high_negative += other.high_negative
self.low_negative += other.low_negative
self.skip += other.skip
self.speech += other.speech
self.positive += other.positive
self.neutral += other.neutral
return self
def get_negative(self) -> float:
return self.low_negative + self.high_negative
def get_sentiment(message: str) -> SentimentData:
sentiment = model.predict([message])[0]
return SentimentData(sentiment)