Skip to main content

Sentiment Analysis

The Sentiment Analysis extension helps you to understand the tone or sentiment of a message.

Extension settings

  1. Login to CometChat and select your app.
  2. Go to the Extensions section and enable the Sentiment Analysis extension.
  3. Open up Settings and choose to Drop messages with Negative sentiments.

How does it work?

A message can be classified into 4 categories:

  1. Positive
  2. Neutral
  3. Negative
  4. Mixed

Along with these categories, we specify the confidence for that category, on a scale of 0 to 100.

The sentiment about the message can be found in the metadata object as shown below:

"@injected": {
"extensions": {
"sentiment-analysis": {
"sentiment": "positive",
"sentiment_score": {
"positive": 95,
"neutral": 4,
"negative": 0,
"mixed": 0
}
}
}
}

If the sentiment-analysis key is missing, then either the extension is not enabled or has timed out.

Sentiment analysis extension is compatible with the languages listed below

Supported languages
German (de)
English (en)
Spanish (es)
Italian (it)
Portuguese (pt)
French (fr)
Japanese (ja)
Korean (ko)
Hindi (hi)
Arabic (ar)
Chinese (simplified) (zh)
Chinese (traditional) (zh-TW)

Implementation

Using this information, you can show either a warning or drop the message completely. Here is how Twitter shows a message:

Image

At the recipients' end, from the message object, you can fetch the metadata by calling the getMetadata() method. Using this metadata, you can fetch the sentiment of the message.

var metadata = message.getMetadata();
if (metadata != null) {
var injectedObject = metadata["@injected"];
if (injectedObject != null && injectedObject.hasOwnProperty("extensions")) {
var extensionsObject = injectedObject["extensions"];
if (
extensionsObject != null &&
extensionsObject.hasOwnProperty("sentiment-analysis")
) {
var sentimentAnalysisObject = extensionsObject["sentiment-analysis"];
var sentiment = sentimentAnalysisObject["sentiment"];
if (sentimentAnalysisObject.hasOwnProperty("sentiment_score")) {
var positive = sentimentAnalysisObject["positive"];
var neutral = sentimentAnalysisObject["neutral"];
var negative = sentimentAnalysisObject["negative"];
var mixed = sentimentAnalysisObject["mixed"];
}
}
}
}