Overwrite user message (custom component)

Hello!

I am trying to create a custom component, which overwrite the user input message. I am working on rasa version 2.3.4. I have read the documentation, the forum discussions, and have considered the different suggestions, but I can’t get it to work!

Your input → Hee llo!!! my name is: DÁÑIEL, 35=YEARS old! someone on the other side?? rasa.core.processor - Received user message ‘Hee llo!!! my name is: DÁÑIEL, 35=YEARS old! someone on the other side??’

My custom component not overwrite the user input message.

my config.yml looks like this:

language: es

pipeline:

  • name: edit_message.Preprocessor
  • name: SpacyNLP
    • model: es_core_news_sm

and my custom component: import typing

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

import re

import unidecode

from rasa.nlu.components import Component

from rasa.nlu.config import RasaNLUModelConfig

from rasa.shared.nlu.training_data.training_data import TrainingData

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

if typing.TYPE_CHECKING:

from rasa.nlu.model import Metadata

class edit_message(Component):

provides = ["text"]

#requires = []

defaults = {}

language_list = None

def __init__(self, component_config=None):

    super(edit_message, self).__init__(component_config)

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

    pass

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

    mt = message.get("text")

    if mt!=None:

        mt = unidecode.unidecode(mt)

        mt = mt.lower()  

        mt = re.sub(r'(\!)', " ! ", mt)

        mt = re.sub(r'(\:)', " : ", mt)

        mt = re.sub('=', ' = ', mt)

        mt = re.sub(r'(\?)', " ? ", mt)

        mt = re.sub(r'\s{2,}', ' ', mt)

        message.set('text', mt, add_to_output = True)

    pass

Can anybody help me?

Hi @rosodelu

What exactly is the component doing? Do you see any errors in the logs. Also, if you formated your post better it would be easier to help.

I want to add a custom component, to replace certain characters. In order to improve the recognition of entities separated by these.

Example user input message:

He llooo! my name is: Dáñiel! 15=years old

It would be overwritten as:

he lloo ! my name is : daniel ! 15 = years old

Then I could put as an example of training data:

he lloo ! my name is : [daniel](name) ! [15](years) = years old

I’ve been trying to change this line:

message.set (‘text’, mt, add_to_output = True)

to

message.set (‘text’, ‘hello’, add_to_output = True)

and now i am sure my component is working, and is working as i want, as in that case all input messages will be overwritten as hello.

Thanks @jjuzl !!

1 Like

Thanks for sharing your solution. I’ve been wondering whether it was safe or not to override the TEXT key in message. Seems like it’s safe.