AIML - <topic> 标签

<topic> 标签用于 AIML 中存储上下文,以便后续对话可以基于该上下文进行。通常,<topic> 标签用于 是/否类型对话。它有助于 AIML 搜索在主题上下文中编写的类别。

语法

使用 <set> 标签定义主题

<template> 
   <set name = "topic"> topic-name </set>
</template>

Define the category using <topic> tag

<topic name = "topic-name">
   <category>
      ...
   </category>     
</topic>

例如,考虑以下对话。

Human: let discuss movies
Robot: Yes movies
Human: Comedy movies are nice to watch
Robot: Watching good movie refreshes our minds.
Human: I like watching comedy
Robot: I too like watching comedy.

此处机器人以"movie"为主题做出响应。

示例

C > ab > bots > test > aiml 目录中创建 topic.aiml,并在 C > ab > bots > test > aimlif 目录中创建 topic.aiml.csv。

topic.aiml

<?xml version = "1.0" encoding = "UTF-8"?>
<aiml version = "1.0.1" encoding = "UTF-8"?>
   <category>
      <pattern>LET DISCUSS MOVIES</pattern>
      <template>Yes <set name = "topic">movies</set></template>  
   </category>
   
   <topic name = "movies">
      <category>
         <pattern> * </pattern>
         <template>Watching good movie refreshes our minds.</template>
      </category>
      
      <category>
         <pattern> I LIKE WATCHING COMEDY! </pattern>
         <template>I like comedy movies too.</template>
      </category>
      
   </topic>
</aiml>

that.aiml.csv

0,LET DISCUSS MOVIES,*,*,Yes <set name = "topic">movies</set>,topic.aiml
0,*,*,movies,Watching good movie refreshes our minds.,topic.aiml
0,I LIKE WATCHING COMEDY!,*,movies,I like comedy movies too.,topic.aiml

执行程序

打开命令提示符。转到 C > ab > 并输入以下命令 −

java -cp lib/Ab.jar Main bot = test action = chat trace = false

验证结果

您将看到以下输出 −

Human: let discuss movies
Robot: Yes movies
Human: Comedy movies are nice to watch
Robot: Watching good movie refreshes our minds.
Human: I like watching comedy
Robot: I too like watching comedy.