Sentiment (aggregated)

At a glance

The Sentiment models are pre-trained classification models for classifying the sentiment of the input text. It can predict the document sentiment, each sentence's sentiment, and each target's sentiment. For example, in the text

I am so frustrated with this heavy traffic!

the model may predict negative as the document's sentiment. If the target word is frustrated, then the model could predict it as negative. The three possible predictions of this model are: positive, neutral, negative.

The Sentiment models are trained to be executed directly on the input text.

Class definition
watson_nlp.workflows.sentiment.aggregated_workflow.AggregatedSentiment

How to get document sentiment and targeted sentiment

The sentence sentiment model will split the whole document into sentences and return each sentence's sentiment. The document sentiment would then be calculated by aggregating all the sentence predictions in the document, while each target's sentiment would be calculated by aggregating the predictions from all the sentences that contain this target's span. Currently, the average is mostly calculated from all sentences' sentiment scores to get the final label.

Pretrained models

Model names are listed below. For language support, see Supported languages.

Model ID Container Image
BERT models
sentiment_aggregated-bert-workflow_lang_multi_stock cp.icr.io/cp/ai/watson-nlp_sentiment_aggregated-bert-workflow_lang_multi_stock:1.4.1
CNN models
sentiment_aggregated-cnn-workflow_lang_ar_stock cp.icr.io/cp/ai/watson-nlp_sentiment_aggregated-cnn-workflow_lang_ar_stock:1.4.1
sentiment_aggregated-cnn-workflow_lang_de_stock cp.icr.io/cp/ai/watson-nlp_sentiment_aggregated-cnn-workflow_lang_de_stock:1.4.1
sentiment_aggregated-cnn-workflow_lang_en_stock cp.icr.io/cp/ai/watson-nlp_sentiment_aggregated-cnn-workflow_lang_en_stock:1.4.1
sentiment_aggregated-cnn-workflow_lang_es_stock cp.icr.io/cp/ai/watson-nlp_sentiment_aggregated-cnn-workflow_lang_es_stock:1.4.1
sentiment_aggregated-cnn-workflow_lang_fr_stock cp.icr.io/cp/ai/watson-nlp_sentiment_aggregated-cnn-workflow_lang_fr_stock:1.4.1
sentiment_aggregated-cnn-workflow_lang_it_stock cp.icr.io/cp/ai/watson-nlp_sentiment_aggregated-cnn-workflow_lang_it_stock:1.4.1
sentiment_aggregated-cnn-workflow_lang_ja_stock cp.icr.io/cp/ai/watson-nlp_sentiment_aggregated-cnn-workflow_lang_ja_stock:1.4.1
sentiment_aggregated-cnn-workflow_lang_ko_stock cp.icr.io/cp/ai/watson-nlp_sentiment_aggregated-cnn-workflow_lang_ko_stock:1.4.1
sentiment_aggregated-cnn-workflow_lang_nl_stock cp.icr.io/cp/ai/watson-nlp_sentiment_aggregated-cnn-workflow_lang_nl_stock:1.4.1
sentiment_aggregated-cnn-workflow_lang_pt_stock cp.icr.io/cp/ai/watson-nlp_sentiment_aggregated-cnn-workflow_lang_pt_stock:1.4.1
sentiment_aggregated-cnn-workflow_lang_zh-cn_stock cp.icr.io/cp/ai/watson-nlp_sentiment_aggregated-cnn-workflow_lang_zh-cn_stock:1.4.1
aggregated-transformer-workflow
sentiment_aggregated-transformer-workflow_lang_multilingual_slate.153m.distilled cp.icr.io/cp/ai/watson-nlp_sentiment_aggregated-transformer-workflow_lang_multilingual_slate.153m.distilled:1.4.1
sentiment_aggregated-transformer-workflow_lang_multilingual_slate.153m.distilled-cpu cp.icr.io/cp/ai/watson-nlp_sentiment_aggregated-transformer-workflow_lang_multilingual_slate.153m.distilled-cpu:1.4.1
sentiment_aggregated-transformer-workflow_lang_multilingual_slate.270m cp.icr.io/cp/ai/watson-nlp_sentiment_aggregated-transformer-workflow_lang_multilingual_slate.270m:1.4.1
sentiment_aggregated-transformer-workflow_lang_multilingual_slate.270m-cpu cp.icr.io/cp/ai/watson-nlp_sentiment_aggregated-transformer-workflow_lang_multilingual_slate.270m-cpu:1.4.1

These models have been trained and tested on labeled data from IBM internal datasets from multiple domains: news reports, blogs, reviews and conversations.

The CNN models utilize GloVE embeddings.

The BERT model uses the Google Multilingual BERT Foundation Model as a base.

For details of the Sentiment type system, see Understanding model type systems.

Creating a custom model

For the current release of Watson Natural Language Processing Library for Embed, you can work with Python notebooks in Watson Studio to train some Watson NLP models with your own data. See Creating custom models for information.

Running models

The Sentiment model request accepts the following fields:

Field Type Required
Optional
Repeated
Description
raw_document watson_core_data_model.nlp.RawDocument required The input document on which to perform Classification predictions
language_code str optional An optional language code for the input text, required if the model is multi-lingual
document_sentiment bool optional Whether to aggregate the document sentiment
target_phrases str optional An optional collection of text-based targets on which to perform targeted analysis
show_neutral_scores bool optional Return scores that would have been flattened to neutral/zero

Example requests

REST API

curl -s \
  "http://localhost:8080/v1/watson.runtime.nlp.v1/NlpService/SentimentPredict" \
  -H "accept: application/json" \
  -H "content-type: application/json" \
  -H "Grpc-Metadata-mm-model-id: sentiment_aggregated-cnn-workflow_lang_en_stock" \
  -d '{ "raw_document": { "text": "Traffic is terrible this morning. It will take me two hours to get to work. I am so frustrated with this heavy traffic!" }, "show_neutral_scores": true, "document_sentiment": true, "target_phrases": ["traffic"] }'

Response

{"documentSentiment":{
  "score":-0.744097,
  "label":"SENT_NEGATIVE",
  "mixed":true,
  "sentimentMentions":[
    {"span":{"begin":0,"end":33,"text":"Traffic is terrible this morning."},"sentimentprob":{"positive":0.0032563824,"neutral":0.0363772,"negative":0.96036637}},
    {"span":{"begin":34,"end":75,"text":"It will take me two hours to get to work."},"sentimentprob":{"positive":0.3106174,"neutral":0.5123993,"negative":0.17698331}},
    {"span":{"begin":76,"end":119,"text":"I am so frustrated with this heavy traffic!"},"sentimentprob":{"positive":0.046196617,"neutral":0.0724822,"negative":0.88132125}}]},
 "targetedSentiments":{
  "targetedSentiments":{"Traffic":{
   "score":-0.954935,
   "label":"SENT_NEGATIVE",
   "mixed":false,
   "sentimentMentions":[
     {"span":{"begin":0,"end":33,"text":"Traffic is terrible this morning."},"sentimentprob":{"positive":0.0032563824,"neutral":0.0363772,"negative":0.96036637}},
     {"span":{"begin":76,"end":119,"text":"I am so frustrated with this heavy traffic!"},"sentimentprob":{"positive":0.046196617,"neutral":0.0724822,"negative":0.88132125}}]},
       "traffic":{
        "score":0,
        "label":"SENT_NEUTRAL",
        "mixed":false,
        "sentimentMentions":[]}},
    "producerId":{"name":"Aggregated Sentiment Workflow","version":"0.0.1"}},"producerId":{"name":"Aggregated Sentiment Workflow","version":"0.0.1"}}

Python

import grpc

from watson_nlp_runtime_client import (
    common_service_pb2,
    common_service_pb2_grpc,
    syntax_types_pb2,
)

channel = grpc.insecure_channel("localhost:8085")

stub = common_service_pb2_grpc.NlpServiceStub(channel)

request = common_service_pb2.SentimentRequest(
    raw_document=syntax_types_pb2.RawDocument(text="Traffic is terrible this morning. It will take me two hours to get to work. I am so frustrated with this heavy traffic!"),
    show_neutral_scores=True,
    document_sentiment=True,
    target_phrases=["traffic"]
)

  response = stub.SentimentPredict(
    request, metadata=[("mm-model-id", "sentiment_aggregated-cnn-workflow_lang_en_stock")]
)

print(response)

Response

document_sentiment {
  score: -0.744097
  label: SENT_NEGATIVE
  mixed: true
  sentiment_mentions {
    span {
      end: 33
      text: "Traffic is terrible this morning."
    }
    sentimentprob {
      positive: 0.00325638242
      neutral: 0.0363772
      negative: 0.960366368
    }
  }
  sentiment_mentions {
    span {
      begin: 34
      end: 75
      text: "It will take me two hours to get to work."
    }
    sentimentprob {
      positive: 0.310617387
      neutral: 0.512399316
      negative: 0.176983312
    }
  }
  sentiment_mentions {
    span {
      begin: 76
      end: 119
      text: "I am so frustrated with this heavy traffic!"
    }
    sentimentprob {
      positive: 0.0461966172
      neutral: 0.0724822
      negative: 0.881321251
    }
  }
}
targeted_sentiments {
  targeted_sentiments {
    key: "traffic"
    value {
      score: -0.954935
      label: SENT_NEGATIVE
      sentiment_mentions {
        span {
          end: 33
          text: "Traffic is terrible this morning."
        }
        sentimentprob {
          positive: 0.00325638242
          neutral: 0.0363772
          negative: 0.960366368
        }
      }
      sentiment_mentions {
        span {
          begin: 76
          end: 119
          text: "I am so frustrated with this heavy traffic!"
        }
        sentimentprob {
          positive: 0.0461966172
          neutral: 0.0724822
          negative: 0.881321251
        }
      }
    }
  }
  producer_id {
    name: "Aggregated Sentiment Workflow"
    version: "0.0.1"
  }
}
producer_id {
  name: "Aggregated Sentiment Workflow"
  version: "0.0.1"
}