Spacy polarity/sentiment method not working

I want to use spacy to get the polarity of some senteces, I have the following code for it:

import spacy


if __name__ == "__main__":

    nlp = spacy.load("en_core_web_lg")
    test_text = ['that is a great news','the book is very bad']

    sentiment = {'polarity':[]}
    for text in test_text:
        doc = nlp(unicode(text))
        print (doc.sentiment)

But I'm getting 0.0 for all cases.

Why can be that? obviously the polarity of both sentences is not the same

Upvotes: 2

Views: 2516

Answers (2)

kayuzee
kayuzee

Reputation: 125

A little old but I don't think you are accessing this correctly.

Spacy has the TexBlob component built in. Try add this as a pipe and then access the properties. https://spacy.io/universe/project/spacy-textblob

The code below should work but is untested.

import spacy
from spacytextblob.spacytextblob import SpacyTextBlob

if __name__ == "__main__":

    nlp = spacy.load("en_core_web_lg")
    nlp.add_pipe('spacytextblob')

    test_text = ['that is a great news','the book is very bad']

    for text in test_text:
        doc = nlp(unicode(text))
        print (doc._.polarity)`

Upvotes: 2

pmbaumgartner
pmbaumgartner

Reputation: 712

Though the documentation lists sentement as a document attribute, spaCy models do not come with a sentiment classifier. To do sentiment classification, you should first train your own model following this example.

Upvotes: 4

Related Questions