-
Notifications
You must be signed in to change notification settings - Fork 20
/
getters.py
25 lines (19 loc) · 870 Bytes
/
getters.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
"""
Getters extension for extracting sentiment.
"""
from spacy.tokens import Doc
from .vaderSentiment_da import SentimentIntensityAnalyzer
def da_vader_getter(doc: Doc, lemmatization: bool = True) -> dict:
"""A getter function for extracting polarity using the Danish implementation of Vader
Args:
doc (Doc): a SpaCy document
lemmatization (bool, optional): Should it use lemmatization of the document? Defaults to True.
Returns:
dict: a dictionary containing positive (pos), negative (neg), neutral (neu) polarity as well as a compound (compound)
"""
analyser = SentimentIntensityAnalyzer()
if lemmatization:
polarity = analyser.polarity_scores(doc.text, tokenlist=[t.lemma_ for t in doc])
else:
polarity = analyser.polarity_scores(doc.text, tokenlist=[t.text for t in doc])
return polarity