Sentiment anlysis graph custom component

Hi, i implemented a pretrained model for sentiment analysis with vadersentiment.

def convert_to_rasa():
        entity = {"value": value,
                  "confidence": confidence,
                  "entity": "sentiment",
                  "extractor": "sentiment_extractor"}

        return entity

    def process(self, messages: List[Message]) -> List[Message]:
        # TODO: This is the method which Rasa Open Source will call during inference.
        sid = SentimentIntensityAnalyzer()
        for message in messages:
            res = sid.polarity_scores_max(message)
            key, value = max(res.items(), key=lambda x: x[1])
            entity = self.convert_to_rasa(key, value)
            messages.set("entities", [entity], add_to_output=True)
        return messages

I have this error and i couldn’t debug it :

  File "C:\Users\agar.blohorn\AppData\Roaming\Python\Python38\site-packages\rasa\engine\graph.py", line 461, in __call__
    output = self._fn(self._component, **run_kwargs)
  File "C:\Users\agar.blohorn\Documents\Analyse_sentiment_rasa\Rasa project\RASA v3\sentiment.py", line 59, in process
    res = sid.polarity_scores_max(message)
  File "C:\Anaconda\envs\rasaflow\lib\site-packages\vaderSentiment_fr\vaderSentiment.py", line 348, in polarity_scores_max
    for chr in text:
TypeError: 'Message' object is not iterable
rasa.engine.exceptions.GraphComponentException: Error running graph component for node run_sentiment.SentimentAnalyzer1.
2022-01-04 11:54:11 DEBUG    urllib3.connectionpool  - https://o251570.ingest.sentry.io:443 "POST /api/2801673/store/ HTTP/1.1" 200 41

Can you help me please

Hi @Hajer, from the error message TypeError: 'Message' object is not iterable, it looks like the function sid.polarity_scores_max is expecting text input. The Message object is a container holding different kinds of data on the user message. If you would like to retrieve the text you can get it by

from rasa.shared.nlu.constants import TEXT
text = message.get(TEXT)

Hope this helps :slight_smile:

1 Like

Thank you very much :slight_smile:

1 Like