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?