chatbotaiml

AIML How to get out of topic from underscored wildcard


In aiml how to get out of topic which has underscored wildcard. Here is the code

<category>
    <pattern>TOPIC</pattern>
    <template>ok <think><set name="topic">ctt</set></think></template>
</category>

<topic name="ctt">
    
    <category>
        <pattern>_</pattern>
        <template>no</template>
    </category>
    
    <category>
        <pattern>CHANGE TOPIC</pattern>
        <template>YES <set name="topic"></set></template>
    </category>
    
</topic>

The output is

Human: TOPIC
Robot: ok
Human: CSA
Robot: no
Human: CHANGE TOPIC
Robot: no

How to fix it without using conditions?


Solution

  • The reason for this is that the underscore wildcard takes priority over everything else, even a direct match.

    Hopefully, you are using AIML 2 rather than AIML 1 and so you can simply change the <pattern>CHANGE TOPIC</pattern> to <pattern>$CHANGE TOPIC</pattern>.

    The dollar wildcard means if your input exactly matches the pattern, the template will be activated.

    If you are not using AIML 2, I would have to ask why, but if for whatever reason you are not, you will need a condition tag to do the same action. Swap your underscore category for this one instead. It checks the input and if it matches CHANGE TOPIC, the topic is reset.

    <category>
        <pattern>_</pattern>
        <template>
            <think><set var="userinput"><star/></set></think>
            <condition var="userinput">
                <li value="CHANGE TOPIC">YES <set name="topic"></set></li>
                <li>no</li>
            </condition>
        </template>
    </category>