AIML - <主题>标签


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

句法

使用 <set> 标签定义主题

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

使用 <topic> 标签定义类别

<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.

这里机器人以“电影”为主题进行响应。

例子

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

主题.aml

<?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.