在此 Java AIML 教程中,我们将学习使用 Java 创建一个简单的聊天机器人程序。 Chatbot 是一种应用程序,旨在模拟与人类用户的对话,尤其是通过 Internet。在内部,它使用任何NLP(自然语言处理) 系统来解释人类交互并回复有意义的信息。
AIML(人工智能标记语言)是一种用于创建自然语言软件代理的 XML 方言。它包含自然语言理解 (NLU) 单元内部使用的基本规则。它可以被认为是发动机的心脏。我们在 AIML 中添加的规则越多,我们的聊天机器人就会越智能。
了解NLP 和 NLU 之间的区别很重要。
program-ab
最新发行版。按照这些简单的步骤构建我们的第一个聊天机器人应用程序。
我们需要先将 program-ab
发行版解压缩到一个方便的文件夹中。我们需要从中获取 Ab.jar
和现有的 AIML 规则。
创建一个eclipse maven项目开始开发。它涉及非常标准的步骤,因此我假设您可以自己完成。
在我们创建了 maven 项目开始开发之后,让我们选择打包为 jar 和 maven 坐标作为您的选择并导入到 eclipse。
现在在基本文件夹中创建一个文件夹 lib
并将 Ab.jar
从 program-ab 分发 复制到此文件夹。
要将 AIML 添加到类路径,请将 Ab.jar
添加到 eclipse 中的部署程序集。或者,您可以将此 jar 安装到本地 maven 存储库中,然后使用它。
要在本地安装,请将 jar 放在任何文件夹中,并在 systemPath 标记中提供该位置。现在,将 AIML maven 依赖项 添加到 pom.xml。现在通过命令 mvn clean install
构建 maven 项目。
<dependencies>
<dependency>
<artifactId>com.google</artifactId>
<groupId>Ab</groupId>
<version>0.0.4.3</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/resources/lib/Ab.jar</systemPath>
</dependency>
</dependencies>
将 bots
文件夹从 program-a
b 目录复制到您的 Maven 项目的 resources
文件夹中。此文件夹包含我们最初将使用的默认 AIML 集。稍后我们将看到如何将自定义规则添加到聊天机器人中。
现在创建一个简单的 Java 程序,即 Chatbot.java
。它的 main()
方法将调用将在命令提示符下运行的聊天程序。
这个程序的基本结构是引入一个无限循环,在每个循环中从命令提示符处获取用户输入,然后我们将要求 program-ab API 对用户提供的输入给出答案。
wiki< 中提到了有关 program-ab API 交互的更多详细信息/a> 链接。
package com.cundage.demo.chatbot;
import java.io.File;
import org.alicebot.ab.Bot;
import org.alicebot.ab.Chat;
import org.alicebot.ab.History;
import org.alicebot.ab.MagicBooleans;
import org.alicebot.ab.MagicStrings;
import org.alicebot.ab.utils.IOUtils;
public class Chatbot {
private static final boolean TRACE_MODE = false;
static String botName = "super";
public static void main(String[] args) {
try {
String resourcesPath = getResourcesPath();
System.out.println(resourcesPath);
MagicBooleans.trace_mode = TRACE_MODE;
Bot bot = new Bot("super", resourcesPath);
Chat chatSession = new Chat(bot);
bot.brain.nodeStats();
String textLine = "";
while (true) {
System.out.print("Human : ");
textLine = IOUtils.readInputTextLine();
if ((textLine == null) || (textLine.length() < 1))
textLine = MagicStrings.null_input;
if (textLine.equals("q")) {
System.exit(0);
} else if (textLine.equals("wq")) {
bot.writeQuit();
System.exit(0);
} else {
String request = textLine;
if (MagicBooleans.trace_mode)
System.out.println(
"STATE=" + request + ":THAT=" + ((History) chatSession.thatHistory.get(0)).get(0)
+ ":TOPIC=" + chatSession.predicates.get("topic"));
String response = chatSession.multisentenceRespond(request);
while (response.contains("<"))
response = response.replace("<", "<");
while (response.contains(">"))
response = response.replace(">", ">");
System.out.println("Robot : " + response);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static String getResourcesPath() {
File currDir = new File(".");
String path = currDir.getAbsolutePath();
path = path.substring(0, path.length() - 2);
System.out.println(path);
String resourcesPath = path + File.separator + "src" + File.separator + "main" + File.separator + "resources";
return resourcesPath;
}
}
现在我们的程序已经准备就绪,我们现在可以开始聊天对话了。为此,只需将程序作为 Java 应用程序运行即可。要退出,您应键入 q or wq
。
Human : Hi
Robot : Hi! It's delightful to see you.
Human : How are you
Robot : Feeling really joyful today.
Human : My name is sajal
Robot : Glad to see you, Sajal
Human : what is my name
Robot : Sajal
Human : tell me my name
Robot : Sajal
像这样,您可以与聊天机器人进行基本交谈。
现在我们可以在机器人中添加自定义模式,为交互添加更多智能。为此,请在您的机器人 (.aiml
) 的 aiml
目录中创建一个新的 src\main\resources\bots\super\aiml
文件。
根据需要在该文件中添加尽可能多的类别。这里的类别指的是人类问题,模板指的是聊天机器人的回答。我创建了一个名为 a-custom-entry.aiml
的文件并添加了三个问题,如下所示。
<?xml version="1.0" encoding="UTF-8"?>
<aiml>
<category>
<pattern>WHAT IS JAVA</pattern>
<template>Java is a programming language.</template>
</category>
<category>
<pattern>WHAT IS CHAT BOT</pattern>
<template>Chatbot is a computer program designed to simulate conversation with human users, especially over the Internet.</template>
</category>
<category>
<pattern>WHAT IS INDIA</pattern>
<template>wonderful country.</template>
</category>
</aiml>
现在我们也可以问这三个问题了。
一旦我们的自定义 aiml 文件 准备就绪,我们需要为与aiml
并行的其他文件夹生成相应的条目。为此,我们需要调用 bot.writeAIMLFiles()
。
为此,我创建了另一个名为 AddAiml.java
的 java 程序。完成 aiml 编辑后,只需在启动机器人之前运行一次该程序。它会将这些自定义条目添加到机器人的大脑中。
package com.cundage.demo.chatbot;
import java.io.File;
import org.alicebot.ab.Bot;
import org.alicebot.ab.MagicBooleans;
public class AddAiml {
private static final boolean TRACE_MODE = false;
static String botName = "super";
public static void main(String[] args) {
try {
String resourcesPath = getResourcesPath();
System.out.println(resourcesPath);
MagicBooleans.trace_mode = TRACE_MODE;
Bot bot = new Bot("super", resourcesPath);
bot.writeAIMLFiles();
} catch (Exception e) {
e.printStackTrace();
}
}
private static String getResourcesPath() {
File currDir = new File(".");
String path = currDir.getAbsolutePath();
path = path.substring(0, path.length() - 2);
System.out.println(path);
String resourcesPath = path + File.separator + "src"
+ File.separator + "main" + File.separator + "resources";
return resourcesPath;
}
}
运行 AddAiml
后,将新条目添加到 AIML 后,再次运行聊天机器人程序并提出新问题。它应该给出适当的响应。
就我而言,这是输出。
Human : What is java Robot : Java is a programming language. Human : what is chat bot Robot : Chatbot is a computer program designed to simulate conversation with human users, especially over the Internet. Human : what is india Robot : wonderful country.
在这个 AIML Java 教程 中,我们学习了如何使用 program-ab 创建一个简单的基于命令行的聊天机器人程序 > 参考应用。下次当你有这样的需求时,你可以考虑基于 AIML 的聊天机器人。它能够轻松地做适度的事情。
为了增加你的知识,你可以尝试——
AIML 可以做很多事情,但现在可以使用更复杂的技术,尤其是在机器学习 领域,也请尝试学习这些技术。
快乐学习!!
下载 Java 聊天机器人源代码
地址:https://www.cundage.com/article/java-aiml-chatbot-example.html