這是為 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.