How to auto correct or give suggestion for a wrong entity

I am using Rasa 2.x. Let say I put a query: I want to book appointment at 6:00 p.m

The bot will ask to fill slot hospital_name.: Please enter the hospital name.

The user enters: Unitd Hospital

The bot replies: Sorry, this hospital does not exists. Please enter the correct hospital name.

Now, for the last reply. I don’t want to reply this way but want something like this: Did you mean United Hospital?

User selects Yes.

I want to give this sort of suggestion to user to click and get results in case of misspelling in entity_name/slot_name. Can you help me how to implement this feature?

1 Like

Hi @Sajjadmanal ,

You could do that as a validation action (example for Rasa 2):

from rasa_sdk import FormValidationAction

# You could define the possible values in the domain, or somewhere else
# possible_hospitals = ['united hospital', 'disunited clinic']

class ValidateHospitalName(FormValidationAction):
    def name(self) -> Text:
        return "validate_company_type"

    def find_likeliest_choice(self, candidate, possible_choices, threshold=0.7):
        # Write code to get some sort of similarity score between candidate
        # each element of possible_choices, take the maximum score
        # Perhaps use a minimum threshold as well

    def validate_hospital_name(
            self,
            slot_value: Any,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: DomainDict,
        ) -> Dict[Text, Any]:
            """Validate hospital_name."""

            possible_values = domain.slots['hospital_name']['values']

            slot_value = slot_value.lower()

            if slot_value in possible_values:
                return {'hospital_name': slot_value}

            likeliest = self.find_likeliest_choice(slot_value, possible_values)
            # You could fill in the slot from likeliest, or perhaps fill in
            # but add a check, maybe by implementing required_slots...

Hope that helps

3 Likes

In fact, here’s an example for a required_slots method in a FormValidationAction in Rasa 2:

    async def required_slots(
        self,
        slots_mapped_in_domain: List[Text],
        dispatcher: "CollectingDispatcher",
        tracker: "Tracker",
        domain: "DomainDict") -> Optional[List[Text]]:

        if tracker.slots.get('feedback_00_authorisation') is False:
            return []
        else:
            return slots_mapped_in_domain

For your use case, you could:

  • create a confirm_entity boolean slot, set it to False by default, but
  • make it True when you’re using your similarity measure in validate_hospital_name
  • If confirm_entity is True, modify the slots required (in required_slots) in order to ask a confirmation question
  • Revert confirm_entity to False when you’re done validating, and carry on with the form
2 Likes

Thanks @E-dC Do you also have a project or a video tutorial where something like this has been implemented? Little difficult to understand from here. But making some sense to me.

Hi @Sajjadmanal,

Unfortunately I don’t have any more code I can share or a tutorial, it’s just that your problem is related to one I encountered at work.

Essentially, the idea is to:

  • Use validation actions to catch the close misspellings of your user and trigger some sort of flag which signals “We’re in fallback mode”
  • Use a required_slots method to alter the slots that the form must ask the user, so that you can asl for confirmation
  • Use another validation action to confirm your fallback, and reset the “We’re in fallback mode” flag

There may be simpler alternatives (maybe rules?), but I can’t think of any right now, sorry!

1 Like

Thanks @E-dC