Classifying entities with similar values - Getting Started with Rasa / Tutorials, Resources & Videos - Rasa Community Forum

👋 Introducing the Rasa Playground

Classifying entities with similar values

post by Jivitesh on Jan 1, 2021

Hi im working in a project where i need to fill in a slot with height and weigh information (multi intent) thus i have an intent defined height_information+weight_information with a few training examples.

The issue is that since height and weight values are so similar eg, 58 kgs and 170 cm that even though the intent is being classified correctly both the values are classified as a single entity weight, any way to solve this issue as i want 3 digit answers to be classified as height (in cm) and 2 digit answers to be classified as weight (in kg)

anyone with any ideas how to fix this would be really helpful

thank you

post by marcos.allysson on Jan 2, 2021

@Jivitesh, you can use Regex to validate kilos (2 digits) and weight (3 digits). Or, if you’re using form, you can even validate each slot on your actions.py file just by verifying if kilos has 2 digits and weight, 3 for example.

post by Jivitesh on Jan 2, 2021

Hi, the issue is if I use regex then I’d have to stop the DIETclassifier from classifying intents right? currently DIET is really working well for me. I am using forms to fill the slots, but I don't have much experience with action.py if you could just show me how the code for the check for 2 and 3 digits will go I will be able to understand how to implement it much better

thank you for your response

post by marcos.allysson on Jan 2, 2021

@Jivitesh, you do not have to remove DIET, you can just use regex like I said or validate slot using your actions.py. That’d look like this:

On your actions.py:

class ValidateKiloWeightForm(FormValidationAction):
    def name(self) -> Text:
        return "validate_kilo_weight_form"

async def validate_kilos(self,
        value: Text,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any]
    ) -> Dict[Text, Any]:

# validate if kilo has 2 digits
        if value.isdigit() and len(value) == 2:
            return {"kilos": value}
        else:
            dispatcher.utter_message("Kilos must contain only 2 digits")
            return {"kilos": None}

async def validate_weight(self,
        value: Text,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any]
    ) -> Dict[Text, Any]:

# validate if weight has 3 digits
        if value.isdigit() and len(value) == 3:
            return {"weight": value}
        else:
            dispatcher.utter_message("weight must contain only 3 digits")
            return {"weight": None}

Then, declare form’s name on your domain, section actions.

actions:
- validate_kilo_weight_form

Retrain and test your assistant. This way, when the user says something different than 2 and 3 digits, your assistant will ask again until the user says correctly.

post by Jivitesh on Jan 2, 2021

@Marcos.allysson thank you for your help! I’ve learned so much from you, I used regex as I already had a form running and adding regex was just easier.

post by marcos.allysson on Jan 2, 2021

@Jivitesh, to make API calls using the collected slot values, create a function to make the call:

def api_call(kilos, weight):
    request_url = "https://link.com/api/"

info = {
        "kilos": kilos,
        "weight": weight
    }

try:
        response = requests.post(
            request_url, data=info
        )
        response.raise_for_status()
    except requests.exceptions.HTTPError as err:
        raise SystemExit(err)

return response
    print(response.status_code)

When your slots are filled, connect it with your function calls in actions.py as well.

post by Jivitesh on Jan 3, 2021

Hi marcos, got a question about the requests.post function while making my API call. I’m attempting to connect to the EDAMAM recipe search API, which takes parameters in the query. Can you help me figure out how to pass these values correctly?

post by marcos.allysson on Jan 3, 2021

@Jivitesh, to fill a certain slot, use a GET request instead. Hence, modify your URL appropriately and pass the values:

request_url = f"https://api.edamam.com/search?q={query}&app_id={YOUR_APP_ID}&app_key={YOUR_APP_KEY}&from=0&to=3"

post by Jivitesh on Jan 7, 2021

I wanted to set a slot value just from user text and not from an entity or intent. Is this possible?

post by marcos.allysson on Jan 7, 2021

@Jivitesh, yes! You can set it in your form mapping:

forms:
  your_form_name:
    your_slot_name:
    - type: from_text

Check the docs about Forms.

post by Jivitesh on Jan 7, 2021

Thank you Marcos, it works!