Emotion

At a glance

Emotion models are pre-trained classification models for the task of classifying the emotion in the input document and the specified target words. For example, in the text

I hate school. School is bad.

the model may predict anger and sadness as the most prevalent emotions overall, and also predict the targeted emotion for specific mentions, for example: frustrated would have anger as the most prevalent emotion.

The Emotion model is trained using the Document classifier task. It requires a Syntax model to predict each sentence's emotion and aggregate the results to the final one. It is executed directly on the input text.

Class definition
watson_nlp.workflows.emotion.aggregated_classification_ensemble.AggregatedClassificationEnsemble

For language support, see Supported languages.

Pretrained models

Model names are listed below.

Model ID Container Image
emotion_aggregated-workflow_lang_en_stock cp.icr.io/cp/ai/watson-nlp_emotion_aggregated-workflow_lang_en_stock:1.4.1
emotion_aggregated-workflow_lang_fr_stock cp.icr.io/cp/ai/watson-nlp_emotion_aggregated-workflow_lang_fr_stock:1.4.1

The Emotion models have been trained on data from a variety of sources including social media conversations, social media commentary, TED talks, and movie dialogs. Data collection was varied, covering multiple industries from multiple geographical regions.

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

Running models

The Emotion model request accepts the following fields:

Field Type Required
Optional
Repeated
Description
raw_document watson_core_data_model.nlp.RawDocument or str required The input document on which to perform Emotion analysis
document_emotion bool optional Whether to perform emotion classification at the document level or not. Default is True
target_phrases str repeated An optional list of target strings or collection of text-based targets that will typically be provided by the user of this workflow directly.

Example requests

REST API

curl -s \
  "http://localhost:8080/v1/watson.runtime.nlp.v1/NlpService/EmotionPredict" \
  -H "accept: application/json" \
  -H "content-type: application/json" \
  -H "Grpc-Metadata-mm-model-id: emotion_aggregated-workflow_lang_en_stock" \
  -d '{ "raw_document": { "text": "I hate school. School is bad." }, "target_phrases": ["school"] }'

Response

{"emotionPredictions":[
    {"emotion":{"anger":0.41363257,"disgust":0.045076143,"fear":0.2293122,"joy":0.007663558,"sadness":0.4184458},
    "target":"school",
      "emotionMentions":[
        {"span":{"begin":0,"end":14,"text":"I hate school."},"emotion":{"anger":0.71405256,"disgust":0.048081085,"fear":0.06746074,"joy":0.012480628,"sadness":0.12677658}},
        {"span":{"begin":15,"end":29,"text":"School is bad."},"emotion":{"anger":0.11321261,"disgust":0.0420712,"fear":0.39116368,"joy":0.0028464883,"sadness":0.710115}}]},
          {"emotion":{"anger":0.41363257,"disgust":0.045076143,"fear":0.2293122,"joy":0.007663558,"sadness":0.4184458},
          "target":"",
          "emotionMentions":[
            {"span":{"begin":0,"end":14,"text":"I hate school."},"emotion":{"anger":0.71405256,"disgust":0.048081085,"fear":0.06746074,"joy":0.012480628,"sadness":0.12677658}},
            {"span":{"begin":15,"end":29,"text":"School is bad."},"emotion":{"anger":0.11321261,"disgust":0.0420712,"fear":0.39116368,"joy":0.0028464883,"sadness":0.710115}}]}],
    "producerId":{"name":"Ensemble Aggregated Emotion 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.EmotionRequest(
    raw_document=syntax_types_pb2.RawDocument(text="I hate school. School is bad."),
    document_emotion=False,
    target_phrases=["school"]
)

  response = stub.EmotionPredict(
    request, metadata=[("mm-model-id", "emotion_aggregated-workflow_lang_en_stock")]
)

print(response)

Response

emotion_predictions {
  emotion {
    anger: 0.413632572
    disgust: 0.045076143
    fear: 0.229312196
    joy: 0.00766355777
    sadness: 0.418445796
  }
  target: "school"
  emotion_mentions {
    span {
      end: 14
      text: "I hate school."
    }
    emotion {
      anger: 0.714052558
      disgust: 0.0480810851
      fear: 0.0674607381
      joy: 0.0124806277
      sadness: 0.126776576
    }
  }
  emotion_mentions {
    span {
      begin: 15
      end: 29
      text: "School is bad."
    }
    emotion {
      anger: 0.113212608
      disgust: 0.0420712
      fear: 0.391163677
      joy: 0.00284648826
      sadness: 0.710115
    }
  }
}
producer_id {
  name: "Ensemble Aggregated Emotion Workflow"
  version: "0.0.1"
}