AIML - <srai> 标签



<srai> 标签是一个多用途标签。此标签使 AIML 能够为同一个模板定义不同的目标。

语法

<srai> pattern </srai> 

以下是与srai相关的常用术语:

  • 符号归约

  • 分治法

  • 同义词消解

  • 关键词检测

符号归约

符号归约技术用于简化模式。它有助于将复杂的语法模式简化为简单的模式。

例如,考虑以下对话。

Human: Who was Albert Einstein?
Robot: Albert Einstein was a German physicist.
Human: Who was Isaac Newton?
Robot: Isaac Newton was a English physicist and mathematician.

现在提出如果…怎么办的问题为

Human: DO YOU KNOW WHO Albert Einstein IS?
Human: DO YOU KNOW WHO Isaac Newton IS?

这里,<srai> 标签起作用。它可以将用户的模式作为模板。

步骤 1:创建类别

<category>
   <pattern>WHO IS ALBERT EINSTEIN?</pattern>
   <template>Albert Einstein was a German physicist.</template>
</category>

<category>
   <pattern> WHO IS Isaac NEWTON? </pattern>
   <template>Isaac Newton was a English physicist and mathematician.</template>
</category>

步骤 2:使用<srai>标签创建通用类别

<category>
   <pattern>DO YOU KNOW WHO * IS?</pattern>
   
   <template>
      <srai>WHO IS <star/></srai>
   </template>
   
</category>

示例

C > ab > bots > test > aiml目录中创建srai.aiml文件,在C > ab > bots > test > aimlif目录中创建srai.aiml.csv文件。

srai.aiml

<?xml version = "1.0" encoding = "UTF-8"?>
<aiml version = "1.0.1" encoding = "UTF-8"?>
   <category>
      <pattern> WHO IS ALBERT EINSTEIN </pattern>
      <template>Albert Einstein was a German physicist.</template>
   </category>
   
   <category>
      <pattern> WHO IS Isaac NEWTON </pattern>
      <template>Isaac Newton was a English physicist and mathematician.</template>
   </category>
   
   <category>
      <pattern>DO YOU KNOW WHO * IS</pattern>
      <template>
         <srai>WHO IS <star/></srai>
      </template>
   </category>
</aiml>

star.aiml.csv

0,WHO IS ALBERT EINSTEIN,*,*,Albert Einstein was a German physicist.,srai.aiml
0,WHO IS Isaac NEWTON,*,*,Isaac Newton was a English physicist and mathematician.,srai.aiml
0,DO YOU KNOW WHO * IS,*,*,<srai>WHO IS <star/></srai>,srai.aiml

执行程序

打开命令提示符。进入C > ab >目录,并输入以下命令:

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

验证结果

您将看到以下输出:

Human: Do you know who Albert Einstein is
Robot: Albert Einstein was a German physicist.

分治法

分治法用于在构建完整回复时重用子句。它有助于减少定义多个类别的需要。

例如,考虑以下对话。

Human: Bye
Robot: GoodBye!
Human: Bye Alice!
Robot: GoodBye!

现在,只要用户在句首说再见,机器人就应该回复再见!

让我们在这里使用<srai>标签。

步骤 1:创建类别

<category>
   <pattern>BYE</pattern>
   <template>Good Bye!</template>
</category>

步骤 2:使用<srai>标签创建通用类别

<category>
   <pattern>BYE *</pattern>
   
   <template>
      <srai>BYE</srai>
   </template>
   
</category>

示例

更新C > ab > bots > test > aiml目录中的srai.aiml文件和C > ab > bots > test > aimlif目录中的srai.aiml.csv文件。

srai.aiml

<?xml version = "1.0" encoding = "UTF-8"?>
<aiml version = "1.0.1" encoding = "UTF-8"?>
   <category>
      <pattern> WHO IS ALBERT EINSTEIN </pattern>
      <template>Albert Einstein was a German physicist.</template>
   </category>
 
   <category>
      <pattern> WHO IS Isaac NEWTON </pattern>
      <template>Isaac Newton was a English physicist and mathematician.</template>
   </category>
   
   <category>
      <pattern>DO YOU KNOW WHO * IS</pattern>
      <template>
         <srai>WHO IS <star/></srai>
      </template>
   </category>
   
   <category>
      <pattern>BYE</pattern>
      <template>Good Bye!</template>
   </category>
   
   <category>
      <pattern>BYE *</pattern>
      <template>
         <srai>BYE</srai>
      </template>
   </category>
   
</aiml>

star.aiml.csv

0,WHO IS ALBERT EINSTEIN,*,*,Albert Einstein was a German physicist.,srai.aiml
0,WHO IS Isaac NEWTON,*,*,Isaac Newton was a English physicist and mathematician.,srai.aiml
0,DO YOU KNOW WHO * IS,*,*,<srai>WHO IS <star/></srai>,srai.aiml
0,BYE,*,*,Good Bye!,srai.aiml
0,BYE *,*,*,<srai>BYE</srai>,srai.aiml

执行程序

打开命令提示符。进入C > ab >目录,并输入以下命令:

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

验证结果

您将看到以下输出:

Human: Bye
Robot: GoodBye!
Human: Bye Alice!
Robot: GoodBye!

同义词消解

同义词是意思相近的词。对于相似的词,机器人应该以相同的方式回复。

例如,考虑以下对话。

Human: Factory
Robot: Development Center!
Human: Industry
Robot: Development Center!

现在,每当用户说工厂工业时,机器人应该回复开发中心!

让我们在这里使用<srai>标签。

步骤 1:创建类别

<category>
   <pattern>FACTORY</pattern>
   <template>Development Center!</template>
</category>

步骤 2:使用<srai>标签创建通用类别

<category>
   <pattern>INDUSTRY</pattern>
   
   <template>
      <srai>FACTORY</srai>
   </template>
   
</category>

示例

更新C > ab > bots > test > aiml目录中的srai.aiml文件和C > ab > bots > test > aimlif目录中的srai.aiml.csv文件。

srai.aiml

<?xml version = "1.0" encoding = "UTF-8"?>
<aiml version = "1.0.1" encoding = "UTF-8"?>
   <category>
      <pattern> WHO IS ALBERT EINSTEIN </pattern>
      <template>Albert Einstein was a German physicist.</template>
   </category>
   
   <category>
      <pattern> WHO IS Isaac NEWTON </pattern>
      <template>Isaac Newton was a English physicist and mathematician.</template>
   </category>
   
   <category>
      <pattern>DO YOU KNOW WHO * IS</pattern>
      <template>
         <srai>WHO IS <star/></srai>
      </template>
   </category>
   
   <category>
      <pattern>BYE</pattern>
      <template>Good Bye!</template>
   </category>
   
   <category>
      <pattern>BYE *</pattern>
      <template>
         <srai>BYE</srai>
      </template>
   </category>
   
   <category>
      <pattern>FACTORY</pattern>
      <template>Development Center!</template>
   </category>
   
   <category>
      <pattern>INDUSTRY</pattern>
      <template>
         <srai>FACTORY</srai>
      </template>
   </category>
   
</aiml>

star.aiml.csv

0,WHO IS ALBERT EINSTEIN,*,*,Albert Einstein was a German physicist.,srai.aiml
0,WHO IS Isaac NEWTON,*,*,Isaac Newton was a English physicist and mathematician.,srai.aiml
0,DO YOU KNOW WHO * IS,*,*,<srai>WHO IS <star/></srai>,srai.aiml
0,BYE,*,*,Good Bye!,srai.aiml
0,BYE *,*,*,<srai>BYE</srai>,srai.aiml
0,FACTORY,*,*,Development Center!,srai.aiml
0,INDUSTRY,*,*,<srai>FACTORY</srai>,srai.aiml

执行程序

打开命令提示符。进入C > ab >目录,并输入以下命令:

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

验证结果

您将看到以下输出:

Human: Factory
Robot: Development Center!
Human: Industry
Robot: Development Center!

关键词检测

使用srai,当用户输入特定关键词(例如“学校”)时,无论“学校”在句子中的位置如何,我们都可以返回简单的回复。

例如,考虑以下对话。

Human: I love going to school daily.
Robot: School is an important institution in a child's life.
Human: I like my school.
Robot: School is an important institution in a child's life.

在这里,每当用户句子中包含学校时,机器人应该回复标准消息“学校是孩子生活中重要的机构。”

让我们在这里使用<srai>标签。我们将在这里使用通配符。

步骤 1:创建类别

<category>
   <pattern>SCHOOL</pattern>
   <template>School is an important institution in a child's life.</template>
</category>

步骤 2:使用<srai>标签创建通用类别

<category>
   <pattern>_ SCHOOL</pattern>
   <template>
      <srai>SCHOOL</srai>
   </template>
</category>

<category>
   <pattern>_ SCHOOL</pattern>
   <template>
      <srai>SCHOOL</srai>
   </template>
</category>

<category>
   <pattern>SCHOOL *</pattern>
   <template>
      <srai>SCHOOL</srai>
   </template>
</category>

<category>
   <pattern>_ SCHOOL *</pattern>
   <template>
      <srai>SCHOOL</srai>
   </template>
</category>

示例

更新C > ab > bots > test > aiml目录中的srai.aiml文件和C > ab > bots > test > aimlif目录中的srai.aiml.csv文件。

srai.aiml

<?xml version = "1.0" encoding = "UTF-8"?>
<aiml version = "1.0.1" encoding = "UTF-8"?>
   <category>
      <pattern> WHO IS ALBERT EINSTEIN </pattern>
      <template>Albert Einstein was a German physicist.</template>
   </category>
   
   <category>
      <pattern> WHO IS Isaac NEWTON </pattern>
      <template>Isaac Newton was a English physicist and mathematician.</template>
   </category>
   
   <category>
      <pattern>DO YOU KNOW WHO * IS</pattern>
      <template>
         <srai>WHO IS <star/></srai>
      </template>
   </category>
   
   <category>
      <pattern>BYE</pattern>
      <template>Good Bye!</template>
   </category>
   
   <category>
      <pattern>BYE *</pattern>
      <template>
         <srai>BYE</srai>
      </template>
   </category>
   
   <category>
      <pattern>FACTORY</pattern>
      <template>Development Center!</template>
   </category>
   
   <category>
      <pattern>INDUSTRY</pattern>
      <template>
         <srai>FACTORY</srai>
      </template>
   </category>
   
   <category>
      <pattern>SCHOOL</pattern>
      <template>School is an important institution in a child's life.</template>
   </category>  
   
   <category>
      <pattern>_ SCHOOL</pattern>
      <template>
         <srai>SCHOOL</srai>
      </template>
   </category>
   
   <category>
      <pattern>_ SCHOOL</pattern>
      <template>
         <srai>SCHOOL</srai>
      </template>
   </category>
   
   <category>
      <pattern>SCHOOL *</pattern>
      <template>
         <srai>SCHOOL</srai>
      </template>
   </category>
   
   <category>
      <pattern>_ SCHOOL *</pattern>
      <template>
         <srai>SCHOOL</srai>
      </template>
   </category>
   
</aiml>

star.aiml.csv

0,WHO IS ALBERT EINSTEIN,*,*,Albert Einstein was a German physicist.,srai.aiml
0,WHO IS Isaac NEWTON,*,*,Isaac Newton was a English physicist and mathematician.,srai.aiml
0,DO YOU KNOW WHO * IS,*,*,<srai>WHO IS <star/></srai>,srai.aiml
0,BYE,*,*,Good Bye!,srai.aiml
0,BYE *,*,*,<srai>BYE</srai>,srai.aiml
0,FACTORY,*,*,Development Center!,srai.aiml
0,INDUSTRY,*,*,<srai>FACTORY</srai>,srai.aiml
0,SCHOOL,*,*,School is an important institution in a child's life.,srai.aiml
0,_ SCHOOL,*,*,<srai>SCHOOL</srai>,srai.aiml
0,SCHOOL *,*,*,<srai>SCHOOL</srai>,srai.aiml
0,_ SCHOOL *,*,*,<srai>SCHOOL</srai>,srai.aiml

执行程序

打开命令提示符。进入C > ab >目录,并输入以下命令:

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

验证结果

您将看到以下输出:

Human: I love going to school daily.
Robot: School is an important institution in a child's life.
Human: I like my school.
Robot: School is an important institution in a child's life.
广告
© . All rights reserved.