0 / 0
Sentiment classification
Last updated: Jul 25, 2024
Sentiment classification

The Watson Natural Language Processing Sentiment classification models classify the sentiment of the input text.

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

Sentiment

The sentiment of text can be positive, negative or neutral.

The sentiment model computes the sentiment for each sentence in the input document. The aggregated sentiment for the entire document is also calculated using the sentiment transformer workflow.

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.

Sentence sentiment workflows

Workflow names

  • sentiment-aggregated_transformer-workflow_multilingual_slate.153m.distilled
  • sentiment-aggregated_transformer-workflow_multilingual_slate.153m.distilled-cpu

The sentiment-aggregated_transformer-workflow_multilingual_slate.153m.distilled workflow can be used on both CPUs and GPUs.

The sentiment-aggregated_transformer-workflow_multilingual_slate.153m.distilled-cpu workflow is optimized for CPU-based runtimes.

Code sample using the sentiment-aggregated_transformer-workflow_multilingual_slate.153m.distilled workflow

# Load the Sentiment workflow
sentiment_model = watson_nlp.load('sentiment-aggregated_transformer-workflow_multilingual_slate.153m.distilled-cpu')

# Run the sentiment model on the result of the syntax results
sentiment_result = sentiment_model.run('The rooms are nice. But the beds are not very comfortable.')

# Print the sentence sentiment results
print(sentiment_result)

Output of the code sample:

{
  "document_sentiment": {
    "score": -0.339735,
    "label": "SENT_NEGATIVE",
    "mixed": true,
    "sentiment_mentions": [
      {
        "span": {
          "begin": 0,
          "end": 19,
          "text": "The rooms are nice."
        },
        "sentimentprob": {
          "positive": 0.9720447063446045,
          "neutral": 0.011838269419968128,
          "negative": 0.016117043793201447
        }
      },
      {
        "span": {
          "begin": 20,
          "end": 58,
          "text": "But the beds are not very comfortable."
        },
        "sentimentprob": {
          "positive": 0.0011594508541747928,
          "neutral": 0.006315878126770258,
          "negative": 0.9925248026847839
        }
      }
    ]
  },
  "targeted_sentiments": {
    "targeted_sentiments": {},
    "producer_id": {
      "name": "Aggregated Sentiment Workflow",
      "version": "0.0.1"
    }
  },
  "producer_id": {
    "name": "Aggregated Sentiment Workflow",
    "version": "0.0.1"
  }
}

Targets sentiment extraction

Targets sentiment extraction 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.

Targets sentiment workflows

Workflow names

  • targets-sentiment_transformer-workflow_multilingual_slate.153m.distilled
  • targets-sentiment_transformer-workflow_multilingual_slate.153m.distilled-cpu

The targets-sentiment_transformer-workflow_multilingual_slate.153m.distilled workflow can be used on both CPUs and GPUs.

The targets-sentiment_transformer-workflow_multilingual_slate.153m.distilled-cpu workflow is optimized for CPU-based runtimes.

Code sample for the targets-sentiment_transformer-workflow_multilingual_slate.153m.distilled workflow

import watson_nlp
# Load Targets Sentiment model for English
targets_sentiment_model = watson_nlp.load('targets-sentiment_transformer-workflow_multilingual_slate.153m.distilled')
# Run the targets sentiment model on the input text
targets_sentiments = targets_sentiment_model.run('The rooms are nice, but the bed was not very comfortable.')
# Print the targets with the associated sentiment
print(targets_sentiments)

Output of the code sample:

{
  "targeted_sentiments": {
    "rooms": {
      "score": 0.990798830986023,
      "label": "SENT_POSITIVE",
      "mixed": false,
      "sentiment_mentions": [
        {
          "span": {
            "begin": 4,
            "end": 9,
            "text": "rooms"
          },
          "sentimentprob": {
            "positive": 0.990798830986023,
            "neutral": 0.0,
            "negative": 0.00920116901397705
          }
        }
      ]
    },
    "bed": {
      "score": -0.9920912981033325,
      "label": "SENT_NEGATIVE",
      "mixed": false,
      "sentiment_mentions": [
        {
          "span": {
            "begin": 28,
            "end": 31,
            "text": "bed"
          },
          "sentimentprob": {
            "positive": 0.00790870189666748,
            "neutral": 0.0,
            "negative": 0.9920912981033325
          }
        }
      ]
    }
  },
  "producer_id": {
    "name": "Transformer-based Targets Sentiment Extraction Workflow",
    "version": "0.0.1"
  }
}

Parent topic: Watson Natural Language Processing task catalog

Generative AI search and answer
These answers are generated by a large language model in watsonx.ai based on content from the product documentation. Learn more