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-document_bert_multi_stock
sentiment-targeted_bert_multi_stock
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
Capabilities
The Sentiment blocks encapsulates algorithms for the task of classifying the sentiment of the input text. The identified sentiment can be positive, negative or neutral.
The blocks return the following results:
sentiment-document_bert_multi_stock
computes the sentiment for each sentence in the input document, and an aggregated sentiment for the entire documentsentiment-targeted_bert_multi_stock
computes the sentiment for given target spans in the input text
The classifications returned contain a probability. The sentiment score varies from -1 to 1. A score greater than 0.01 denotes a positive sentiment, a score less than -0.01 a negative sentiment and a score between -0.01 and 0.01 a neutral sentiment.
Capabilities | Example | Sentiment |
---|---|---|
Computes document sentiment | "The rooms are nice. The staff is friendly. The location is at the river" | SENT_POSITIVE |
Computes sentence sentiment | "The rooms are nice. The staff is friendly. The location is at the river" | SENT_POSITIVE,SENT_POSITIVE,SENT_NEUTRAL |
Computes target sentiment for "room" | "The rooms are nice. The staff is friendly. The location is at the river" | SENT_POSITIVE |
Computes target sentiment for "river" | "The rooms are nice. The staff is friendly. The location is at the river" | SENT_NEUTRAL |
Computes target sentiment for "bed" | "The rooms are nice. The staff is friendly. The location is at the river" | SENT_UNSET |
Dependencies on other blocks
The following block must run before you can run the sentiment classification block:
syntax_izumo_<language>_stock
Code sample using the sentiment-document_bert_multi_stock
block
import watson_nlp
# 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-document_bert_multi_stock'))
# Run the syntax model on the input text
syntax_prediction = syntax_model.run('The rooms are nice. The staff is friendly. The location is at the river')
# Run the sentiment model on the result of syntax
sentiment_result = sentiment_model.run(syntax_prediction, sentence_sentiment=True)
# Print document sentiment, and sentiment per sentence
print(sentiment_result.prettify_document_sentiment())
print(sentiment_result.prettify_sentence_based_sentiment())
Output of the code sample:
{'label': 'Positive', 'mixed': False, 'score': 0.880813}
[{'label': 'Positive', 'score': 0.954035, 'target': 'The rooms are nice.'}, {'label': 'Positive', 'score': 0.944634, 'target': 'The staff is friendly.'}, {'label': 'Neutral', 'score': 0.0, 'target': 'The location is at the river'}]
Code sample using the sentiment-targeted_bert_multi_stock
block
import watson_nlp
# Load Syntax and a Sentiment model for English
syntax_model = watson_nlp.load(watson_nlp.download('syntax_izumo_en_stock'))
targeted_sentiment_model = watson_nlp.load(watson_nlp.download('sentiment-targeted_bert_multi_stock'))
# Run the syntax model on the input text
text = 'The rooms are nice. The staff is friendly. The location is at the river'
syntax_prediction = syntax_model.run(text)
# Extract the target spans for the target keywords
target_words = ('room', 'river', 'bed')
target_spans = watson_nlp.toolkit.get_target_spans(text, target_words)
# Run the sentiment model on the result of syntax and with the target spans
sentiment_result = targeted_sentiment_model.run(syntax_prediction, target_spans)
# Print document sentiment, and sentiment per sentence
print(sentiment_result.prettify_targeted_sentiment())
Output of the code sample:
[{'label': 'Positive', 'mixed': False, 'score': 0.954035, 'target': 'room'}, {'label': 'Neutral', 'mixed': False, 'score': 0.0, 'target': 'river'}, {'label': 'Unset', 'mixed': None, 'score': None, 'target': ''}]
Parent topic: Watson Natural Language Processing block catalog