这是为 Java 编程语言编写的 RiveScript 解释器库。 RiveScript 是聊天机器人的脚本语言,可以轻松编写触发器/响应对来构建机器人的智能。
RiveScript 是一种用于编写聊天机器人的脚本语言。它的语法非常简单,旨在易于阅读和快速编写。
RiveScript 的一个简单示例如下:
+ hello bot - Hello human.
这与用户的“hello bot”消息匹配,并会回复“Hello human”。或者举一个稍微复杂一点的例子:
+ my name is * * <formal> == <bot name> => <set name=<formal>>Wow, we have the same name! * <get name> != undefined => <set name=<formal>>Did you change your name? - <set name=<formal>>Nice to meet you, <get name>!
RiveScript 的官方网站是 https://www.rivescript.com/
要在 Web 浏览器中测试 RiveScript,请尝试 RiveScript Playground。
API文档:http://www.javadoc.io/doc/com.rivescript/rivescript-core/
工作草案:http://www.rivescript.com/wd/RiveScript
另请查看RiveScript 社区 Wiki ,了解 RiveScript 的常见设计模式以及提示和技巧。
将rivescript-core
依赖项添加到您的项目中:
行长:
<依赖关系> <groupId>com.rivescript</groupId> <artifactId>rivescript-core</artifactId> <版本>0.10.0</版本> </依赖>
摇篮:
依赖项{ 编译“com.rivescript:rivescript-core:0.10.0”}
如果您想在 Spring Boot 应用程序中使用 RiveScript,请参阅 Spring Boot Starter 部分。
当用作编写自己的聊天机器人的库时,概要如下:
import com.rivescript.Config;import com.rivescript.RiveScript;// 使用默认设置创建一个新的机器人。RiveScript bot = new RiveScript();// 要启用 UTF-8 模式,您需要像这样初始化机器人:RiveScript bot = new RiveScript(Config.utf8());// 加载一个充满 RiveScript 文档的目录 (.rive files)bot.loadDirectory("./replies");// 加载单个文件.bot.LoadFile("./testsuite.rive");// 加载回复后对回复进行排序!bot.sortReplies();//获取回复。Stringreply = bot.reply("user", "Hello bot!");
rivescript-core
发行版还包括一个用于测试 RiveScript 机器人的交互式 shell。使用磁盘上包含 RiveScript 文档的文件夹的路径运行它。例子:
java com.rivescript.cmd.Shell [options] </path/to/documents>
com.rivescript.RiveScript
构造函数采用可选的Config
实例。这是包含所有支持选项的完整示例。您只需提供与默认值不同的配置选项值。
RiveScript 机器人 = new RiveScript(Config.newBuilder() .throwExceptions(false) // 是否启用异常抛出.strict(true) // 是否启用严格语法检查.utf8(false) // 是否启用UTF-8模式.unicodePunctuation("[.,!?;: ]") // unicode 标点pattern.forceCase(false) // 是否启用强制触发器为小写。concat(ConcatMode.NONE) // concat 模式.depth(50) // 递归深度limit .sessionManager(sessionManager) // 用户变量的会话管理器.errorMessages(errors) // 自定义错误消息的映射.build());
为了方便起见,您可以使用快捷方式:
// 默认构造函数使用基本配置。RiveScript bot = new RiveScript();// 这类似于:RiveScript bot = new RiveScript(Config.basic());// 使用 UTF-8 模式的基本配置启用使用:RiveScript bot = new RiveScript(Config.utf8());
RiveScript 中的 UTF-8 支持被认为是一项实验性功能。默认情况下它是禁用的。
默认情况下(未启用 UTF-8 模式),触发器只能包含基本 ASCII 字符(无外来字符),并且用户的消息将去除除字母、数字和空格之外的所有字符。这意味着,例如,您无法在 RiveScript 回复中捕获用户的电子邮件地址,因为 @ 和 .人物。
启用 UTF-8 模式后,这些限制将被解除。触发器仅限于不包含某些元字符,例如反斜杠,并且用户消息仅去除反斜杠和 HTML 尖括号(如果您在 Web 应用程序中使用 RiveScript,则可以防止明显的 XSS)。此外,常见的标点符号被删除,默认设置为[.,!?;:]
。这可以通过向Config.Builder#unicodePunctuation()
方法提供新的正则表达式字符串来覆盖。例子:
// 创建一个启用 UTF-8 模式的新机器人,并覆盖从用户消息中删除的标点符号。RiveScript bot = new RiveScript(Config.Builder.utf8() .unicodePunctuation("[.,!?;:]") 。建造());
RiveScript 中的<star>
标签将捕获用户的“原始”输入,因此您可以编写回复来获取用户的电子邮件地址或在其姓名中存储外来字符。
将rivescript-spring-boot-starter
依赖项添加到您的项目中:
行长:
<依赖关系> <groupId>com.rivescript</groupId> <artifactId>rivescript-spring-boot-starter</artifactId> <版本>0.10.0</版本> </依赖>
摇篮:
依赖项{ 编译“com.rivescript:rivescript-spring-boot-starter:0.10.0”}
启动器将自动将rivescript-core
依赖项添加到您的项目中,并触发自动配置以创建RiveScript
机器人实例。
尽管自动配置将使用合理的默认值来创建机器人实例,但可以在application.properties
/ application.yml
文件中指定以下属性(或作为命令行开关)来自定义自动配置行为:
脚本: enabled: true # 为应用程序启用 RiveScript。 source-path: classpath:/rivescript/ # RiveScript 源文件和/或目录的逗号分隔列表。 file-extensions: .rive, .rs # 要加载的以逗号分隔的 RiveScript 文件扩展名列表。 throw-exceptions: false # 启用抛出异常。 strict: true # 启用严格语法检查。 utf8: false # 启用 UTF-8 模式。 unicode-punctuation: [.,!?;:] # unicode 标点符号模式(仅在启用 UTF-8 模式时使用)。 force-case: false # 启用强制触发器为小写。 concat: none # 连接模式(无|换行|空格)。 depth: 50 # 递归深度限制。 error-messages: # 自定义错误消息覆盖。例如 `rivescript.error-messages.deepRecursion=自定义深度递归检测到的消息` object-handlers: # 要注册的对象处理程序名称的逗号分隔列表(当前支持:`groovy`、`javascript`、`ruby`)。
要在创建的RiveScript
机器人实例中自动注册自定义 Java 子例程和/或非默认支持的对象处理程序,请在应用程序上下文中定义适当的 bean,例如:
@Beanpublic Map<String, Subroutine> subroutines() {// key是要注册的Java对象宏的名称。Map<String, Subroutine> subroutines = new HashMap<>();subroutines.put("subroutine1", new Subroutine1());subroutines.put("subroutine2", new Subroutine2());返回子例程; }@Beanpublic Map<String, ObjectHandler> objectHandlers() {// key是要注册的编程语言的名称。Map<String, ObjectHandler> objectHandlers = new HashMap<>();objectHandlers.put("handler1", new ObjectHandler1());objectHandlers.put("handler2", new ObjectHandler2());return objectHandlers; }
要编译、测试、构建所有 jar 和文档,请运行:
./gradlew build
要将所有 jar 安装到本地 Maven 缓存中,请运行:
./gradlew install
/samples
文件夹包含 Java RiveScript 机器人实现的各种示例。
rsbot
- RSBot.java
是使用com.rivescript.cmd.Shell
的简单实现。
这些命令可以在 RSBot 中的输入提示处使用:
/quit - Quit the program /dump topics - Dump the internal topic/trigger/reply struct (debugging) /dump sorted - Dump the internal trigger sort buffers (debugging) /last - Print the last trigger you matched.
要执行RSBot
以开始与基于 Eliza 的演示运行聊天:
./gradlew :rivescript-samples-rsbot:runBot --console plain
spring-boot-starter-rsbot
- 此示例使用 RiveScript Spring Boot Starter 自动配置RiveScript
机器人实例。
要开始与演示机器人聊天,请运行:
./gradlew :rivescript-samples-spring-boot-starter-rsbot:bootRun --console plain
诺亚·佩瑟布里奇,https://www.kirsle.net/
马塞尔·奥弗迪克,https://twitter.com/marceloverdijk
The MIT License (MIT) Copyright (c) 2016 the original author or authors. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.