Last active
October 8, 2020 12:16
-
-
Save nikhilcheke/589e242063f914ccf4dfa0da096ea28d to your computer and use it in GitHub Desktop.
Custom component for spell checking in Rasa NLU
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rasa.nlu.components import Component | |
from rasa.nlu import utils | |
from rasa.nlu.model import Metadata | |
from spellchecker import SpellChecker | |
spell = SpellChecker() | |
class CorrectSpelling(Component): | |
name = "Spell_checker" | |
provides = ["message"] | |
requires = ["message"] | |
language_list = ["en"] | |
def __init__(self, component_config=None): | |
super(CorrectSpelling, self).__init__(component_config) | |
def train(self, training_data, cfg, **kwargs): | |
"""Not needed, because the the model is pretrained""" | |
pass | |
def process(self, message, **kwargs): | |
"""Retrieve the text message, do spelling correction word by word, | |
then append all the words and form the sentence, | |
pass it to next component of pipeline""" | |
textdata = message.text | |
textdata = textdata.split() | |
new_message = ' '.join(spell.correction(w) for w in textdata) | |
message.text = new_message | |
def persist(self,file_name, model_dir): | |
"""Pass because a pre-trained model is already persisted""" | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment