How to use the accumulator: perform calculations in Beyond Virtual’s brain

use the accumulator in Beyond Virtual
use the accumulator in Beyond Virtual

Published by:

Eran Soroka

Building conversation flows in Beyond Virtual is fun. However, the textual conversation can go to another dimension when mathematical calculations enter the picture. Our virtual humans can do it for you too – and that’s why the accumulator exists in our brain. So in this tutorial, we’ll take you step by step through the best practices – how to use the accumulator.

For example, here are some possible uses of the accumulator:

  • Give your students and learners a grade suited to your needs. For example, give a different grade to a perfect answer and a lower grade to an answer that was close enough.
  • Give your users a pricing offer on the spot.

Let’s start. First, enter your virtual human’s brain.

1. Let’s say you want to accumulate a grade throughout an exam – add points for every correct answer. In this case, you have to select a “Save Value” green component, to be able to accumulate points. So, click on the canvas and choose it. Then, go to the inspector, give the variable a name – in this case, “grade” – and set the initial value – in this case, 0. 

2. In the first Say, tell the user what you will do. 

Then, add (in the same Say or a different one) the first question.

3. Now, create an intent to match the question you asked. Intents are ways to understand the user’s input, and you can select between exact matches (Keyword Intents) and ones where our AI/NLP algorithm decides if the answer is close enough (Learned Intents). To learn more about intents and how to create them, read this tutorial. In this stage, connect the Say to the Navigation with the relevant intents.

For example, the question here is about France’s capital. So the answer will be “Paris”. You can add the same answer with lowercase letters. 

Then, create another Say, to congratulate the user about answering correctly. Now, connect the Navigation’s port with the correct answer to this Say.

Now, click on the canvas, and type in “accumulator” in the search space. Here, by hovering over it, you can see its functionalities. We’ll get into it in a second.

Let the fun begin

4. Click on the accumulator to bring it into the conversational flow. Then, go to the inspector on the right, and click on “Advanced features”. Here, you need to fill three fields:

  • variableName: the name of the variable you chose at the beginning of the conversation (Step 1)
  • operation: the operation you want the virtual human or bot to perform. Here you have six options, among them the four basic operations: addition (add), subtraction (sub), multiplication (mul), and divide (div).
  • operand: what number do you want to add, subtract, multiply by, or divide by? 

In this case, we promised to add an amount of 10 points to the grade for each correct answer, so our values will be:

variableName

grade

operation

add

operand

10

5. Now, remember to notify the user if the answer is incorrect. Otherwise, if the user answers incorrectly, and the “Anything else” port is left unconnected, the conversation will end here.

6. Now, repeat the same process detailed in step 2 – and ask the user another question. Here, don’t forget to connect both options – the correct one (with the accumulator being the most recent component) and the incorrect one (the Say from step 5) to the upcoming question.

7. Here, repeat step 3 to create an intent that matches the second question (in this case – about the capital city of Spain). After that, if you want the same scoring from question 1 in question 2 as well, you can duplicate the accumulator. So to do that, right-click (or left-click, if you’re left-handed) on the accumulator component and select “duplicate”. However, if you want to perform another mathematical calculation, go back to Step 4 to see the options.

Now, create a new Say to congratulate the user on the correct answer, and connect it forward to the next accumulator. Also, create a new Say to notify the user about the incorrect answer, like we did in Step 5.

It’s the Final Countdown!

8. Now, it’s time to tell the user how many points they got – what’s the grade? So, to do that, we’ll write a Say about it, and to recall the score saved in the context, write inside it:

${context.____} 

Instead of ____, fill in the name of the variable you saved in Step 1. In this case – it’s grade, so to recall and announce the grade, write

${context.grade} 

In addition, don’t forget to connect both the previous correct answer (accumulator) and incorrect answer to the Say with the grade announcement. 

9. Now, test yourself in the project test (on the bottom right side of the screen). In this case, if you answered “Paris” and “Madrid”, you will get 20 points out of 20. However, if you answered only one question correctly, your grade will be 10, and if both answers are incorrect – you will have 0 points.

If everything goes according to the plan, you’re done with this part.

Nevertheless, what happens if you want to have further calculations? That’s what we’ll do next.

Perform another calculation with the same variable

10. Now, let’s say that you want to give the user a bonus – an additional 50% of the grade. Here, we’ll use another accumulator and call it to the conversation (and because it’s a different calculation – DON’T duplicate the previous one). However, how will we tell the user about the new grade? The variable “grade” is already taken.

Here, we’ll introduce another feature of the accumulator: the ability to save the result of the calculation under a new name. In that case, use the variable name “resultVariableName” to create and store a new variable, and under “value” fill in a new name – for example, here, “newgrade”. To sum this part, we’ll need to multiply the previous variable (“grade”) by 1.5, to add 50% to “grade”. So, this is what our new accumulator will look like in the inspector:

variableName

grade

operation

mul

operand

1.5

resultVariableName

newgrade

11. Now, connect the accumulator forward to a new Say, and announce the new grade by mentioning it, like we did in step 8 – but with the new variable. 

So here, we’ll recall the new grade by writing in the new Say

${context.newgrade}

12. Now, test the whole flow in the project test window. In our case, if the student got both answers correctly, the new grade will be 20*1.5 = 30 points.

We’re done with this step as well!

Perform a calculation between two saved variables

13. Now, let’s say that you want to give a customer a pricing offer on the spot. Here, you know the number of items the customer wants and the price of every item.

In this case, we need to save the variables under two different names. Then, we’ll do the math.

First of all, let’s ask (in a Say) how many items our customer wants to buy. Ask them to mention only the number as an answer. Then, connect it forward to a Navigation where every answer is possible since every number of items is possible. Now, connect the Navigation forward to a Save  Context component, and save the user’s answer under the variable name Number.

14. Now, let’s ask the customer what’s the price of a single item – and save it under the variable name “price”.

Alternatively – if we know the prices in advance and have a limited variety of items, we can do it otherwise. This one isn’t detailed in the video, so here’s how to do it:

(a) Ask the customer what item they want to buy

(b) Save it as an “item”

(c) List every “item” under a different intent, e.g Cucumber, Tomato, and Lettuce, and connect each of them to a “save value to context” component, where the variable name will be “price”, but the value will be different for every item.

In that case, we’ll do the math between variables “item” and “price”.

Now, let’s go back to the previous flow.

15. Here, connect the Save Context component forward to a new Say, and then to a new accumulator. (You can put the accumulator before the new Say). Now it gets tricky, so make sure to follow us.

  • The variableName will be the first saved variable – e.g, number
  • The operation will be, like in Step 10, “mul” (for multiply).
  • Here, under the operand, instead of filling in a constant number for calculation, we’ll recall the second variable. To do it, don’t write the just name of the second variable; Instead, write it in the form of ${context.____}, where the ____ is replaced by the name of the second variable, e.g price. So the value written here will be ${context.price}.
  • resultVariableName – the result of the calculation. We’ll save it under the variable “total”.

16. Now, connect the accumulator forward to the Say where you’ll announce the total price, in the form of ${context.total}

Subsequently, test your flow in the Project Test window. Here, the customer wants 14 cucumbers, each of them costs $1.5, so the total cost will be 14 x $1.5 = $21.

Make the virtual human sound more conversational with ReadNumber

17. Sometimes, people won’t just say a number when being asked about the number of items they want to purchase. So, it can complicate things when we use the accumulator. With this purpose in mind – to make the conversation more natural – we created a component called “ReadNumber”. It gets a user’s input and “squeezes” out only the number from it. In case there are several mentioned numbers, it remembers the first one. Furthermore, it also stores the number as a variable, so we don’t need to use the Save Context component.

Now, let’s see how do to it:

  • Click on the canvas to call a “ReadNumber” red component into your flow
  • Go to the Inspector on the right
  • Under “name”, write out
  • Under “value”, write the same variable that we had before (in our case, number)

Now, just replace the “Save Context” component with the ReadNumber component – with the same connections before and after it.

To conclude our tutorial, test your flow, to see that everything goes according to the plan, and… that’s it!

Feel free to use the accumulator any time you want. If you still have any questions, contact us here.  

More tutorials from Beyond Virtual: