Back to Blog

Conversational Calculator: How to use Math in your Chatbot

Why to use math in a chatbot

If you really want to go beyond simple chat bot functionalities like understanding simple questions, you might want to use math in your chatbot.

When your AI bot understands math, various new possibilities arise, depending on your use case. Let me give you some examples:

For chatbots in the hotels & hospitality industry, you could have a standard rate per night. Then you could ask the user for how many nights they would like to stay and multiply that with the rate per night to inform them about the total costs.

Restaurant chatbots could use math for determining the group size of a booking. If somebody says something like “I’d like to book 2 tables for 4 persons each”, the bot can understand that it will be about 8 persons in total.

You could even use math for personal trainer fitness bots:

Personal Trainer Chatbot

Different types of math

In this article, we will teach you how to implement 4 types of math in your chatbot:

Different Types of Math Questions

The first step is to create an intent that detects when somebody wants the chatbot to calculate something. Someone could ask a generic question, like “Do you understand math?” or “Can you calculate something for me?”.

If that’s the case, you could simply let the bot ask for what it should calculate.

Sometimes the user already gives some more specific information about the math question, like “What is 8 divided by 4?” or “8/4=?”.

In that case, you can make your calculator chatbot already save that information, so it won’t have to ask again. This is better for the user experience since it reduces the amount of needed effort from the user.

Creating a Math Intent

Now we discussed some ways people can let the bot know they want it to solve their math problem; it’s time to create an intent for it:

Math Intent

You can see the intent is using 3 entities:

The first and second numbers in the intent are separately captured because for dividing and subtracting, you’d need to know which number should be divided/subtracted from the other number. Both number-entities use the default system entity for numbers.

The Math Entity

The third entity (math) is a custom entity. It contains the 4 types of math we already discussed briefly, along with some synonyms for them:

Math Entity

Requiring all Entities

Now we have the entity and the intent in place, it’s about time to look at the actual flow.

All 3 entities will need to have a value to enable the bot to solve the math question, so the first step is to set all of them to be required:

Requiring All Parameters

This will add sections underneath the intent for when one of the entities isn’t known after matching the intent. Click on all of them and add a Text Reply that instructs to give it a value.

Missing Required Parameters

Mathematical Conditions

Since we have 4 types of math, we need to add 4 conditions that check which type the chatbot should use.

The image below shows how to check with a condition if the math type is “Minus”. Do the same for “Multiply”, “Divide,” and “Plus” in the other conditions.

Chatbot condition with Math

Also, add a 5th Else condition without any rules, so we can give an error message when the bot wasn’t able to match any other condition due to some unforeseen problem we missed.

String Templates

Now it’s finally time to actually implement the math itself. This is easier than you might think! We will simply use String Templates.

To calculate the value of the entity number_first minus the value of number_second, use the following String Template:

{{number_first.first.match | minus: number_second.first.match}}

If you change “minus” to the other math types, you get the following flow:

Chatbot Math Flow

Resetting parameters

When we now test our flow twice, you can see the second time it doesn’t work correctly yet. The parameter number_first still contains the value from our first test.

Testing the math

That’s because we haven’t reset the parameters before giving them new values. Simply solve this by resetting the values after each response, as shown in the image below.

The final result of the flow

As you can see, the problem has now been solved!

The Math Problem Solved

And that’s how you teach math to your chatbot!

Learn More

There are way more possibilities with String Templates, so have a look at our documentation if you want to learn more.

You can also join our free Slack Support Channel. It’s a great place for getting inspired and asking questions about your chatbot project!

Request demo