AI Zone Admin Forum Add your forum

NEWS: Chatbots.org survey on 3000 US and UK consumers shows it is time for chatbot integration in customer service!read more..

Working with maths in AIML - For Andres
 
 

This is for Andres as an example of how to work with maths in AIML. I have demonstrated addition as an example.

First the category which will be sraied to is set up. This will also accept things like “What is x added to y”, “What is the total of x plus y”. or even things like “If I have x apples and Jim gives me y more, how many do I have?” SETUPNUM just explodes the initial numbers, so instead of 3261, we get 3 2 6 1. This makes it easier to work out the maths. This category also handles non standard numbers like “half a dozen”, “a score”, “my age” and so on. As mentioned on a previous thread, this will only handle numbers up to 9999, as I make a chatbot not a calculator but you could make loops to handle numbers of any length.

<category><pattern>WHAT IS PLUS *</pattern>
<
template>
<
think>
<
set name="num1"><star/></set>
<
set name="num2"><star index="2"/></set>
 <
condition name="silentmode"
  <
li value="YES"><set name="message"></set><set name="silentmode"></set></li>
 </
condition>
<
srai>SETUPNUM <star/></srai>
<
set name="th1"><get name="thousands"/></set>
<
set name="hu1"><get name="hundreds"/></set>
<
set name="te1"><get name="tens"/></set>
<
set name="un1"><get name="units"/></set>
<
srai>SETUPNUM <star index="2"/></srai>
<
set name="th2"><get name="thousands"/></set>
<
set name="hu2"><get name="hundreds"/></set>
<
set name="te2"><get name="tens"/></set>
<
set name="un2"><get name="units"/></set>
 <
condition name="num1"
  <
li value="x"><set name="num1">X</set></li>
  <
li value="your iq"><set name="num1">250</set></li>
  <
li value="WHY"><set name="num1">Y</set></li>
 </
condition>
 <
condition name="num2"
  <
li value="x"><set name="num2">X</set></li>
  <
li value="your iq"><set name="num2">250</set></li>
  <
li value="WHY"><set name="num2">Y</set></li>
 </
condition>
<
set name="message"><get name="num1"/> plus <get name="num2"/> is</set>
</
think>
 <
condition name="th1"
  <
li value="UNKNOWN">Sorry but I can only work with whole numbers from 0 to 9,999.</li>
  <
li>
   <
condition name="th2"
    <
li value="UNKNOWN">Sorry but I can only work with whole numbers from 0 to 9,999.</li>
    <
li><srai>XADDITION <star/> PLUS <star index="2"/></srai></li>
   </
condition>
  </
li>
 </
condition>
</
template>
</
category

Now we have initialised the numbers and put values into the thousands, hundreds, tens and units for each number, we can work out the maths.

 

 
  [ # 1 ]

This is done by adding the units, tens, hundreds, and thousands to each other like a human would do it.

<category><pattern>XADDITION PLUS *</pattern>
<
template>
<
think>
 <
condition name="un2"
  <
li value="1"><srai>XINCRUNIT</srai></li>
  <
li value="2"><srai>XINCRUNIT</srai><srai>XINCRUNIT</srai></li>
  <
li value="3"><srai>XINCRUNIT</srai><srai>XINCRUNIT</srai><srai>XINCRUNIT</srai></li>
  <
li value="4"><srai>XINCRUNIT</srai><srai>XINCRUNIT</srai><srai>XINCRUNIT</srai><srai>XINCRUNIT</srai></li>
  <
li value="5"><srai>XINCRUNIT</srai><srai>XINCRUNIT</srai><srai>XINCRUNIT</srai><srai>XINCRUNIT</srai><srai>XINCRUNIT</srai></li>
  <
li value="6"><srai>XINCRUNIT</srai><srai>XINCRUNIT</srai><srai>XINCRUNIT</srai><srai>XINCRUNIT</srai><srai>XINCRUNIT</srai><srai>XINCRUNIT</srai></li>
  <
li value="7"><srai>XINCRUNIT</srai><srai>XINCRUNIT</srai><srai>XINCRUNIT</srai><srai>XINCRUNIT</srai><srai>XINCRUNIT</srai><srai>XINCRUNIT</srai><srai>XINCRUNIT</srai></li>
  <
li value="8"><srai>XINCRUNIT</srai><srai>XINCRUNIT</srai><srai>XINCRUNIT</srai><srai>XINCRUNIT</srai><srai>XINCRUNIT</srai><srai>XINCRUNIT</srai><srai>XINCRUNIT</srai><srai>XINCRUNIT</srai></li>
  <
li value="9"><srai>XINCRUNIT</srai><srai>XINCRUNIT</srai><srai>XINCRUNIT</srai><srai>XINCRUNIT</srai><srai>XINCRUNIT</srai><srai>XINCRUNIT</srai><srai>XINCRUNIT</srai><srai>XINCRUNIT</srai><srai>XINCRUNIT</srai></li>
 </
condition>
 <
condition name="te2"
  <
li value="1"><srai>XINCRTENS</srai></li>
  <
li value="2"><srai>XINCRTENS</srai><srai>XINCRTENS</srai></li>
  <
li value="3"><srai>XINCRTENS</srai><srai>XINCRTENS</srai><srai>XINCRTENS</srai></li>
  <
li value="4"><srai>XINCRTENS</srai><srai>XINCRTENS</srai><srai>XINCRTENS</srai><srai>XINCRTENS</srai></li>
  <
li value="5"><srai>XINCRTENS</srai><srai>XINCRTENS</srai><srai>XINCRTENS</srai><srai>XINCRTENS</srai><srai>XINCRTENS</srai></li>
  <
li value="6"><srai>XINCRTENS</srai><srai>XINCRTENS</srai><srai>XINCRTENS</srai><srai>XINCRTENS</srai><srai>XINCRTENS</srai><srai>XINCRTENS</srai></li>
  <
li value="7"><srai>XINCRTENS</srai><srai>XINCRTENS</srai><srai>XINCRTENS</srai><srai>XINCRTENS</srai><srai>XINCRTENS</srai><srai>XINCRTENS</srai><srai>XINCRTENS</srai></li>
  <
li value="8"><srai>XINCRTENS</srai><srai>XINCRTENS</srai><srai>XINCRTENS</srai><srai>XINCRTENS</srai><srai>XINCRTENS</srai><srai>XINCRTENS</srai><srai>XINCRTENS</srai><srai>XINCRTENS</srai></li>
  <
li value="9"><srai>XINCRTENS</srai><srai>XINCRTENS</srai><srai>XINCRTENS</srai><srai>XINCRTENS</srai><srai>XINCRTENS</srai><srai>XINCRTENS</srai><srai>XINCRTENS</srai><srai>XINCRTENS</srai><srai>XINCRTENS</srai></li>
 </
condition>
 <
condition name="hu2"
  <
li value="1"><srai>XINCRHUND</srai></li>
  <
li value="2"><srai>XINCRHUND</srai><srai>XINCRHUND</srai></li>
  <
li value="3"><srai>XINCRHUND</srai><srai>XINCRHUND</srai><srai>XINCRHUND</srai></li>
  <
li value="4"><srai>XINCRHUND</srai><srai>XINCRHUND</srai><srai>XINCRHUND</srai><srai>XINCRHUND</srai></li>
  <
li value="5"><srai>XINCRHUND</srai><srai>XINCRHUND</srai><srai>XINCRHUND</srai><srai>XINCRHUND</srai><srai>XINCRHUND</srai></li>
  <
li value="6"><srai>XINCRHUND</srai><srai>XINCRHUND</srai><srai>XINCRHUND</srai><srai>XINCRHUND</srai><srai>XINCRHUND</srai><srai>XINCRHUND</srai></li>
  <
li value="7"><srai>XINCRHUND</srai><srai>XINCRHUND</srai><srai>XINCRHUND</srai><srai>XINCRHUND</srai><srai>XINCRHUND</srai><srai>XINCRHUND</srai><srai>XINCRHUND</srai></li>
  <
li value="8"><srai>XINCRHUND</srai><srai>XINCRHUND</srai><srai>XINCRHUND</srai><srai>XINCRHUND</srai><srai>XINCRHUND</srai><srai>XINCRHUND</srai><srai>XINCRHUND</srai><srai>XINCRHUND</srai></li>
  <
li value="9"><srai>XINCRHUND</srai><srai>XINCRHUND</srai><srai>XINCRHUND</srai><srai>XINCRHUND</srai><srai>XINCRHUND</srai><srai>XINCRHUND</srai><srai>XINCRHUND</srai><srai>XINCRHUND</srai><srai>XINCRHUND</srai></li>
 </
condition>
 <
condition name="th2"
  <
li value="1"><srai>XINCRTHOU</srai></li>
  <
li value="2"><srai>XINCRTHOU</srai><srai>XINCRTHOU</srai></li>
  <
li value="3"><srai>XINCRTHOU</srai><srai>XINCRTHOU</srai><srai>XINCRTHOU</srai></li>
  <
li value="4"><srai>XINCRTHOU</srai><srai>XINCRTHOU</srai><srai>XINCRTHOU</srai><srai>XINCRTHOU</srai></li>
  <
li value="5"><srai>XINCRTHOU</srai><srai>XINCRTHOU</srai><srai>XINCRTHOU</srai><srai>XINCRTHOU</srai><srai>XINCRTHOU</srai></li>
  <
li value="6"><srai>XINCRTHOU</srai><srai>XINCRTHOU</srai><srai>XINCRTHOU</srai><srai>XINCRTHOU</srai><srai>XINCRTHOU</srai><srai>XINCRTHOU</srai></li>
  <
li value="7"><srai>XINCRTHOU</srai><srai>XINCRTHOU</srai><srai>XINCRTHOU</srai><srai>XINCRTHOU</srai><srai>XINCRTHOU</srai><srai>XINCRTHOU</srai><srai>XINCRTHOU</srai></li>
  <
li value="8"><srai>XINCRTHOU</srai><srai>XINCRTHOU</srai><srai>XINCRTHOU</srai><srai>XINCRTHOU</srai><srai>XINCRTHOU</srai><srai>XINCRTHOU</srai><srai>XINCRTHOU</srai><srai>XINCRTHOU</srai></li>
  <
li value="9"><srai>XINCRTHOU</srai><srai>XINCRTHOU</srai><srai>XINCRTHOU</srai><srai>XINCRTHOU</srai><srai>XINCRTHOU</srai><srai>XINCRTHOU</srai><srai>XINCRTHOU</srai><srai>XINCRTHOU</srai><srai>XINCRTHOU</srai></li>
 </
condition>
</
think>
 <
condition name="th1"
  <
li value="ERROR">Sorry but the total is more than 9,999 and so I can't work it out.</li>
  <li><think><set name="mathanswer"><get name="th1"/> <get name="hu1"/> <get name="te1"/> <get name="un1"/></set></think><get name="message"/> <srai>NUMBERTOWORDS <get name="mathanswer"/></srai></li>
 </condition>
</template>
</category> 
 

 
  [ # 2 ]

The below categories are reused by all the maths functions to increase numbers:

<category><pattern>XINCRUNIT</pattern>
<
template>
 <
condition name="un1"
  <
li value="0"><set name="un1">1</set></li>
  <
li value="1"><set name="un1">2</set></li>
  <
li value="2"><set name="un1">3</set></li>
  <
li value="3"><set name="un1">4</set></li>
  <
li value="4"><set name="un1">5</set></li>
  <
li value="5"><set name="un1">6</set></li>
  <
li value="6"><set name="un1">7</set></li>
  <
li value="7"><set name="un1">8</set></li>
  <
li value="8"><set name="un1">9</set></li>
  <
li value="9"><set name="un1">0</set><srai>XINCRTENS</srai></li>
 </
condition>
</
template>
</
category>

<
category><pattern>XINCRTENS</pattern>
<
template>
 <
condition name="te1"
  <
li value="0"><set name="te1">1</set></li>
  <
li value="1"><set name="te1">2</set></li>
  <
li value="2"><set name="te1">3</set></li>
  <
li value="3"><set name="te1">4</set></li>
  <
li value="4"><set name="te1">5</set></li>
  <
li value="5"><set name="te1">6</set></li>
  <
li value="6"><set name="te1">7</set></li>
  <
li value="7"><set name="te1">8</set></li>
  <
li value="8"><set name="te1">9</set></li>
  <
li value="9"><set name="te1">0</set><srai>XINCRHUND</srai></li>
 </
condition>
</
template>
</
category>

<
category><pattern>XINCRHUND</pattern>
<
template>
 <
condition name="hu1"
  <
li value="0"><set name="hu1">1</set></li>
  <
li value="1"><set name="hu1">2</set></li>
  <
li value="2"><set name="hu1">3</set></li>
  <
li value="3"><set name="hu1">4</set></li>
  <
li value="4"><set name="hu1">5</set></li>
  <
li value="5"><set name="hu1">6</set></li>
  <
li value="6"><set name="hu1">7</set></li>
  <
li value="7"><set name="hu1">8</set></li>
  <
li value="8"><set name="hu1">9</set></li>
  <
li value="9"><set name="hu1">0</set><srai>XINCRTHOU</srai></li>
 </
condition>
</
template>
</
category>

<
category><pattern>XINCRTHOU</pattern>
<
template>
 <
condition name="th1"
  <
li value="0"><set name="th1">1</set></li>
  <
li value="1"><set name="th1">2</set></li>
  <
li value="2"><set name="th1">3</set></li>
  <
li value="3"><set name="th1">4</set></li>
  <
li value="4"><set name="th1">5</set></li>
  <
li value="5"><set name="th1">6</set></li>
  <
li value="6"><set name="th1">7</set></li>
  <
li value="7"><set name="th1">8</set></li>
  <
li value="8"><set name="th1">9</set></li>
  <
li value="9"><set name="th1">ERROR</set></li>
 </
condition>
</
template>
</
category

Using this method, we can create categories for subtraction and multiplication. I found division quite difficult to handle and so work on how many times I could subtract a number from the original. For example:

What is 12/4?
12-4=8 (count of 1)
8-4=4 (count of 2)
4-4=0 (count of 3)
The final count is 3 and so 12/4 must be 3.

Once we have these steps, we can combine them to answer questions such as “What is eighty plus 9 - 1?” like so. Here is an exmaple of such a category:

<category><pattern>XMULTISUM _ PLUS MINUS *</pattern>
<
template>
<
think>
<
srai>what is <star/> plus <star index="2"/></srai>
<
set name="totalsofar"><srai>numbertowords <get name="mathanswer"/></srai></set>
</
think>
<
srai>XMULTISUM <get name="totalsofar"/> MINUS <star index="3"/></srai>
</
template>
</
category

Put it all together and you get something like:

Human: What is eighty plus 9 - 1
Mitsuku:  I reckon that lot comes to eighty eight.

The question of reuse came up. In AIML, once you’ve created a category, you can just pass parameters to it. So now we can answer, “what is x plus y minus z” for any number.

If we want to stick to BODMAS rules, we would need to work on each part of the input by priority. For example in this category, multiplication takes priority over addition:

<category><pattern>XMULTISUM _ PLUS TIMES *</pattern>
<
template>
<
think>
<
srai>what is <star index="2"/> times <star index="3"/></srai>
<
set name="totalsofar"><srai>numbertowords <get name="mathanswer"/></srai></set>
</
think>
<
srai>XMULTISUM <get name="totalsofar"/> plus <star index="1"/></srai>
</
template>
</
category

Hope that clears things up for Andres but I am happy to answer any further questions you may have.

 

 
  [ # 3 ]

Wow, I barely get any of that. But it is pretty freaking brilliant how you solved division. Of course I could just think that because math is like a bad ex I try to forget….words are my playthings…math is ugh. Regardless, colour me impressed.

 

 
  [ # 4 ]

Steve, you are a genius! with Turtlegraphics, sorry AIML, but believe me math-power, functions, nested functions, with parenthesis, etc, nesting, it’s barely impossible. and this is brilliantly solved, but odd! no regrets! smile

 

 
  [ # 5 ]

It’s certainly not impossible. I just showed you how to do it above.

Naturally, it appears complicated at first glance as we are teaching the concept of maths and numbers to a program that has no knowledge of either subject. Let’s assume an alien asked you to perform the random function of “floogle” using “boogles” and “beegles”, surely you would need to know what these concepts represent?

Sorry, it’s not to your taste but to me the simplicity of AIML is its strength. As, I say it is the only programming language that has won the Loebner Prize by different people. It allowed me to win the Funniest Ever Computer competition both times it has run: http://funniestcomputer.neurogrid.com/home as well as various other contests. This involved creating original jokes and poetry on any subject. It has also allowed over 225,000 people to create over 285,000 chatbots with over 3 billion interactions just on the Pandorabots website alone.

Even this weekend, Mitsuku (and Rose) was featured in a real world German newspaper called Die Zeit, with a readership of some 2 million people: http://www.square-bear.co.uk/mitsuku/Die_Zeit_2016_03_00055.pdf

To class it as a toy because you tried it once and couldn’t get it to work is a little disrespectful to be honest.

 

 
  [ # 6 ]

I have to agree. At first glance, it was all a blur. Which was not unexpected, considering I had no knowledge AIML even existed a few weeks ago and my “challenges” with math. (Don’t take that to mean I am some kind of dummy who can’t do math. I can, I just blank out under pressure, and any kind of math beyond simple stuff and statistics makes my brain pressure max out LOL.)

But since there is no exam coming up I have been able to relax a little and take a good look at this. I have probably come back and looked it over at least 10 times since you posted (each time after puzzling out some more things in AIML) and last night I surprised myself when I actually understood a good half of what I was looking at.

Of course not everyone has a “math block” like me, but I imagine understanding doesn’t happen overnight for anyone who isn’t familiar with AIML. I doubt Steve sat down on his first day working with AIML and wrote this code. It takes commitment and time.

I think that minimizing, mocking and disregarding AIML is an ignorant behaviour. It is the equivalent of saying people should stop speaking a language because some a non-native speaker prefers their own. Should we stop speaking English because its too hard to learn? Should we tell people who speak Esperanto that they need to stop that childishness?

There are many options for language and there are many options for AI programming. Each has its place, and each has its “fans” or native speakers. To each his own (or her).

And all that being said, someone seriously needs to stop making fun of turtle graphics, which I happen to remember fondly from my childhood. I am Canadian. We all carry hockey sticks. wink

 

 
  [ # 7 ]

And in case it loses something in translation, I meant that last sentence as a joke. If anyone thought it was in poor taste or inappropriate, I apologize. My sense of humor sometimes runs away with me. In my mind I am a comedian LOL. But if it did cause offense please let me know and I will curb my impulses in the future.

 

 
  login or register to react