When trying to validade my form, I got no changes to the slot values that were supposed to be corrected. The idea here is to get the extracted slots to be clean and formated accordingly.
class ValidateHealthInsuranceForm(FormValidationAction):
def name(self) -> Text:
return "validate_health_insurance_form"
def validate_name(self, slot_value: Any, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> Dict[Text, Any]:
# Clean and Captalize
slot_value_clean = clean_text(slot_value)
print(f"Name={slot_value_clean}")
# Check if the response has irrelevant info
if re.search(r'\b(valor|preço|plano|idoso|idade|quanto|cidade)\b', slot_value_clean, re.IGNORECASE):
dispatcher.utter_message(text="Please fill the form correctly.")
return {"name": None} # Invalid data
return {"name": slot_value_clean}
def validate_city(self, slot_value: Any, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> Dict[Text, Any]:
# Clean and Captalize
slot_value_clean = clean_text(slot_value)
# remove accent
city_normalized = remove_accents(slot_value_clean.lower())
# Get the right names for cities
city = CITY_MAPPING.get(city_normalized, slot_value_clean)
if not city:
dispatcher.utter_message(text="Please fill it correctly.")
return {"city": None} # Invalid data
return {"city": city}
def validate_ages(self, slot_value: Any, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> Dict[Text, Any]:
cleaned_ages = process_ages(slot_value)
if not cleaned_ages:
dispatcher.utter_message(text="Please enter valid ages(ex.: 30, 35).")
return {"ages": None} # Invalid data
return {"ages": cleaned_ages}
def validate_cnpj(self, slot_value: Any, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> Dict[Text, Any]:
slot_value_clean = clean_text(slot_value)
if re.search(r'\b(valor|preço|plano|idoso|idade|quanto|cidade)\b', slot_value_clean, re.IGNORECASE):
dispatcher.utter_message(text="Please enter info correctly.")
return {"cnpj": None} # Invalid data
return {"cnpj": slot_value_clean}
def run(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: dict) -> List[Dict]:
# Get clean and validated slots
name = tracker.get_slot('name')
city = tracker.get_slot('city')
ages = tracker.get_slot('ages')
cnpj = tracker.get_slot('cnpj')
# Check if slots are filled
if not name or not city or not ages:
dispatcher.utter_message(text="Please enter info correctly.")
return [SlotSet("name", name), SlotSet("city", city), SlotSet("ages", ages), SlotSet("cnpj", cnpj)]
# Final formatting
ages_str = ', '.join(map(str, ages)) if ages else "Not informed"
message = (
f"Great! Let's conform everything:\n"
f"Nome: {name}\n"
f"Cidade: {city}\n"
f"Idades: {ages_str}\n"
f"Possui CNPJ: {cnpj}\n"
f"Are they correct? (Yes/No)"
)
dispatcher.utter_message(text=message)
# Add action_listen
return [SlotSet("name", name), SlotSet("city", city), SlotSet("ages", ages), SlotSet("cnpj", cnpj), FollowupAction("action_listen")]