-
Notifications
You must be signed in to change notification settings - Fork 0
/
combo_sentiment_paragraph.py
60 lines (49 loc) · 2.12 KB
/
combo_sentiment_paragraph.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
# updated version of the sentiment analysis script that trains using the AFINN, VADER, SentiWordNet, TextBlob, and Pattern lexicons
# and loops through all lines of a paragraph
# (does not produce an aggregated score anymore; see compute_aggregated_score.py)
import nltk
from nltk.sentiment import SentimentIntensityAnalyzer
from nltk.corpus import sentiwordnet as swn
from afinn import Afinn
from textblob import TextBlob
from pattern.en import sentiment as pattern_sentiment
def analyze_sentiment(text):
# Initialize the sentiment analyzers
sia = SentimentIntensityAnalyzer()
afinn = Afinn()
# Perform sentiment analysis using AFINN lexicon
afinn_score = afinn.score(text)
# Perform sentiment analysis using VADER
sentiment_scores = sia.polarity_scores(text)
# Perform sentiment analysis using SentiWordNet
sentiwordnet_scores = 0.0
tokens = nltk.word_tokenize(text)
for token in tokens:
synsets = list(swn.senti_synsets(token))
if synsets:
sentiwordnet_scores += synsets[0].pos_score() - synsets[0].neg_score()
# Perform sentiment analysis using TextBlob
blob = TextBlob(text)
textblob_polarity = blob.sentiment.polarity
# Perform sentiment analysis using Pattern
pattern_score = pattern_sentiment(text)[0]
return create_dictionary(afinn_score,sentiment_scores["compound"],sentiwordnet_scores,textblob_polarity,pattern_score)
def create_dictionary(a,b,c,d,e):
lexicon_scores = dict();
lexicon_scores['AFINN'] = a
lexicon_scores['VADER'] = b
lexicon_scores['SentiWordNet'] = c
lexicon_scores['TextBlob'] = d
lexicon_scores['Pattern'] = e
return lexicon_scores
def analyze_paragraph(paragraph):
lines = paragraph.split('\n')
lexicon_scores_dict = dict()
for i, line in enumerate(lines):
lexicon_scores = analyze_sentiment(line)
lexicon_scores_dict[i] = lexicon_scores
return lexicon_scores_dict
# Test run
paragraph = "I love this product.\nIt's amazing!\nI hate it though."
lexicon_scores_dict = analyze_paragraph(paragraph)
print('The lexicon score dictionaries for each line are:', lexicon_scores_dict)