# 理解 AIML 背后的概念

> AIML，即人工智能标记语言（Artificial Intelligence Markup Language），是一种用来创建聊天机器人（AI）的基于 XML 的语言，文件总是以扩展名（.aiml）保存。

作者 Shodipo Ayomide，发布于 2017年12月8日。网页版本：https://blog.shodipoayomide.com/zh/understanding-the-concept-behind-aiml

![](https://res.cloudinary.com/developerayo/image/upload/v1551555821/aiml.png)

## 什么是 AIML？

AIML，即人工智能标记语言（Artificial Intelligence Markup Language），是一种用来创建聊天机器人（AI）的基于 XML 的语言，文件总是以扩展名（.aiml）保存。

在 AIML 中，用来定义一个新功能的标签总是 `category` 标签，它下面还有另外两个标签，也就是 `pattern` 标签和 `template` 标签。在 template 标签里面，我们还可以内嵌其他标签，“具体取决于你在构建哪种类型的 BOT”，比如 `set`、`br/`、`srai`……等等

不过要注意一点，就像 HTML 或任何其他编程语言、脚本语言，甚至标记语言都有闭合标签一样，AIML 在这方面其实也没多大区别，闭合标签有 `/category`、`/template`、`/srai>`、`/set`……等等

## 示例

```HTML
<category><pattern>WHO IS ABRAHAM LINCOLN</pattern>

<template>was a great President of the United States, during the American Civil War.<br/>

</template>

</category>
```

当用户输入这句话，或者和它非常接近的内容时，机器人就会对这个输入作出回应

对“WHO IS ABRAHAM LINCOLN”，回应“was a great President of the United States, during the American Civil War.”。

现在，我们用 AIML 来创建一个简单的机器人

```html
<!doctype html>
<html>
  <body>
    <em>Emphasized text</em><br />
    <strong>Strong text</strong><br />
    <code>A piece of computer code</code><br />
    <samp>Sample output from a computer program</samp><br />
    <kbd>Keyboard input</kbd><br />
    <var>Variable</var>
  </body>
</html>
```

```AIML
<?xml version=”1.0" encoding=”UTF-8"?>

<aiml version=”1.0">

<category><pattern>Hi</pattern>

<template>Hi, I’m Oscar I was developed by Shodipo Ayomide 2017, <br/>How may I be of help</template>

</category>

<category><pattern>What are you </pattern>

<template>I said, My name is Oscar and I was developed by Shodipo Ayomide ok! </template>

</category>

<category><pattern>Why are you rude </pattern>

<template>Oh sorry, Please forgive my manners do not report me PLEASE!!! </template>

</category>

<category><pattern>ok </pattern>

<template>PLEASE!!! </template>

</category>

</aiml>
```

## 接下来解释一下模板（Template）

模板指定了对匹配到的模式所作出的回应。

**我的名字叫 Oscar**

模板可以使用变量，比如

你告诉过我你 `get name=”user-age”` 岁了
如果数据库里有用户的年龄，它就会把年龄取出来。

---

感谢阅读！🔥🚀
