In [2]:
from polyglot.downloader import downloader
print(downloader.supported_languages_table("sentiment2", 3))
In [3]:
from polyglot.text import Text
In [4]:
text = Text("The movie was really good.")
In [5]:
print("{:<16}{}".format("Word", "Polarity")+"\n"+"-"*30)
for w in text.words:
print("{:<16}{:>2}".format(w, w.polarity))
In [4]:
blob = ("Barack Obama gave a fantastic speech last night. "
"Reports indicate he will move next to New Hampshire.")
text = Text(blob)
First, we need split the text into sentneces, this will limit the words tha affect the sentiment of an entity to the words mentioned in the sentnece.
In [6]:
first_sentence = text.sentences[0]
print(first_sentence)
Second, we extract the entities
In [6]:
first_entity = first_sentence.entities[0]
print(first_entity)
Finally, for each entity we identified, we can calculate the strength of the positive or negative sentiment it has on a scale from 0-1
In [8]:
first_entity.positive_sentiment
Out[8]:
In [9]:
first_entity.negative_sentiment
Out[9]:
This work is a direct implementation of the research being described in the Building sentiment lexicons for all major languages paper. The author of this library strongly encourage you to cite the following paper if you are using this software.
@inproceedings{chen2014building,
title={Building sentiment lexicons for all major languages},
author={Chen, Yanqing and Skiena, Steven},
booktitle={Proceedings of the 52nd Annual Meeting of the Association for Computational Linguistics (Short Papers)},
pages={383--389},
year={2014}}