这是为 Java 编程语言编写的 RiveScript 解释器库。 RiveScript 是聊天机器人的脚本语言,可以轻松编写触发器/响应对来构建机器人的智能。
RiveScript 是一种用于编写聊天机器人的脚本语言。它的语法非常简单,旨在易于阅读和快速编写。
RiveScript 的一个简单示例如下:
+ hello bot
- Hello human.
这与用户的消息“hello bot”匹配,并会回复“Hello human”。或者举一个稍微复杂一点的例子:
+ my name is *
* == => >Wow, we have the same name!
* != undefined => >Did you change your name?
- >Nice to meet you, !
RiveScript 的官方网站是 https://www.rivescript.com/
要在 Web 浏览器中测试 RiveScript,请尝试 RiveScript Playground。
另请查看RiveScript 社区 Wiki ,了解 RiveScript 的常见设计模式以及提示和技巧。
将rivescript-core
依赖项添加到您的项目中:
行家:
< dependency >
< groupId >com.rivescript groupId >
< artifactId >rivescript-core artifactId >
< version >0.10.0 version >
dependency >
摇篮:
dependencies {
compile " com.rivescript:rivescript-core:0.10.0 "
}
如果您想在 Spring Boot 应用程序中使用 RiveScript,请参阅 Spring Boot Starter 部分。
当用作编写自己的聊天机器人的库时,概要如下:
import com . rivescript . Config ;
import com . rivescript . RiveScript ;
// Create a new bot with the default settings.
RiveScript bot = new RiveScript ();
// To enable UTF-8 mode, you'd have initialized the bot like:
RiveScript bot = new RiveScript ( Config . utf8 ());
// Load a directory full of RiveScript documents (.rive files)
bot . loadDirectory ( "./replies" );
// Load an individual file.
bot . LoadFile ( "./testsuite.rive" );
// Sort the replies after loading them!
bot . sortReplies ();
// Get a reply.
String reply = bot . reply ( "user" , "Hello bot!" );
rivescript-core
发行版还包括一个用于测试 RiveScript 机器人的交互式 shell。使用磁盘上包含 RiveScript 文档的文件夹的路径运行它。例子:
java com.rivescript.cmd.Shell [options]
com.rivescript.RiveScript
构造函数采用可选的Config
实例。这是包含所有支持选项的完整示例。您只需为配置选项提供与默认值不同的值。
RiveScript bot = new RiveScript ( Config . newBuilder ()
. throwExceptions ( false ) // Whether exception throwing is enabled
. strict ( true ) // Whether strict syntax checking is enabled
. utf8 ( false ) // Whether UTF-8 mode is enabled
. unicodePunctuation ( "[.,!?;:]" ) // The unicode punctuation pattern
. forceCase ( false ) // Whether forcing triggers to lowercase is enabled
. concat ( ConcatMode . NONE ) // The concat mode
. depth ( 50 ) // The recursion depth limit
. sessionManager ( sessionManager ) // The session manager for user variables
. errorMessages ( errors ) // Map of custom error messages
. build ());
为了方便起见,您可以使用快捷方式:
// The default constructor uses a basic configuration.
RiveScript bot = new RiveScript ();
// This is similar as:
RiveScript bot = new RiveScript ( Config . basic ());
// To use the basic configuration with UTF-8 mode enabled use:
RiveScript bot = new RiveScript ( Config . utf8 ());
RiveScript 中的 UTF-8 支持被认为是一项实验性功能。默认情况下它是禁用的。
默认情况下(未启用 UTF-8 模式),触发器只能包含基本 ASCII 字符(无外来字符),并且用户的消息将去除除字母、数字和空格之外的所有字符。这意味着,例如,您无法在 RiveScript 回复中捕获用户的电子邮件地址,因为 @ 和 .人物。
启用 UTF-8 模式后,这些限制将被解除。触发器仅限于不包含某些元字符,例如反斜杠,并且用户消息仅去除反斜杠和 HTML 尖括号(如果您在 Web 应用程序中使用 RiveScript,则可以防止明显的 XSS)。此外,常见的标点符号被删除,默认设置为[.,!?;:]
。这可以通过向Config.Builder#unicodePunctuation()
方法提供新的正则表达式字符串来覆盖。例子:
// Make a new bot with UTF-8 mode enabled and override the punctuation
characters that get stripped from the user 's message.
RiveScript bot = new RiveScript ( Config . Builder
. utf8 ()
. unicodePunctuation ( "[.,!?;:]" )
. build ());
RiveScript 中的
标签将捕获用户的“原始”输入,因此您可以编写回复来获取用户的电子邮件地址或在其姓名中存储外来字符。
将rivescript-spring-boot-starter
依赖项添加到您的项目中:
行家:
< dependency >
< groupId >com.rivescript groupId >
< artifactId >rivescript-spring-boot-starter artifactId >
< version >0.10.0 version >
dependency >
摇篮:
dependencies {
compile " com.rivescript:rivescript-spring-boot-starter:0.10.0 "
}
启动器将自动将rivescript-core
依赖项添加到您的项目中,并触发自动配置以创建RiveScript
机器人实例。
尽管自动配置将使用合理的默认值来创建机器人实例,但可以在application.properties
/ application.yml
文件(或作为命令行开关)中指定以下属性来自定义自动配置行为:
rivescript:
enabled: true # Enable RiveScript for the application.
source-path: classpath:/rivescript/ # The comma-separated list of RiveScript source files and/or directories.
file-extensions: .rive, .rs # The comma-separated list of RiveScript file extensions to load.
throw-exceptions: false # Enable throw exceptions.
strict: true # Enable strict syntax checking.
utf8: false # Enable UTF-8 mode.
unicode-punctuation: [.,!?;:] # The unicode punctuation pattern (only used when UTF-8 mode is enabled).
force-case: false # Enable forcing triggers to lowercase.
concat: none # The concat mode (none|newline|space).
depth: 50 # The recursion depth limit.
error-messages: # The custom error message overrides. For instance `rivescript.error-messages.deepRecursion=Custom Deep Recursion Detected Message`
object-handlers: # The comma-separated list of object handler names to register (currently supported: `groovy`, `javascript`, `ruby`).
要在创建的RiveScript
机器人实例中自动注册自定义 Java 子例程和/或非默认支持的对象处理程序,请在应用程序上下文中定义适当的 bean,例如:
@ Bean public Map < String , Subroutine > subroutines () { // The key is the name of the Java object macro to register. Map < String , Subroutine > subroutines = new HashMap <>(); subroutines . put ( "subroutine1" , new Subroutine1 ()); subroutines . put ( "subroutine2" , new Subroutine2 ()); return subroutines ; } @ Bean public Map < String , ObjectHandler > objectHandlers () { // The key is the name of the programming language to register. 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
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.