> ## Documentation Index
> Fetch the complete documentation index at: https://cometchat-013b37f0.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Sentiment Analysis (Legacy)

<Warning>
  **Legacy Notice**: This extension is considered legacy and is scheduled for deprecation in the near future. It is no longer recommended for new integrations.

  Please note: Legacy extensions are no longer actively maintained and will not receive feature updates or enhancements.
</Warning>

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

## Extension settings

1. Login to [CometChat](https://app.cometchat.com/login) 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:

<Tabs>
  <Tab title="JSON">
    ```json theme={null}
    "@injected": {
      "extensions": {
        "sentiment-analysis": {
          "sentiment": "positive",
          "sentiment_score": {
            "positive": 95,
            "neutral": 4,
            "negative": 0,
            "mixed": 0
          }
        }
      }
    }
    ```
  </Tab>
</Tabs>

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:

<Frame>
  <img src="https://mintcdn.com/cometchat-013b37f0/cjVyusI_GGBVqQQt/images/1bddee87-1623199851-9ba87f2af3672335e32d8d52bb3a11ca.png?fit=max&auto=format&n=cjVyusI_GGBVqQQt&q=85&s=1f9bf8ca53ef8c098608b36bdd0cd888" width="1070" height="786" data-path="images/1bddee87-1623199851-9ba87f2af3672335e32d8d52bb3a11ca.png" />
</Frame>

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.

<Tabs>
  <Tab title="JavaScript">
    ```js theme={null}
    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"];
          }
        }
      }
    }
    ```
  </Tab>

  <Tab title="Java">
    ```java theme={null}
    JSONObject metadata = message.getMetadata();
    if (metadata != null) {
      JSONObject injectedObject = metadata.getJSONObject("@injected");
      if (injectedObject != null && injectedObject.has("extensions")) {
        JSONObject extensionsObject = injectedObject.getJSONObject("extensions");
        if (extensionsObject != null && extensionsObject.has("sentiment-analysis"))
            {
              JSONObject sentimentAnalysisObject = extensionsObject.getJSONObject("sentiment-analysis");
              String sentiment = sentimentAnalysisObject.getString("sentiment");
              if(sentimentAnalysisObject.has("sentiment_score")){
                int positive = sentimentAnalysisObject.getInt("positive");
                int neutral = sentimentAnalysisObject.getInt("neutral");
                int negative = sentimentAnalysisObject.getInt("negative");
                int mixed = sentimentAnalysisObject.getInt("mixed");
              }

            }
        }
    }
    ```
  </Tab>

  <Tab title="Kotlin">
    ```kotlin theme={null}
    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")
          }         
         }
        }
       }
     }
    ```
  </Tab>

  <Tab title="Swift">
    ```swift theme={null}
    let textMessage = message as? TextMessage
    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
            }
          }
       }
    }
    ```
  </Tab>
</Tabs>
