# Why Sentiment Analysis cannot be tested?

**URL:** <https://forum.rasa.com/t/why-sentiment-analysis-cannot-be-tested/44414>\
**Category:** Rasa Open Source\
**Created:** [June 8, 2021, 5:36pm UTC](https://forum.rasa.com/t/why-sentiment-analysis-cannot-be-tested/44414 "2021-06-08T17:36:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alvin](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/alvin/32/21876_2.png) [@Alvin](https://forum.rasa.com/u/Alvin)\
**Post date:** [June 8, 2021, 5:36pm UTC](https://forum.rasa.com/t/why-sentiment-analysis-cannot-be-tested/44414/1 "2021-06-08T17:36:32Z")

</div>

Hello, I want to implement the sentiment analysis component in the rasa pipeline. But when I run the **Rasa Test** command with the model. The error shows as below.

Here is my error after running the model test:

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/rasa/original/2X/8/8a4a8168c64b392094403746dfbbb18f7901f51a.png)

Here is my sentiment analysis code:

from rasa.nlu.components import Component

from rasa.nlu import utils

from rasa.nlu.model import Metadata

import nltk

from nltk.sentiment.vader import SentimentIntensityAnalyzer

import os

class SentimentAnalyzer(Component):

```
"""A pre-trained sentiment component"""

name = "sentiment"

provides = ["entities"]

requires = []

defaults = {}

language_list = ["en"]

def __init__ (self, component_config=None):

    super(SentimentAnalyzer, self). __init__ (component_config)

def train(self, training_data, cfg, **kwargs):

    """Not needed, because the the model is pretrained"""

    pass

def convert_to_rasa(self, value, confidence):

    """Convert model output into the Rasa NLU compatible output format."""

    entity = {"value": value,

              "confidence": confidence,

              "entity": "sentiment",

              "extractor": "sentiment_extractor"}

    return entity

def process(self, message, **kwargs):

    """Retrieve the text message, pass it to the classifier

        and append the prediction results to the message class."""

    sid = SentimentIntensityAnalyzer()

    data = ""

    try:

        data = message.data['text']

    except KeyError:

        pass

    res = sid.polarity_scores(data)

    key, value = max(res.items(), key=lambda x: x[1])

    entity = self.convert_to_rasa(key, value)

    message.set("entities", [entity], add_to_output=True)

def persist(self, file_name, model_dir):

    """Pass because a pre-trained model is already persisted"""

    pass

```

Is it have any solution to solve this?

---

<div class="post-metadata">

**Author:** ![hsm207](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/hsm207/32/2647_2.png) [@hsm207](https://forum.rasa.com/u/hsm207)\
**Post date:** [June 9, 2021, 7:09am UTC](https://forum.rasa.com/t/why-sentiment-analysis-cannot-be-tested/44414/2 "2021-06-09T07:09:05Z")

</div>

Does the sentiment analysis custom component work as expected when you test in using `rasa shell nlu`?

---

<div class="post-metadata">

**Author:** ![Alvin](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/alvin/32/21876_2.png) [@Alvin](https://forum.rasa.com/u/Alvin)\
**Post date:** [June 9, 2021, 4:10pm UTC](https://forum.rasa.com/t/why-sentiment-analysis-cannot-be-tested/44414/3 "2021-06-09T16:10:35Z")

</div>

Yes, but sometimes the sentiment extractor does not give the accurate sentiment for the sentences.
