I am facing issue with my Custom Policy where it checks for any spelling errors and display the user with “Did you mean ”, to utter the message I have created a custom action and invoked it in the policy but action is being triggered continuously without break.
SpellCheckPolicy.py:
class SpellCheckPolicy(Policy):
""" This policy handles low NLU confidence in multiple stages."""
SPELL_CHECK_POLICY_PRIORITY = 6
def __init__(
self,
priority: int = SPELL_CHECK_POLICY_PRIORITY,
spell_check_action_name: Text = "action_spell_check",
) -> None:
super().__init__(
priority=priority,
)
self.priority = priority
self.spell_check_action_name = spell_check_action_name
def train(
self,
training_trackers: List[DialogueStateTracker],
domain: Domain,
**kwargs: Any,
) -> None:
"""Does nothing. This policy is deterministic."""
pass
def persist(self, path: Text) -> None:
"""Persists the policy to storage."""
config_file = os.path.join(path, "spellCheck_policy.json")
meta = {
"priority": self.priority,
"spell_check_action_name": self.spell_check_action_name,
}
rasa.utils.io.create_directory_for_file(config_file)
rasa.utils.io.dump_obj_as_json_to_file(config_file, meta)
def predict_action_probabilities(
self, tracker: DialogueStateTracker, domain: Domain
) -> List[float]:
"""Predicts the next action if NLU confidence is low.
"""
print("Policyy--",tracker.latest_message.parse_data["text"])
mt = tracker.latest_message.parse_data["text"]
str = mt.translate(mt.maketrans('', '', '!\"#$%&\'()*+,.-:;<=>?@[\]^_`{|}~'))
words = str.split(' ')
beforeMessage = mt
spell = SpellChecker(language='en')
result = self._default_predictions(domain)
print("Spelll Check Policy Hit!!!!")
for word in words:
if word not in spell and len(word)>1:
mt = mt.replace(word, spell.correction(word))
print("Word after spell check", mt)
if beforeMessage is not mt:
idx = domain.index_for_action(self.spell_check_action_name)
result[idx] = 1.0
return result
@classmethod
def load(cls, path: Text) -> "SpellCheckPolicy":
meta = {}
if os.path.exists(path):
meta_path = os.path.join(path, "spellCheck_policy.json")
if os.path.isfile(meta_path):
meta = json.loads(rasa.utils.io.read_file(meta_path))
return cls(**meta)
@staticmethod
def _standard_featurizer() -> None:
return None
Config.yml
Action:
class SpellCheckAction(Action):
def name(self) -> Text:
return "action_spell_check"
async def run(self,
dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict]:
message = {
"text": f"SpellCheck Action Hit"
}
return [create_bot_utterance(message)]