Rasa 2.0, how to get user message in custom component for spell checking?

from rasa.nlu.components import Component

from rasa.nlu import utils

from rasa.nlu.model import Metadata

from rasa.shared.nlu.training_data.message import Message

from typing import Any, Optional, Text, List, Type, Dict

import pkg_resources

from symspellpy import SymSpell

sym_spell = SymSpell(max_dictionary_edit_distance=2, prefix_length=7)

dictionary_path = pkg_resources.resource_filename(

    "symspellpy", "frequency_dictionary_en_82_765.txt")

bigram_path = pkg_resources.resource_filename(

    "symspellpy", "frequency_bigramdictionary_en_243_342.txt")

# term_index is the column of the term and count_index is the

# column of the term frequency

sym_spell.load_dictionary(dictionary_path, term_index=0, count_index=1)

sym_spell.load_bigram_dictionary(bigram_path, term_index=0, count_index=2)

class CorrectSpelling(Component):

    name = "Spell_checker"

    provides = ["message"]

    requires = ["message"]

    language_list = ["en"]

    def __init__(self, component_config: Optional[Dict[Text, Any]] = None) -> None:

        super().__init__(component_config)

    

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

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

        pass

    def process(self, message: Message, **kwargs: Any) -> None:

        """Retrieve the text message, do spelling correction,

        pass it to next component of pipeline"""

        textdata = sym_spell.lookup_compound(message.data["text"], max_edit_distance=2)[0].term

        message.data["Text"] = textdata

    def persist(self, file_name: Text, model_dir: Text) -> Optional[Dict[Text, Any]]:

        """Persist this component to disk for future loading."""

        pass

​I am trying to write custom component for spellchecking to use in pipeline. In the process function, I use message.data[“text”] to get user message but it results in Key error. How can I get user message?

any help from Rasa team?

def process(self, message: Message, **kwargs: Any) -> None:

    """Retrieve the text message, do spelling correction,

    pass it to next component of pipeline"""

    textdata = sym_spell.lookup_compound(message.get("text"), max_edit_distance=2)[0].term

    message["text"] = textdata

I have updated the process function as above. This time it gives the below error:

TypeError: 'Message' object does not support item assignment

Hi @kurtsenol

Have you tried using the message.set method?

Hi @jjuzl. I use message.set('text', textdata, add_to_output=True) but this time I get below error:

AttributeError: ‘NoneType’ object has no attribute ‘lower’