CometChat Pro Documentation

You'll find comprehensive guides and documentation to help you start working with CometChat as quickly as possible. Let's jump right in!

Sentiment Analysis

Learn how to understand the sentiment of a message.

The Sentiment Analysis extension helps you to understand the sentiment of a message. A message can be classified into 4 categories- positive, neutral, negative and mixed. Along with these categories, we specify the confidence for that category, on a scale of 0 to 100.

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

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

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 : [String : Any]? = message.metaData
if metadata != nil {

  var injectedObject : [String : Any]? = (metadata?["@injected"] as? [String : Any])!
            
  if injectedObject != nil && (injectedObject!["extensions"] != nil){

    var extensionsObject : [String : Any]? = injectedObject?["extensions"] as? [String : Any]
                
    if extensionsObject != nil && extensionsObject?["sentiment-analysis"] != nil 		 {

      var sentimentAnalysisObject = extensionsObject?["sentiment-analysis"] as! [String :  Any]
                    
        let sentiment = sentimentAnalysisObject["sentiment"] as! String

        if sentimentAnalysisObject["sentiment_score"] {
                        
          let positive = imageModerationObject["positive"] as! Int
          let neutral = imageModerationObject["neutral"] as! Int
          let negative = imageModerationObject["negative"] as! Int
          let mixed = imageModerationObject["mixed"] as! Int
        }
      }
   }
}
 if (metadata != null) {
     if (metadata.has("@injected")) {
      val injectedJSONObject = metadata.getJSONObject("@injected")
      if (injectedJSONObject != null && injectedJSONObject.has("extensions")) {
      val extensionsObject = injectedJSONObject.getJSONObject("extensions")
      if (extensionsObject.has("sentiment-analysis")) {

      val sentimentObject = extensionsObject.getJSONObject("sentiment-analysis")
      val sentiment= sentimentObject.getString("sentiment")
      if (sentimentObject.has("sentiment_score")) {
      if (sentimentObject.has("positive"))
        val positive=sentimentObject.getInt("positive")
      if (sentimentObject.has("neutral"))
       val neutral= sentimentObject.getInt("neutral")
      if (sentimentObject.has("negative"))
       val negative= sentimentObject.getInt("negative")
      if (sentimentObject.has("mixed"))
        val mixed= sentimentObject.getInt("mixed")
      }         
     }
    }
   }
 }

Sentiment Analysis


Learn how to understand the sentiment of a message.

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.