Open In App

AIML <condition> Tag

Last Updated : 31 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In AIML, the <condition> tag is used to implement conditional logic within the chatbot responses. The chatbot can give the response based on the input of the user processing with the value attribute.

Syntax:

<condition name="variable_name">
    <li var="variable_value1">...</li>
    <li var="variable_value2">...</li>
</condition>

Attributes:

  • name: The name attribute is used to specify the name of the variable or condition that we want to evaluate within the `<condition>` tag.
  • var: The var attribute is used to define the value or range of values that we want to compare with the name attribute in the `<condition>` tag.

Example 1: Let’s consider an example where the chatbot responds based on the mood (happy sad or angry) of the user.

XML




<?xml version="1.0" encoding="UTF-8"?>
<aiml>
    <category>
        <pattern>I am *</pattern>
        <template>
            <think>
                <set name="mood"><star /></set>
            </think>
            <condition name="mood">
                <li value="happy">I am glad to hear that!</li>
                <li value="sad">I am sorry to hear that!</li>
                <li value="angry">Take a deep breath!</li>
                <li>You seems <star /> today!</li>
            </condition>
        </template>
    </category>
  
    <category>
        <pattern>*</pattern>
        <template>I'm here to help. What can I assist you with today?</template>
    </category>
</aiml>


Output: The pattern gonna match the inputs with the prefix “I am” and let the chatbot process the mood by using `<think>` and `<set>` tags. Based on the name and value inside the `<condition>` tag the corresponding condition will be evaluated and generated by the chatbot.

ezgifcom-video-to-gif-(6)

Example 2: Lets see the example where the chatbot responds with the weather condition.

XML




<?xml version="1.0" encoding="UTF-8"?>
<aiml>
    <category>
        <pattern>how is the weather today</pattern>
        <template>
            <think>
                <set name="weather">sunny</set>
            </think>
            <condition name="weather">
                <li value="sunny">It seems sunny today</li>
                <li value="rainy">It's raining</li>
                <li>The weather is unpredictable</li>
            </condition>
        </template>
    </category>
    <category>
        <pattern>*</pattern>
        <template>I'm here to help. What can I assist you with today?</template>
    </category>
</aiml>


Output: When the user asks “how is the weather today”, the chatbot processes and sets the weather value with sunny and based on the name and value attributes the output will be generated as “It seems sunny today”.

ezgifcom-video-to-gif-(7)

Steps to execute your bot:

  • Ensure that you sign-up to pandorabots for free.
  • create your bot and navigate to AIML folder in the folder structure and start writing your XML code in the udc file.

Screenshot-(1148)

  • Once you wrote the code, click on the chat option button on the right-bottom corner and start your conversation with your bot.

Screenshot-(1150)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads