AIML <srai>標記

AIML <srai>標記是一個多功能標籤。 此標記使AIML能夠爲同一模板定義不同的目標。

語法

<srai> pattern </srai>

以下是與srai相關的常用術語 -

  • 符號縮減
  • 分而治之
  • 同義詞解析
  • 關鍵詞檢測

1. 符號縮減

符號縮減技術用於簡化模式,它有助於用簡單的模式減少複雜的語法模式。

例如,請考慮以下對話。

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>

示例
D:/software/ab/bots/test/aiml目錄中創建文件:srai.aiml,並在D:/software/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

執行程序

打開命令提示符,轉到目錄:D:/software/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.

2. 分而治之

分而治之用於重複使用子句來做出完整的回覆。 它有助於減少定義多個類別。

例如,考慮以下對話。

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

每當用戶在句子開頭說Bye時,機器人回覆GoodBye!

在這裏將使用<srai>標籤。參考以下實現步驟 -

第1步: 創建類別

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

第2步: 使用<srai>標籤創建通用類別

<category>
   <pattern>BYE *</pattern>

   <template>
      <srai>BYE</srai>
   </template>

</category>

示例

D:/software/ab/bots/test/aiml目錄中創建文件:srai.aiml,並在D:/software/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>

文件:srai.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

執行程序

打開命令提示符。 進入到目錄:D:/software/ab 並鍵入以下命令 -

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

執行上面示例代碼,得到以下結果 -

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

3. 同義詞解析

同義詞是具有相似含義的詞。 機器人應該以相同的方式回覆類似的單詞。
例如,請考慮以下對話。

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

每當用戶說FactoryIndustry時,這裏機器人回覆: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>

D:/software/ab/bots/test/aiml目錄中創建:srai.aiml 文件,以及在D:/software/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>

文件:srai.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

執行程序
打開命令提示符。 進入到目錄:D:/software/ab/ 並鍵入以下命令 -

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

執行上面命令,得到以下結果 -

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

關鍵詞檢測

使用<srai>標籤,當用戶鍵入特定關鍵詞時,可以返回一個簡單的響應,例如,無論句子中school還是School都一樣,它不分大小寫字母。

例如,請考慮以下對話。

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.

在這裏,每當用戶在句子中包含:School時,機器人回覆標準信息:「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>

D:/software/ab/bots/test/aiml目錄中創建:srai.aiml 文件,以及在D:/software/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>

文件:srai.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

執行程序
打開命令提示符。 進入到目錄:D:/software/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.