Sentiment classification block
The Watson Natural Language Processing Sentiment classification block classifies the sentiment of the input text.
Block name
The Watson Natural Language Processing library offers 2 Sentiment classification blocks:
sentiment_sentence-bert_multi_stock
targets-sentiment_sequence-bert_multi_stock
These Sentiment classification blocks are new and should be used in place of the deprecated Sentiment classification blocks described in Sentiment classification block (deprecated)
Supported languages
Sentiment classification is available for the following languages. For a list of the language codes and the corresponding language, see Language codes.
ar, cs, da, de, en, es, fi, fr, he, hi, it, ja, ko, nb, nl, nn, pl, pt, ro, ru, sk, sv, tr, zh-cn
Sentence sentiment block
The Sentence sentiment block classifies the sentiment of the input text. The identified sentiment can be positive, negative or neutral.
It computes the sentiment for each sentence in the input document. The aggregated sentiment for the entire document can be computed by the helper method called predict_document_sentiment
.
The classifications returned contain a probability. The sentiment score varies from -1 to 1. A score greater than 0 denotes a positive sentiment, a score less than 0 a negative sentiment, and a score of 0 a neutral sentiment.
Dependencies on other blocks
The following block must run before you can run the Sentence sentiment block:
syntax_izumo_<language>_stock
Code sample using the sentiment_sentence-bert_multi_stock
block
import watson_nlp
from watson_nlp.toolkit import predict_document_sentiment
# Load Syntax and a Sentiment model for English
syntax_model = watson_nlp.load(watson_nlp.download('syntax_izumo_en_stock'))
sentiment_model = watson_nlp.load(watson_nlp.download('sentiment_sentence-bert_multi_stock'))
# Run the syntax model on the input text
syntax_result = syntax_model.run('The rooms are nice. But the beds are not very comfortable.')
# Run the sentiment model on the result of the syntax results
sentiment_result = sentiment_model.run_batch(syntax_result.get_sentence_texts(), syntax_result.sentences)
# Print the sentence sentiment results
print(sentiment_result)
# Get the aggregated document sentiment
document_sentiment = predict_document_sentiment(sentiment_result, sentiment_model.class_idxs)
print(document_sentiment)
Output of the code sample:
[{
"score": 0.9540348989256836,
"label": "SENT_POSITIVE",
"sentiment_mention": {
"span": {
"begin": 0,
"end": 19,
"text": "The rooms are nice."
},
"sentimentprob": {
"positive": 0.919123649597168,
"neutral": 0.05862388014793396,
"negative": 0.022252488881349564
}
},
"producer_id": {
"name": "Sentence Sentiment Bert Processing",
"version": "0.1.0"
}
}, {
"score": -0.9772116371114815,
"label": "SENT_NEGATIVE",
"sentiment_mention": {
"span": {
"begin": 20,
"end": 58,
"text": "But the beds are not very comfortable."
},
"sentimentprob": {
"positive": 0.015949789434671402,
"neutral": 0.025898978114128113,
"negative": 0.9581512808799744
}
},
"producer_id": {
"name": "Sentence Sentiment Bert Processing",
"version": "0.1.0"
}
}]
{
"score": -0.335185,
"label": "SENT_NEGATIVE",
"mixed": true,
"sentiment_mentions": [
{
"span": {
"begin": 0,
"end": 19,
"text": "The rooms are nice."
},
"sentimentprob": {
"positive": 0.919123649597168,
"neutral": 0.05862388014793396,
"negative": 0.022252488881349564
}
},
{
"span": {
"begin": 20,
"end": 58,
"text": "But the beds are not very comfortable."
},
"sentimentprob": {
"positive": 0.015949789434671402,
"neutral": 0.025898978114128113,
"negative": 0.9581512808799744
}
}
]
}
Targets sentiment extraction block
The Targets sentiment extraction block extracts sentiments expressed in text and identifies the targets of those sentiments.
It can handle multiple targets with different sentiment in one sentence as opposed to the sentiment block described above.
For example, given the input sentence The served food was delicious, yet the service was slow., the Targets sentiment block identifies that there is a positive sentiment expressed in the target "food", and a negative sentiment expressed in "service".
The model has been fine-tuned on English data only. Although you can use the model on the other languages listed under Supported languages, the results might vary.
Dependencies on other blocks
The following block must run before you can run the Targets sentiment extraction block:
syntax_izumo_<language>_stock
Code sample using the sentiment-targeted_bert_multi_stock
block
import watson_nlp
# Load Syntax and the Targets Sentiment model for English
syntax_model = watson_nlp.load(watson_nlp.download('syntax_izumo_en_stock'))
targets_sentiment_model = watson_nlp.load(watson_nlp.download('targets-sentiment_sequence-bert_multi_stock'))
# Run the syntax model on the input text
syntax_result = syntax_model.run('The rooms are nice, but the bed was not very comfortable.')
# Run the targets sentiment model on the syntax results
targets_sentiments = targets_sentiment_model.run(syntax_result)
# Print the targets with the associated sentiment
print(targets_sentiments)
Output of the code sample:
{
"targeted_sentiments": {
"rooms": {
"score": 0.9989274144172668,
"label": "SENT_POSITIVE",
"mixed": false,
"sentiment_mentions": [
{
"span": {
"begin": 4,
"end": 9,
"text": "rooms"
},
"sentimentprob": {
"positive": 0.9989274144172668,
"neutral": 0.0,
"negative": 0.0010725855827331543
}
}
]
},
"bed": {
"score": -0.9977545142173767,
"label": "SENT_NEGATIVE",
"mixed": false,
"sentiment_mentions": [
{
"span": {
"begin": 28,
"end": 31,
"text": "bed"
},
"sentimentprob": {
"positive": 0.002245485782623291,
"neutral": 0.0,
"negative": 0.9977545142173767
}
}
]
}
},
"producer_id": {
"name": "BERT TSA",
"version": "0.0.1"
}
}
Parent topic: Watson Natural Language Processing block catalog