In aiml i am trying to make my bot recognize of middle letter of any word. for exemple if i ask "What is the middle letter of cat" then it should response "A" this is what code looks like
<category><pattern>MIDDLELETTER *</pattern>
<template>The middle letter of <star/> is: <srai>MIDDLEITEM <explode><star/></explode></srai></template>
</category>
<category><pattern>MIDDLEITEM * * *</pattern>
<template><srai>MIDDLEITEM <star index="2"/></srai></template>
</category>
<category><pattern>MIDDLEITEM *</pattern>
<template><star/></template>
</category>
The above code works fine in this scenario
HUMAN: MIDDLELETTER CAT
ROBOT: The middle letter of CAT is: A
HUMAN: MIDDLELETTER SKY
ROBOT: The middle letter of SKY is: K
But does not work properly in this case
HUMAN: MIDDLELETTER ROMAN
ROBOT: The middle letter of ROMAN is: O
HUMAN: MIDDLELETTER DCNOVAN
ROBOT: The middle letter of DCNOVAN is: C
I was expecting these kind of results
HUMAN: MIDDLELETTER ROMAN
ROBOT: The middle letter of ROMAN is: M
HUMAN: MIDDLELETTER DCNOVAN
ROBOT: The middle letter of DCNOVAN is: O
Also, what if if there are 8 letters in an word? i am expecting to make it work like
HUMAN: MIDDLELETTER ABSOLUTE
ROBOT: The middle letter of ABSOLUTE could be either O or L
What am i missing in my code?
To find the middle letter, you need to break up the word into individual letters and work out how long the word is. You can then use the length of the word to find the middle letter.
This method will work. For compatibility, I've assumed you have no math capabilities in your bot ie division. It works up to letters that are 11 letters long. For longer words, you should divide the word length by 2 to find the middle letter of a word.
<category>
<pattern>MIDDLELETTER *</pattern>
<template>
<think>
<set name="letternum">0</set>
<srai>BreakUpWord <explode><star/></explode></srai>
<condition name="letternum">
<li value="1"><set name="middle"><get name="char1"/></set></li>
<li value="2"><set name="middle"><get name="char1"/> or <get name="char2"/></set></li>
<li value="3"><set name="middle"><get name="char2"/></set></li>
<li value="4"><set name="middle"><get name="char2"/> or <get name="char3"/></set></li>
<li value="5"><set name="middle"><get name="char3"/></set></li>
<li value="6"><set name="middle"><get name="char3"/> or <get name="char4"/></set></li>
<li value="7"><set name="middle"><get name="char4"/></set></li>
<li value="8"><set name="middle"><get name="char4"/> or <get name="char5"/></set></li>
<li value="9"><set name="middle"><get name="char5"/></set></li>
<li value="10"><set name="middle"><get name="char5"/> or <get name="char6"/></set></li>
<li value="11"><set name="middle"><get name="char6"/></set></li>
</condition>
</think>
The middle letter of <star/> is <get name="middle"/>.
</template>
</category>
<category>
<pattern>BREAKUPWORD * *</pattern>
<template>
<srai>IncreaseLetternum <star/></srai>
<srai>BREAKUPWORD <star index="2"/></srai>
</template>
</category>
<category>
<pattern>BREAKUPWORD *</pattern>
<template>
<srai>IncreaseLetternum <star/></srai>
</template>
</category>
<category>
<pattern>INCREASELETTERNUM *</pattern>
<template>
<think>
<condition name="letternum">
<li value="0"><set name="letternum">1</set><set name="char1"><star/></set></li>
<li value="1"><set name="letternum">2</set><set name="char2"><star/></set></li>
<li value="2"><set name="letternum">3</set><set name="char3"><star/></set></li>
<li value="3"><set name="letternum">4</set><set name="char4"><star/></set></li>
<li value="4"><set name="letternum">5</set><set name="char5"><star/></set></li>
<li value="5"><set name="letternum">6</set><set name="char6"><star/></set></li>
<li value="6"><set name="letternum">7</set><set name="char7"><star/></set></li>
<li value="7"><set name="letternum">8</set><set name="char8"><star/></set></li>
<li value="8"><set name="letternum">9</set><set name="char9"><star/></set></li>
<li value="9"><set name="letternum">10</set><set name="char10"><star/></set></li>
<li value="10"><set name="letternum">11</set><set name="char11"><star/></set></li>
</condition>
</think>
</template>
</category>