-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_sentiment.py
More file actions
63 lines (51 loc) · 1.51 KB
/
text_sentiment.py
File metadata and controls
63 lines (51 loc) · 1.51 KB
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
from dostoevsky.tokenization import RegexTokenizer
from dostoevsky.models import FastTextSocialNetworkModel
from .sentiment import Sentiment
dostoevsky_tokenizer = RegexTokenizer()
dostoevsky_model = FastTextSocialNetworkModel(tokenizer=dostoevsky_tokenizer)
def process_label(label: str) -> Sentiment:
"""
Turn text label into instance of `Sentiment` class
Arguments:
----------
label: str
Original label provided by the model
Returns:
--------
Corresponding instance of `Sentiment` class
"""
if label == 'positive':
return Sentiment.POSITIVE
elif label == 'negative':
return Sentiment.NEGATIVE
else:
return Sentiment.NEUTRAL
def get_texts_sentiment(texts: list[str]) -> list[Sentiment]:
"""
Make sentiment predictions for texts
Arguments:
----------
texts: list[str]
List of texts to predict sentiment for
Returns:
--------
list[Sentiment]
Predictions for sentiments
"""
estimates = dostoevsky_model.predict(texts, k=1)
labels = map(lambda x: list(x.keys())[0], estimates)
labels = map(process_label, labels)
return list(labels)
def get_text_sentiment(text: str) -> Sentiment:
"""
Make sentiment prediction for single text
Arguments:
----------
text: str
Text to predict sentiment for
Returns:
--------
Sentiment
Prediction for sentiment
"""
return get_texts_sentiment([text])[0]