How to insert calculator inside chat bot that written by Python

Viewed 491

I have write a chat bot program by using Python 3.6. I study the syntax from several website and the most of my chat bot content is come from here: https://medium.com/analytics-vidhya/building-a-simple-chatbot-in-python-using-nltk-7c8c8215ac6e. This program have no problem for perform nltk on text. I plan to input physics knowledge to my chat bot. So far, the bot can answer all the things relate to physics such as Newton second law and even give out the formula. However, I plan to make this bot can perform simple calculation on equation such as F=m*a. I search so many website in google in these few days, yet still cannot find the solutions. I also got search in stack overflow, and yet the nearest post to my answer seems not an answer for me. (how do I add a calculator to my chatbot). If it is for normal python script, I know how to write it and I think I will write in this way:

  mass = input("input the mass of an object")
  acceleration=input("input the acceleration of an object")
  print("Force of the object: ", mass*acceleration "Newton.")

However, how am i gonna implement this thing in chat bot? So far the only way i can think is using chat bot platform like chatterbot.AI, Dialogflow, Chatfuel all this. If I only want use python, how can I improve the chatbot, so it can perform the calculation that I have stated above. Thank you for stack overflow experts helpings and advises.

1 Answers

In a quick summary, your chatbot should:

  • Identify intent (each formula needs to be linked to a single intent)
  • Gather all the required entities (each entity represent a variable that is required for the formula).
  • Python calculation (solving the formula)
  • Retrieving the answer.

Long answer:

The first item is the most tricky. So let's separate the 2 mainstream Chatbot modes:

  1. Based on Scripts (like Chatfuel). In this mode, your chatbot needs some kind of menu (buttons / carroussel / quick answers), offering the user the list of the predefined formulas it can solve. After the user selection (F=ma), your script must ask for each variable (mass, acceleration).

  2. Based on Natural Language Processing (such as NLTK, RASA, or Dialogflow). I assume this is the way you wanna go. It is more user friendly and interactive, but also more complex. You must first set an intent for each formula, than teach your chatbot with some queries examples for each intent, like:

    • I want to solve F=ma
    • Calculate Newtons Second Law
    • What force is required to accelerate my 500kg car at 10m/s² ?

In the last example, the user already inputs all entities needed to solve the equation. If you want to allow that use case, you need to setup an entity extractor. Anyway, when the user doesn't provide all info needed, you need to ask them for the remaining entities.

You also might need to extract the units of measurement (m/s, km/h) and convert it internally.

After that, you will have the intent (formula / function) and all the entities (input / variables) necessary to compute, and prepare the answer and deliver to the user.

Related