NLP - AIML
文章目錄
- 關(guān)于AIML
- 關(guān)于 PYAIML
- 安裝
- AIML標(biāo)簽
- 元素示例
- AIML
- Topic 主題
- Category 類別
- Pattern 模式
- Template 模板
- 原子模板元素
- Star(*)
- Template-side that
- Input
- Thatstar
- Topicstar
- Get
- Short-cut
- System-defined predicates
- 文本格式化元素
- Text Format
- 條件元素
- 捕獲元素
- 轉(zhuǎn)換元素
- 隱藏元素
- think
- Learn
- 擴(kuò)展處理器元素
- System
- 使用 aiml 自帶文件
- 創(chuàng)建配置文件
- 相關(guān)資料
關(guān)于AIML
AIML:Artificial Intelligence Markup Language,人工智能標(biāo)記語(yǔ)言
AIML是由 Alicebot自由軟件社區(qū) 和 Richard S. Wallace博士在1995-2000期間開發(fā)。
AIML用于創(chuàng)建或自定義Alicebot,這是一個(gè)基于 A.L.I.C.E(人工語(yǔ)言互聯(lián)網(wǎng)計(jì)算機(jī)實(shí)體)聊天框應(yīng)用程序的免費(fèi)軟件。
- 官網(wǎng):http://www.aiml.foundation
- AIML docs:http://www.aiml.foundation/doc.html
- Github:https://github.com/AIML-Foundation
- PYPI:https://pypi.org/project/aiml/
關(guān)于 PYAIML
- Pypi:https://pypi.org/project/PyAIML/
- Github:https://github.com/andelf/PyAIML
安裝
(base) $ pip install aimlAIML標(biāo)簽
以下是AIML文檔中常用的重要標(biāo)簽。
| 1 | <aiml> | 定義AIML文檔的開頭和結(jié)尾。 |
| 2 | <category> | 定義Alicebot知識(shí)庫(kù)中的知識(shí)單元。 |
| 3 | <pattern> | 定義模式以匹配用戶可以輸入到Alicebot的模式。 |
| 4 | <template> | 定義Alicebot對(duì)用戶輸入的響應(yīng)。 |
我們將在AIML基礎(chǔ)標(biāo)簽章節(jié)中討論這些標(biāo)簽。
以下是一些其他廣泛使用的aiml標(biāo)簽。 我們將在接下來(lái)的章節(jié)中詳細(xì)討論每個(gè)標(biāo)簽。
| 1 | <star> | 用于匹配<pattern>標(biāo)簽中的通配符*字符 |
| 2 | <srai> | 多用途標(biāo)簽,用于調(diào)用/匹配其他類別。 |
| 3 | <random> | 使用<random>獲取隨機(jī)響應(yīng)。 |
| 4 | <li> | 用于表示多個(gè)響應(yīng)。 |
| 5 | <set> | 用于在AIML變量中設(shè)置值。 |
| 6 | <get> | 用于獲取存儲(chǔ)在AIML變量中的值。 |
| 7 | <that> | 在AIML中用于根據(jù)上下文進(jìn)行響應(yīng)。 |
| 8 | <topic> | 在AIML中用于存儲(chǔ)上下文,以便以后可以基于該上下文完成對(duì)話。 |
| 9 | <think> | 在AIML中用于存儲(chǔ)變量而不通知用戶。 |
| 10 | <condition> | 與編程語(yǔ)言中的switch語(yǔ)句類似。 它有助于ALICE響應(yīng)匹配輸入。 |
- 類別,代表一次會(huì)話
- 模式,用來(lái)匹配對(duì)方的問題
- 模板
- 原子模板元素
- <Template-side that>
- <Input>
- <Thatstar>
- <Topicstar>
- <Get>
- <Short-cut>
- <System-defined predicates>
- <Text Format>
- 條件元素
- 捕獲元素
- 轉(zhuǎn)換元素
- 隱藏元素
- 擴(kuò)展處理器元素
元素示例
AIML
Aiml:定義在AIML文件的開頭和結(jié)尾。
它包含版本和編碼屬性的版本和編碼信息。
語(yǔ)法結(jié)構(gòu)
<aiml:aiml version = number> <!-- Content: top-level-elements) --> </aiml:aiml>案例
<?xml version="1.0" encoding="UTF-8"?> <aiml> <category><pattern>hi</pattern><template>你好</template> </category> </aiml>Topic 主題
用于AIML存儲(chǔ)上下文,這樣以后可以談話基于這一 背景下進(jìn)行。
語(yǔ)法規(guī)則
<!-- Category: top-level-element --> <aiml:topic name=aiml-simple-pattern-expression> <!-- Content: aiml:category+ --> </aiml:topic>說(shuō)明
- 頂級(jí)元素:可以包含
- 可選元素:一個(gè)AMIL對(duì)象必須包含0/1個(gè)主題元素
- Name屬性:必須包括Name屬性
- 獨(dú)立空間:完整的匹配路徑
案例
設(shè)置主題:只有當(dāng)前的topic系統(tǒng)變量是“功夫” 時(shí)才會(huì)匹配到這里;
<aiml> <topic name="功夫"> <category> <pattern> phrase </pattern> <that> phrase </that> <template> phrase </template> </category> </topic> </aiml>Category 類別
語(yǔ)法規(guī)則
<!-- Category: top-level-element --> < aiml:category><!-- Content: aim/-category-elements --> </aiml:category>規(guī)則說(shuō)明
Pattern 模式
語(yǔ)法規(guī)則
<!-- Category: aiml-category-elements -- < aiml:pattern> <!-- Content: aiml-pattern- expression -> </aiml:pattern>二、規(guī)則說(shuō)明
Template 模板
語(yǔ)法規(guī)則
<!-- Category: aiml-category-elements --> < aiml:template><!-- Content: aim/-template-elements --> </aiml:template>規(guī)則說(shuō)明
原子模板元素
Star(*)
替代Pattern中的第Index個(gè)“*”所表述的內(nèi)容 1.1 語(yǔ)法規(guī)則
語(yǔ)法規(guī)則
<! - Category:aiml-template-elements - > <aiml:star index = single-integer-index />案例
用戶:我的名字叫黑山!
<pattern>* 名 字 叫 *</pattern> <template>呵呵,我知道了,你的名字叫<star index="2"/>。 </template>注意事項(xiàng) pattern元素里的匹配模式是用* 號(hào)表示任意匹配的,但在其他元素里面不能用* 號(hào),而用<star/>這個(gè)元素來(lái)表示。
Template-side that
<!-- Category: aiml-template-elements --> < aiml:that index = (single- integer-indexl comma- separated-integer-pair) />案例
對(duì)話場(chǎng)景:
用戶:你好.
機(jī)器人:計(jì)算機(jī)的型號(hào)是什么
用戶: p4
機(jī)器人: p4這個(gè)型號(hào)是計(jì)算機(jī)里面很好的商品
Input
Input:用戶輸入的內(nèi)容
語(yǔ)法規(guī)則:
示例
<input index="2"/>將輸出用戶倒數(shù)第2次的輸入,看如 下對(duì)話: 用戶:好阿 機(jī)器人:你也好,你叫什么名字? 用戶:我叫黑山 機(jī)器人:呵呵,你好黑山。 如果有aiml文件里有片斷: <category> <pattern>嘿 嘿</pattern> <template> <gossip>你剛才說(shuō):“<input index="2"/>”? </gossip> </template> </category> 如果這個(gè)時(shí)候用戶輸入“嘿嘿”,那么機(jī)器人將回復(fù):你 剛才說(shuō):“我叫黑山”? Index屬性的數(shù)字表示倒數(shù)第幾句話,1:倒數(shù)第一句,2: 倒數(shù)第2句,依此類推。Thatstar
語(yǔ)法規(guī)則:
<!-- Category: aiml-template-elements --> <aiml:thatstar index = single-integer-index />案例
對(duì)話場(chǎng)景:
用戶:你好
機(jī)器人:計(jì)算機(jī) 的 型 號(hào) 是 什 么
用戶:p4 機(jī)器人:p4這個(gè)型號(hào)是計(jì)算機(jī)里面很好的商品
Topicstar
5.1 語(yǔ)法規(guī)則:
案例
Get
Get:用于獲取存儲(chǔ)在AIML變量的值;
6.1 語(yǔ)法規(guī)則:
Short-cut
7.1 SR:
語(yǔ)法規(guī)則:
<! - Category:aiml-template-elements - > <AIML:SR />
7.2 Person2:第一人稱替換為第二人稱
語(yǔ)法規(guī)則:
7.3 Person:第一人稱替換為第三人稱
語(yǔ)法規(guī)則:
gender:替換性別以及代名詞
語(yǔ)法規(guī)則
案例
將被替換成:He told her to take a hike,跟性 別有關(guān)的單詞都將被替換,中文怎么處理不是很清 楚。
<gender>She told him to take a hike.</gender>System-defined predicates
模板-原子模板元素
7.1 Date:系統(tǒng)時(shí)間; 語(yǔ)法規(guī)則: <!-- Category: aiml-template-elements --> <aiml:date/> 7.2 ID:用戶ID 語(yǔ)法規(guī)則: <!-- Category: aiml-template-elements --> <aiml:id/> 7.3 Size:元素?cái)?shù)量 語(yǔ)法規(guī)則: <!-- Category: aiml-template-elements --> <aiml:size/> 7.4 Version:版本 語(yǔ)法規(guī)則: <!-- Category: aiml-template-elements --> <aiml:version/>文本格式化元素
Text Format
模板-文本格式化元素
1 Uppercase:元素轉(zhuǎn)換為大寫 語(yǔ)法規(guī)則: <!-- Category: aiml-template-elements --> <aiml:uppercase> <!-- Contents: aiml-template-elements --> </aiml:uppercase>2 Lowercase:元素轉(zhuǎn)換為小寫 語(yǔ)法規(guī)則: <!-- Category: aiml-template-elements --> <aiml:lowercase> <!-- Content: aiml-template-elements --> </aiml:lowercase>3 Formal:格式化,首字母大寫,標(biāo)題樣式 語(yǔ)法規(guī)則: <!-- Category: aiml-template-elements --> <aiml:formal> <!-- Content: aiml-template-elements --> </aiml:formal>4. Sentence:格式化句子 語(yǔ)法規(guī)則: <!-- Category: aiml-template-elements --> <aiml:sentence> <!-- Content: aiml-template-elements --> </aiml:sentence> 典型示例: <sentence>this is some kind of sentence test.</sentence> This is some kind of sentence test. 典型案例: <upcase>xiaoxie</upcase> XIAOXIE條件元素
1.Condition:條件語(yǔ)句 典型示例: <?xml version="1.0"encoding="UTF-8"?> <aiml version="1.0.1" encoding="UTF-8"?> <category><pattern> HOW ARE YOUFEELING TODAY </pattern> <template><think><setname="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!2.Random:隨機(jī)選擇 語(yǔ)法結(jié)構(gòu): <!-- Category: aiml-template-elements --> <aiml:random> <!-- Contents: default-list-item+ --> </aiml:random>典型示例:
捕獲元素
.Set:設(shè)置變量取值
語(yǔ)法結(jié)構(gòu):
示例
轉(zhuǎn)換元素
1.Person2:人稱替換 <!-- Category: aiml-template-elements --> <aiml:person2> <!-- Contents: aiml-template-elements --> </aiml:person2>2.Person:人稱替換 <!-- Category: aiml-template-elements --> <aiml:person> <!-- Contents: aiml-template-elements --> </aiml:person>3.Gender:性別替換 <!-- Category: aiml-template-elements --> <aiml:gender> <!-- Contents: aiml-template-elements --> </aiml:gender>隱藏元素
think
記錄用戶輸入且里面的內(nèi)容,不回復(fù)給用戶
語(yǔ)法規(guī)則:
<!-- Category: aiml-template-elements --> <aiml:think><!-- Contents: aim/- template -elements --> </aiml:think>案例:
下面的示例:把female這個(gè)值保存到gender這個(gè)變量里,且回復(fù)內(nèi)容不包括female
Learn
Learn:檢索資源,并加載
語(yǔ)法規(guī)則:
典型案例: <aiml version="1.0.1" encoding= "UTF-8"> <!-- std-startup.xml --> <!-- Category是-個(gè)自劫的AIML単元--> <category><!-- Pattern用來(lái)匹配用戸輸入--><!--如果用戸輸入"LOAD AIML B" --><pattern>LOAD AIML C</pattern><!-- Template是模式的喃虛--<!--這里學(xué)習(xí)一個(gè)aiml文件--><template><learn> basic. chat.aiml</learn><!--你可以在遠(yuǎn)里添加更多的aiml文件--><!-- <learn> more_ aiml.aiml</learn>--><learn> tuling. aiml</learn></template> </category> </aiml>
擴(kuò)展處理器元素
System
語(yǔ)法結(jié)構(gòu)
<aiml:system> <!-- Contents: character data, aiml-template- elements --> </aiml:system>示例
表示取系統(tǒng)當(dāng)前日期
使用 aiml 自帶文件
import aimlimport os import sysdef get_module_dir(name):path = getattr(sys.modules[name], '__file__', None)print(path) # /Users/xx/opt/anaconda3/lib/python3.7/site-packages/aiml/__init__.pyif not path:raise AttributeError('module %s has not attribute __file__' % name)return os.path.dirname(os.path.abspath(path))def test():alice_path = get_module_dir('aiml') + '/botdata/alice'print(alice_path)#切換到語(yǔ)料庫(kù)所在工作目錄os.chdir(alice_path)alice = aiml.Kernel()alice.learn("startup.xml")alice.respond('LOAD ALICE')while True:print(alice.respond(input("Enter your message >> ")))if __name__ == '__main__':test1()效果
Loading startup.xml...done (0.03 seconds) Loading xfind.aiml...done (0.00 seconds) Loading mp6.aiml...done (0.09 seconds) ... Loading mp5.aiml...done (0.14 seconds) Loading iu.aiml...done (0.00 seconds) Loading money.aiml...done (0.00 seconds) Loading reduction0.safe.aiml...done (0.21 seconds) Enter your message >> hi Hi there! Enter your message >> Enter your message >> this is my friend You don't say. Enter your message >> what is your name I am called Nameless.創(chuàng)建配置文件
1、創(chuàng)建 startup.xml,作為加載AIML文件的主入口點(diǎn)
我們想要匹配模式load aiml b,然后讓它加載我們的aiml大腦作為響應(yīng)。我們將在一步內(nèi)創(chuàng)建basic_chat.aiml文件。
2、創(chuàng)建 basic_chat.aiml
上面 startup.xml 中只能處理一個(gè)模式: load aiml b。當(dāng)我們向機(jī)器人輸入那個(gè)命令時(shí),它將會(huì)嘗試加載basic_chat.aiml。如果沒有創(chuàng)建則無(wú)效。
下面是 basic_chat.aiml 的內(nèi)容,將匹配兩個(gè)基本的模式和響應(yīng)。
相關(guān)資料
- Wiki: AIML
https://en.wikipedia.org/wiki/AIML - https://blog.csdn.net/xvshu/article/details/80973649
- https://segmentfault.com/a/1190000010216805
- 易百教程:AIML
https://www.yiibai.com/aiml/aiml_introduction.html - 使用Python AIML搭建聊天機(jī)器人的方法示例
https://www.jb51.net/article/143395.htm
總結(jié)
以上是生活随笔為你收集整理的NLP - AIML的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 超强 | 保险单据在线OCR,秒速识别保
- 下一篇: 王晶:华为云OCR文字识别服务技术实践、