1
$\begingroup$

I'm trying to get a sentence's sentiment score using Spacy and apparently every sentence I pass gets a score of 0.0. Can someone help me understand what's going wrong here?

import spacy

nlp = spacy.load("en_core_web_sm")
doc1 = nlp("This Movie is really Great!")
doc2 = nlp("This Movie was the Worst!")

print("Sentiment score for 1st Sentence ",doc1.sentiment)
print("Sentiment score for 2nd Sentence ",doc2.sentiment)

for token in doc1:
    print(token.text, token.pos_, token.dep_)


for token in doc2:
    print(token.text, token.pos_, token.dep_)

Which gives the output as:

Sentiment score for 1st Sentence  0.0
Sentiment score for 2nd Sentence  0.0
This DET det
Movie PROPN nsubj
is AUX ROOT
really ADV advmod
Great ADJ acomp
! PUNCT punct
This DET det
Movie PROPN nsubj
was AUX ROOT
the DET det
Worst ADJ attr
! PUNCT punct

Spacy's documentation says this about Doc.sentiment:

enter image description here

$\endgroup$

1 Answer 1

1
$\begingroup$

The default Spacy English model en_core_web_sm does not support sentiment analysis (despite class Doc having a sentiment property).

To add a sentiment analysis component to your Spacy pipeline, you can use the TextBlob library, by means of the spacytextblob Spacy component:

import spacy
from spacytextblob.spacytextblob import SpacyTextBlob

nlp = spacy.load('en_core_web_sm')
text = "I had a really horrible day. It was the worst day ever! But every now and then I have a really good day that makes me happy."
nlp.add_pipe("spacytextblob")
doc = nlp(text)

print(doc._.blob.polarity)
# -0.125

print(doc._.blob.subjectivity)
# 0.9

print(doc._.blob.sentiment_assessments.assessments)
# [(['really', 'horrible'], -1.0, 1.0, None), (['worst', '!'], -1.0, 1.0, None), (['really', 'good'], 0.7, 0.6000000000000001, None), (['happy'], 0.8, 1.0, None)]
$\endgroup$
1
  • $\begingroup$ Thanks this is really helpful! I'll play around further with this; I'm interested in getting an aggregated score for each sentence and use it to determine the sentiment of the sentence. $\endgroup$ Commented Sep 17, 2023 at 13:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.