Can i do mathematical functions in rasa

i want to create a mathematical functions in rasa, how can i do

1 Like

What kind of mathematical function? What will be your input and output. Please describe your problem in detail.

1 Like

I want to make a basic chatbot, that will ask the height and weight of the user and return the BMI of the user. This is not actual thing I want to make but this will give me idea about it. Thank you in advance.

1 Like

You need to create an action using a forms that will obtain the height and weight and then run the IMC script.

actions_imc.py

from rasa_sdk.executor import CollectingDispatcher, Action
from typing import Dict, Text, Union, Any, List
from rasa_sdk import Tracker, FormValidationAction
from rasa_sdk.events import SlotSet, UserUttered, FollowupAction
from rasa_sdk.executor import CollectingDispatcher
from rasa_sdk.types import DomainDict
from rasa_sdk.forms import FormAction

class Imc_Form(FormAction):
    """Example of a custom form action"""

    def name(self) -> Text:
        """Unique identifier of the form"""

        return "imc_form"

    @staticmethod
    def required_slots(tracker: Tracker) -> List[Text]:
        """A list of required slots that the form has to fill"""

        return ["altura", "peso"]

    def slot_mappings(self) -> Dict[Text, Union[Dict, List[Dict]]]:
        """A dictionary to map required slots to
            - an extracted entity
            - intent: value pairs
            - a whole message
            or a list of them, where a first match will be picked"""

        return {
            "altura": [self.from_entity(entity="altura"), self.from_text()],
            "peso": [self.from_entity(entity="peso"), self.from_text()]
        }
    @staticmethod
    def is_int(string: Text) -> bool:
        """Check if a string is an integer"""

        try:
            int(string)
            return True
        except ValueError:
            return False

    def submit(
        self,
        #value: Text,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any],
    ) -> List[Dict]:
        """Define what the form has to do
            after all required slots are filled"""
        sender = tracker.sender_id
        peso = tracker.get_slot('peso')
        peso = float(peso)
        altura = tracker.get_slot('altura')
        altura = float(altura)
        imc = peso / altura**2
        #
        if imc < 16:
            dispatcher.utter_message("Magreza grave")
            return[]
        elif imc < 17:
            dispatcher.utter_message("Magreza moderada")
            return[]
        elif imc < 18.5:
            dispatcher.utter_message("Magreza leve")
            return[]
        elif imc < 25:
            dispatcher.utter_message("Saudável")
            return[]
        elif imc < 30:
            dispatcher.utter_message("Sobrepeso")
            return[]
        elif imc < 35:
            dispatcher.utter_message("Obesidade Grau I")
            return[]
        elif imc < 40:
            dispatcher.utter_message("Obesidade Grau II (severa)")
            return[]
        else:
            dispatcher.utter_message("Obesidade Grau III (mórbida)")
            return[]

stories.yml

version: "2.0"
- story: imc
  steps:
  - intent: imc
  - action: imc_form
  - active_loop: imc_form
  - active_loop: null
  - action: action_restart

domain.yml

version: "2.0"

intents:
  - imc
entities:
- imc
slots:
  altura:
    type: unfeaturized
    auto_fill: false
    influence_conversation: false
  peso:
    type: unfeaturized
    auto_fill: false
    influence_conversation: false
responses:
  utter_ask_altura:
    - text: "Qual sua altura? Ex: 1.74"
  utter_ask_peso:
    - text: "Qual o seu peso em Kg. Ex: 73"

forms:
- imc_form

nlu.yml

version: "2.0"
nlu:
- intent: imc
  examples: |
    - Meu imc
    - Estou no peso ideal
    - Meu peso está ok
    - Como está meu peso
    - Estou acima do peso

Fonte: https://guiatech.net/python-algoritmo-para-calculo-do-imc/

1 Like