【AIML系列-3】人工智能标记语言——高级标签教学

【AIML系列】人工智能标记语言——高级标签教学

AIML代表人工智能标记语言.AIML由Alicebot自由软件社区和Richard S. Wallace博士在1995-2000期间开发.
AIML用于创建或自定义Alicebot,这是一个基于A.L.I.C.E的聊天框应用程序. (人工语言互联网计算机实体)免费软件.

【AIML系列】基本内容教学
【AIML系列】初级标签教学
【AIML系列】高级标签教学
【AIML系列】实战使用教学


AIML是由标签和词汇组成的,熟练掌握基本标签便可以写出基本的AIML对话生成,而掌握高级标签便可以写出多种多样的回复并做到对应的模式匹配等,是AIML语言的重点部分

< star>

< star> 标记用于匹配< pattern>中的通配符 * 字符

  • 语法
<star index = "n"/>

n 表示< pattern>中用户输入中*的位置标记.

  • 示例
   <category>
      <pattern>I LIKE *</pattern>
      <template>
         I too like <star/>.
      </template>
   </category>

如果用户输入"I like dogs"
然后机器人会回答"I too like dogs."

   <category>
      <pattern> A * is a *. </pattern>
      <template>
         When a <star index = "1"/> is not a <star index = "2"/>?
      </template>
   </category>

如果用户输入"A dog is a kind of animal."
然后机器人会回答"When a dog is not a kind of animal?"

< srai>

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

  • 语法
<srai> </srai>
  • 符号缩减
    符号缩减技术用于简化模式.它有助于用简单的模式减少复杂的语法模式.
<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>

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

在上面的例子中,我们首先定义了两个细粒度的句子:
谁是爱因斯坦和谁是牛顿?

在使用<srai>标签中通过<star>得到上文“你知道这个人物是谁么?”中的这个人物的值,然后将其一并作为问题再提给“谁是爱因斯坦或谁是牛顿”的问题模板中

  • 分而治之
    Divide and Conquer用于在完成回复时重复使用子句.它有助于减少定义多个类别.
   <category>
      <pattern>再见</pattern>
      <template>拜拜</template>
   </category>

   <category>
      <pattern>再见 *</pattern>
      <template>
         <srai>再见</srai>
      </template>
   </category>

这样,就会将再见一类的问答均匹配到定义的再见模板中

  • 同义词解析
    同义词是具有相似含义的词.机器人应该以相同的方式回复类似的单词.
<category>
   <pattern>FACTORY</pattern>
   <template>Development Center!</template>
</category>

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

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

  • 关键字检测
    使用 srai ,我们可以在用户返回简单响应键入一个特定的关键词,比如学校,无论句子中出现"学校".
<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>

使用如上代码,您将看到

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.

< random>&< li>

< random> 标签用于获取随机响应.此标签使AIML能够针对相同的输入做出不同的响应. < random>标签与< li>一起使用标签. < li> 标签带有随机传递给用户的不同响应.

  • 语法
<random>
   <li> pattern1 </li>
   <li> pattern2 </li>
   ...
   <li> patternN </li>
</random>
  • 示例
   <category>
      <pattern>HI</pattern>
      
      <template>
         <random>
            <li> Hello! </li>
            <li> Hi! Nice to meet you! </li>
         </random>
      </template>
   <category>      

Human: Hi
Robot: Hi! Nice to meet you!
Human: Hi
Robot: Hello!

这样在输入同一个问题< pattern>就可以随机获得不同的答案回复,实现多样性的回复了

< set>&< get>

< set> 和< get> 标记用于处理AIML中的变量.变量可以是预定义变量或程序员创建的变量

  • 语法

< set> tag用于设置变量中的值.

<set name = "variable-name"> variable-value </set>

< get> tag用于从变量中获取值.

<get name = "variable-name"></get>
  • 示例
   <category>
      <pattern>I am *</pattern>
      <template>
         Hello <set name = "username"> <star/>! </set>
      </template>  
   </category>  
   
   <category>
      <pattern>Good Night</pattern>
      <template>
         Good Night <get name = "username"/>! Thanks for the conversation!
      </template>  
   </category>  

上述代码中用< set>和< get>分别做到了取和用一个特定字符,并通过同一个name来进行两者的对应

Human: I am Mahesh
Robot: Hello Mahesh!
Human: Good Night
Robot: Good Night Mahesh! Thanks for the conversation!

< topic>

< topic> 标记在AIML中用于存储上下文,以便以后的对话可以根据该上下文完成.通常,< topic> 标记用于是/否类型对话.它有助于AIML搜索在主题上下文中编写的类别.

  • 语法
    使用< set>定义主题标签
<template> 
   <set name = "topic"> topic-name </set>
</template>

使用< topic>定义类别标签

<topic name = "topic-name">
   <category>
      ...
   </category>     
</topic>
  • 示例
<?xml version = "1.0" encoding = "UTF-8"?>
<aiml version = "1.0" 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>

您将看到以下输出 :

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.

< think>

< think> 标签在AIML中用于存储变量而不通知用户.

  • 语法
    使用< think>存储值标签
<think> 
   <set name = "variable-name"> variable-value </set>
</think>
  • 示例
<?xml version = "1.0" encoding = "UTF-8"?>
<aiml version = "1.0" encoding = "UTF-8"?>

   <category>
      <pattern>My name is *</pattern>
      <template>
         Hello!<think><set name = "username"> <star/></set></think>
      </template>  
   </category>  
   
   <category>
      <pattern>Byeee</pattern>
      <template>
         Byeee <get name = "username"/>. Thanks for the conversation!
      </template>  
   </category>  

</aiml>
  • 示例
    您将看到以下输出 :
Human: My name is Mahesh
Robot: Hello!
Human: Byeee
Robot: Byeee Mahesh Thanks for the conversation!

< condition>

< condition> 标记类似于编程语言中的switch语句.它有助于ALICE响应匹配的输入.

  • 语法
<condition name = "variable-name" value = "variable-value"/>
  • 示例
    简单示例
<?xml version = "1.0" encoding = "UTF-8"?>
<aiml version = "1.0.1" encoding = "UTF-8"?>
   <category>
      <pattern> HOW ARE YOU FEELING TODAY </pattern>
      
      <template>
         <think><set name = "state"> happy</set></think>
         <condition name = "state" value = "happy">
            I am happy!
         </condition>
         
         <condition name = "state" value = "sad">
            I am sad!
         </condition>
      </template>
      
   </category>
</aiml>

Human: How are you feeling today
Robot: I am happy!

标准语料中的示例

<category>
	<pattern>GET NAME GENDER</pattern>
	<template>
	<think><set name="gender">
		<condition name="name">
			<li value="jeb">male</li>
			<li value="anders">male</li>
			<li value="andre">male</li>
			<li value="tiffany">female</li>
			<li value="tina">female</li>
			<li value="valerie">female</li>
			<li>it</li>
		</condition>
	</set></think>
	<srai>setheshe</srai>
	</template>
</category>

<category>
	<pattern>SETHESHE</pattern>
	<template>
		<condition name="gender">
			<li value="male"><think><set name="heshe">he</set></think></li>
			<li value="female"><think><set name="heshe">she</set></think></li>
			<li>
				<think><set name="heshe">it</set></think>
				Are you a man or a woman?
			</li>
		</condition>
	</template>
</category>

< system>

搜索引擎功能

在AIML实现的聊天机器人中,可以通过定义<system>python search.py</system>来实现python文件的调用,并使用<star>进行传参

具体示例如下述代码
<system>python baidu.py <star/></system>

<!-- 主循环 -->
<category>
    <pattern>SEARCH FOR *</pattern>
    <template>
        <think><set name="search"><star/></set></think>
        Which search engine would you like to use? Baidu,Yahoo, Yahoo Auctions, or Ebay.
    </template>
</category>

<category>
    <pattern>BAIDU</pattern>
    <that>WHICH SEARCH ENGINE WOULD YOU LIKE TO USE *</that>
    <template>
        <srai>SEARCH BAIDU FOR <get name="search"/></srai>
    </template>
</category>


<category>
<pattern>SEARCH BAIDU FOR *</pattern>
<template>
    <system>python baidu.py <star/></system>
    <srai>WEBDONE</srai>
</template>
</category>

<!-- 结束循环 -->
<category>
    <pattern>WEBDONE</pattern>
    <template>搜索完成~</template>
</category>

学习功能

同样,通过结合python文件可以实现聊天机器人的学习功能,如遇到语料库中没有的语句,可以通过“学习”保存到语料库中

<category>
  <pattern> * 答案是 * </pattern>
  <template>
    <system>python learn.py <star index="1" /> <star index="2" /> </system>
    <learn>auto-gen.aiml</learn>
    好的我学会了, 你可以再问我试试.
  </template>
</category>

可以通过定义learn.py文件同样进行传参学习

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿芒Aris

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值