Pretrained sentiment component nltk

Hi there. I’m trying to use a pretrained sentiment component nltk mention in thi tutorial How to Enhance Rasa NLU Models with Custom Components | Rasa Blog | The Rasa Blog | Rasa but when i run rasa train I have this error

Traceback (most recent call last):
  File "C:\Users\FARES\anaconda3\envs\rasa2.8\lib\runpy.py", line 192, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\FARES\anaconda3\envs\rasa2.8\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\FARES\anaconda3\envs\rasa2.8\Scripts\rasa.exe\__main__.py", line 7, in <module>
  File "C:\Users\FARES\anaconda3\envs\rasa2.8\lib\site-packages\rasa\__main__.py", line 117, in main
    cmdline_arguments.func(cmdline_arguments)
  File "C:\Users\FARES\anaconda3\envs\rasa2.8\lib\site-packages\rasa\cli\train.py", line 59, in <lambda>
    train_parser.set_defaults(func=lambda args: run_training(args, can_exit=True))
  File "C:\Users\FARES\anaconda3\envs\rasa2.8\lib\site-packages\rasa\cli\train.py", line 91, in run_training
    training_result = train_all(
  File "C:\Users\FARES\anaconda3\envs\rasa2.8\lib\site-packages\rasa\api.py", line 109, in train
    return rasa.utils.common.run_in_loop(
  File "C:\Users\FARES\anaconda3\envs\rasa2.8\lib\site-packages\rasa\utils\common.py", line 296, in run_in_loop
    result = loop.run_until_complete(f)
  File "C:\Users\FARES\anaconda3\envs\rasa2.8\lib\asyncio\base_events.py", line 608, in run_until_complete
    return future.result()
  File "C:\Users\FARES\anaconda3\envs\rasa2.8\lib\site-packages\rasa\model_training.py", line 108, in train_async
    return await _train_async_internal(
  File "C:\Users\FARES\anaconda3\envs\rasa2.8\lib\site-packages\rasa\model_training.py", line 288, in _train_async_internal
    await _do_training(
  File "C:\Users\FARES\anaconda3\envs\rasa2.8\lib\site-packages\rasa\model_training.py", line 352, in _do_training
    await _train_core_with_validated_data(
  File "C:\Users\FARES\anaconda3\envs\rasa2.8\lib\site-packages\rasa\model_training.py", line 549, in _train_core_with_validated_data
    await rasa.core.train.train(
  File "C:\Users\FARES\anaconda3\envs\rasa2.8\lib\site-packages\rasa\core\train.py", line 70, in train
    agent.train(training_data, **additional_arguments)
  File "C:\Users\FARES\anaconda3\envs\rasa2.8\lib\site-packages\rasa\core\agent.py", line 753, in train
    self.policy_ensemble.train(
  File "C:\Users\FARES\anaconda3\envs\rasa2.8\lib\site-packages\rasa\core\policies\ensemble.py", line 206, in train
    policy.train(
  File "C:\Users\FARES\anaconda3\envs\rasa2.8\lib\site-packages\rasa\core\policies\ted_policy.py", line 676, in train
    model_data, label_ids = self._prepare_for_training(
  File "C:\Users\FARES\anaconda3\envs\rasa2.8\lib\site-packages\rasa\core\policies\ted_policy.py", line 570, in _prepare_for_training
    tracker_state_features, label_ids, entity_tags = self._featurize_for_training(
  File "C:\Users\FARES\anaconda3\envs\rasa2.8\lib\site-packages\rasa\core\policies\policy.py", line 191, in _featurize_for_training
    state_features, label_ids, entity_tags = self.featurizer.featurize_trackers(
  File "C:\Users\FARES\anaconda3\envs\rasa2.8\lib\site-packages\rasa\core\featurizers\tracker_featurizers.py", line 382, in featurize_trackers
    tracker_state_features = self._featurize_states(trackers_as_states, interpreter)
  File "C:\Users\FARES\anaconda3\envs\rasa2.8\lib\site-packages\rasa\core\featurizers\tracker_featurizers.py", line 103, in _featurize_states
    return [
  File "C:\Users\FARES\anaconda3\envs\rasa2.8\lib\site-packages\rasa\core\featurizers\tracker_featurizers.py", line 104, in <listcomp>
    [
  File "C:\Users\FARES\anaconda3\envs\rasa2.8\lib\site-packages\rasa\core\featurizers\tracker_featurizers.py", line 105, in <listcomp>
    self.state_featurizer.encode_state(state, interpreter)
  File "C:\Users\FARES\anaconda3\envs\rasa2.8\lib\site-packages\rasa\core\featurizers\single_state_featurizer.py", line 281, in encode_state
    self._extract_state_features(sub_state, interpreter, sparse=True)
  File "C:\Users\FARES\anaconda3\envs\rasa2.8\lib\site-packages\rasa\core\featurizers\single_state_featurizer.py", line 249, in _extract_state_features       
    parsed_message = interpreter.featurize_message(message)
  File "C:\Users\FARES\anaconda3\envs\rasa2.8\lib\site-packages\rasa\core\interpreter.py", line 159, in featurize_message
    result = self.interpreter.featurize_message(message)
  File "C:\Users\FARES\anaconda3\envs\rasa2.8\lib\site-packages\rasa\nlu\model.py", line 491, in featurize_message
    component.process(message, **self.context)
  File "E:\act\sentiment.py", line 44, in process
    res = sid.polarity_scores(message.Text)
AttributeError: 'Message' object has no attribute 'Text'

I need help please

and this is my sentiment.py file

from typing import Text
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()
        res = sid.polarity_scores(message.text)
        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

Now, I realised that this code (sentiment.py) works without problems with rasa 1.9 !!! but for me i use rasa 2.8 can someone tell me how to fix this problem?

@Fares For you I have seen this issue it related to message.text, can you please follow this thread and try sort your code? Message object does not recognize text attribute in Custom Component for RASA 2.0 - #11 by fabrice-toussaint

1 Like

thanks, @nik202 I changed message.text by message message.data[‘text’] and this is the error

  File "E:\act\sentiment.py", line 44, in process
    res = sid.polarity_scores(message.data['text'])
KeyError: 'text'

@Fares yes, its obvious, but seriously I not implemented this code :frowning: and I am trying to help you only dear!

thanks a lot @nik202 :slightly_smiling_face:

@Fares I know and trust you that you will crack this issue :slight_smile:

1 Like

@Juste can you please help me in this problem?

Finnaly, I solved this problem. I used this:

 res = sid.polarity_scores(str(message.get("text")))

instead of

 res = sid.polarity_scores(message.text)

and every thing is working now :muscle: :muscle:

@nik202 thank you very much for your responses and encouragement :muscle:.

@Juste It looks like you are not here :roll_eyes: It wasn’t the first time I mention you in other problem but you never respond :smirk: thank you any way

@Fares I told you and trust you, many congratulations :clap::clap:

1 Like

Hey @Fares. I am definitely here, my apologies for taking some time to look into this problem. I am very glad you found the solution for this. What I’ll do on my end is have a look at the tutorial and make the code updates as some things are obviously out of date there. :frowning:

2 Likes