Public Member Functions | |
void | dumpSorted () |
void | dumpTopics () |
String | error () |
String | getCurrentUser () |
String[] | getUsers () |
String | getUservar (String user, String name) |
ClientManager | getUservars () |
HashMap< String, String > | getUservars (String user) |
boolean | loadDirectory (final String path, final String[] exts) |
boolean | loadDirectory (final String path) |
boolean | loadFileJava (String file) |
boolean | loadFileRive (final String file) |
BotReply | reply (String username, String message) |
RiveScript (boolean debug) | |
RiveScript () | |
boolean | setGlobal (String name, String value) |
void | setHandler (String name, ObjectHandler< BotReply > handler) |
boolean | setPersonSubstitution (String pattern, String output) |
boolean | setSubstitution (String pattern, String output) |
boolean | setUservar (String user, String name, String value) |
boolean | setUservars (String user, HashMap< String, String > data) |
void | setUservars (ClientManager clients) |
boolean | setVariable (String name, String value) |
void | sortReplies () |
boolean | stream (String code) |
boolean | stream (String[] code) |
Static Public Attributes | |
static final String | TYPE_JAVA = "java" |
static final String | TYPE_PERL = "perl" |
static final String | TYPE_PYTHON = "python" |
static final double | VERSION = 0.02 |
Protected Member Functions | |
boolean | error (final String message) |
List< File > | getFiles (final File directory, final String type) |
HashMap< String, ObjectHandler < BotReply > > | getHandlers () |
void | say (String line) |
Protected Attributes | |
HashMap< String, String > | objects |
Private Member Functions | |
void | cry (String line) |
void | cry (String text, String file, int line) |
String | formatMessage (String message) |
String[] | loadFile (String file, File fh) throws IOException |
boolean | parse (String filename, String[] code) |
String | processTags (String user, Client profile, String message, String reply, ArrayList< String > vstars, ArrayList< String > vbotstars, int step) |
BotReply | processTags (String user, Client profile, String message, BotReply botReply, ArrayList< String > vstars, ArrayList< String > vbotstars, int step) |
BotReply | reply (String user, String message, boolean begin, int step) |
String | stringTransform (String format, String text) |
void | trace (IOException e) |
String | triggerRegexp (String user, Client profile, String trigger) |
Private Attributes | |
HashMap< String, Vector< String > > | arrays |
ClientManager | clients = new ClientManager() |
String | currentUser = null |
boolean | debug = false |
int | depth = 50 |
String | error = "" |
HashMap< String, String > | globals |
HashMap< String, ObjectHandler < BotReply > > | handlers |
HashMap< String, String > | person |
String[] | person_s |
HashMap< String, String > | subs |
String[] | subs_s |
TopicManager | topics = new TopicManager() |
HashMap< String, String > | vars |
Static Private Attributes | |
static final String | CMD_CONDITION = "*" |
static final String | CMD_CONTINUE = "^" |
static final String | CMD_DEFINE = "!" |
static final String | CMD_ENDLABEL = "<" |
static final String | CMD_LABEL = ">" |
static final String | CMD_PREVIOUS = "%" |
static final String | CMD_REDIRECT = "@" |
static final String | CMD_REPLY = "-" |
static final String | CMD_TRIGGER = "+" |
static Random | rand = new Random() |
static final double | RS_VERSION = 2.0 |
A RiveScript interpreter written in Java.
SYNOPSIS
import com.rivescript.RiveScript;
// Create a new interpreter
RiveScript rs = new RiveScript();
// Load a directory full of replies in *.rive files
rs.loadDirectory("./replies");
// Sort replies
rs.sortReplies();
// Get a reply for the user
String reply = rs.reply("user", "Hello bot!");
Definition at line 68 of file RiveScript.java.
com.rosalfred.core.ia.rivescript.RiveScript.RiveScript | ( | boolean | debug | ) | [inline] |
Create a new RiveScript interpreter object, specifying the debug mode.
debug | Enable debug mode (a *lot* of stuff is printed to the terminal) |
Definition at line 140 of file RiveScript.java.
Create a new RiveScript interpreter object.
Definition at line 150 of file RiveScript.java.
void com.rosalfred.core.ia.rivescript.RiveScript.cry | ( | String | line | ) | [inline, private] |
Print a line of warning text to the terminal.
line | The line of warning text. |
Definition at line 2284 of file RiveScript.java.
void com.rosalfred.core.ia.rivescript.RiveScript.cry | ( | String | text, |
String | file, | ||
int | line | ||
) | [inline, private] |
Print a line of warning text including a file name and line number.
text | The warning text. |
file | The file name. |
line | The line number. |
Definition at line 2295 of file RiveScript.java.
void com.rosalfred.core.ia.rivescript.RiveScript.dumpSorted | ( | ) | [inline] |
DEVELOPER: Dump the trigger sort buffers to the terminal.
Definition at line 2179 of file RiveScript.java.
void com.rosalfred.core.ia.rivescript.RiveScript.dumpTopics | ( | ) | [inline] |
DEVELOPER: Dump the entire topic/trigger/reply structure to the terminal.
Definition at line 2196 of file RiveScript.java.
String com.rosalfred.core.ia.rivescript.RiveScript.error | ( | ) | [inline] |
Return the text of the last error message given.
Definition at line 161 of file RiveScript.java.
boolean com.rosalfred.core.ia.rivescript.RiveScript.error | ( | final String | message | ) | [inline, protected] |
Set the error message.
message | The new error message to set. |
Definition at line 170 of file RiveScript.java.
String com.rosalfred.core.ia.rivescript.RiveScript.formatMessage | ( | String | message | ) | [inline, private] |
Format the user's message to begin reply matching. Lowercases it, runs substitutions, and neutralizes what's left.
message | The input message to format. |
Definition at line 2157 of file RiveScript.java.
String com.rosalfred.core.ia.rivescript.RiveScript.getCurrentUser | ( | ) | [inline] |
Definition at line 585 of file RiveScript.java.
List<File> com.rosalfred.core.ia.rivescript.RiveScript.getFiles | ( | final File | directory, |
final String | type | ||
) | [inline, protected] |
Definition at line 221 of file RiveScript.java.
HashMap<String, ObjectHandler<BotReply> > com.rosalfred.core.ia.rivescript.RiveScript.getHandlers | ( | ) | [inline, protected] |
Definition at line 175 of file RiveScript.java.
String [] com.rosalfred.core.ia.rivescript.RiveScript.getUsers | ( | ) | [inline] |
Get a list of all the user IDs the bot knows about.
Definition at line 580 of file RiveScript.java.
String com.rosalfred.core.ia.rivescript.RiveScript.getUservar | ( | String | user, |
String | name | ||
) | [inline] |
Retrieve a single variable from a user's profile.
Returns null if the user doesn't exist. Returns the string "undefined" if the variable doesn't exist.
user | The user ID to get data from. |
name | The name of the variable to get. |
Definition at line 613 of file RiveScript.java.
ClientManager com.rosalfred.core.ia.rivescript.RiveScript.getUservars | ( | ) | [inline] |
Definition at line 569 of file RiveScript.java.
HashMap<String, String> com.rosalfred.core.ia.rivescript.RiveScript.getUservars | ( | String | user | ) | [inline] |
Retrieve a listing of all the uservars for a user as a HashMap. Returns null if the user doesn't exist.
user | The user ID to get the vars for. |
Definition at line 595 of file RiveScript.java.
boolean com.rosalfred.core.ia.rivescript.RiveScript.loadDirectory | ( | final String | path, |
final String[] | exts | ||
) | [inline] |
Load a directory full of RiveScript documents, specifying a custom list of valid file extensions.
path | The path to the directory containing RiveScript documents. |
exts | A string array containing file extensions to look for. |
Definition at line 190 of file RiveScript.java.
boolean com.rosalfred.core.ia.rivescript.RiveScript.loadDirectory | ( | final String | path | ) | [inline] |
Load a directory full of RiveScript documents (.rive files).
path | The path to the directory containing RiveScript documents. |
Definition at line 254 of file RiveScript.java.
String [] com.rosalfred.core.ia.rivescript.RiveScript.loadFile | ( | String | file, |
File | fh | ||
) | throws IOException [inline, private] |
file | |
fh |
IOException |
Definition at line 349 of file RiveScript.java.
boolean com.rosalfred.core.ia.rivescript.RiveScript.loadFileJava | ( | String | file | ) | [inline] |
Load a single RiveScript document.
file | Path to a RiveScript document. |
Definition at line 301 of file RiveScript.java.
boolean com.rosalfred.core.ia.rivescript.RiveScript.loadFileRive | ( | final String | file | ) | [inline] |
Load a single RiveScript document.
file | Path to a RiveScript document. |
Definition at line 264 of file RiveScript.java.
boolean com.rosalfred.core.ia.rivescript.RiveScript.parse | ( | String | filename, |
String[] | code | ||
) | [inline, private] |
Parse RiveScript code and load it into internal memory.
filename | A file name to associate with this code (for error reporting) |
code | A string array of all the code to parse. |
Definition at line 632 of file RiveScript.java.
String com.rosalfred.core.ia.rivescript.RiveScript.processTags | ( | String | user, |
Client | profile, | ||
String | message, | ||
String | reply, | ||
ArrayList< String > | vstars, | ||
ArrayList< String > | vbotstars, | ||
int | step | ||
) | [inline, private] |
Definition at line 1752 of file RiveScript.java.
BotReply com.rosalfred.core.ia.rivescript.RiveScript.processTags | ( | String | user, |
Client | profile, | ||
String | message, | ||
BotReply | botReply, | ||
ArrayList< String > | vstars, | ||
ArrayList< String > | vbotstars, | ||
int | step | ||
) | [inline, private] |
Process reply tags.
user | The name of the end user. |
profile | The RiveScript client object holding the user's profile |
message | The message sent by the user. |
reply | The bot's original reply including tags. |
stars | The vector of wildcards the user's message matched. |
botstars | The vector of wildcards in any Previous. |
step | The current recursion depth limit. |
Definition at line 1774 of file RiveScript.java.
BotReply com.rosalfred.core.ia.rivescript.RiveScript.reply | ( | String | username, |
String | message | ||
) | [inline] |
Get a reply from the RiveScript interpreter.
username | A unique user ID for the user chatting with the bot. |
message | The user's message to the bot. |
Definition at line 1102 of file RiveScript.java.
BotReply com.rosalfred.core.ia.rivescript.RiveScript.reply | ( | String | user, |
String | message, | ||
boolean | begin, | ||
int | step | ||
) | [inline, private] |
Internal method for getting a reply.
user | The username of the calling user. |
message | The (formatted!) message sent by the user. |
begin | Whether the context is that we're in the BEGIN statement or not. |
step | The recursion depth that we're at so far. |
Definition at line 1156 of file RiveScript.java.
void com.rosalfred.core.ia.rivescript.RiveScript.say | ( | String | line | ) | [inline, protected] |
Print a line of debug text to the terminal.
line | The line of text to print. |
Definition at line 2273 of file RiveScript.java.
boolean com.rosalfred.core.ia.rivescript.RiveScript.setGlobal | ( | String | name, |
String | value | ||
) | [inline] |
Set a global variable for the interpreter (equivalent to ! global). Set the value to null to delete the variable.
There are two special globals that require certain data types:
debug is boolean-like and its value must be a string value containing "true", "yes", "1", "false", "no" or "0".
depth is integer-like and its value must be a quoted integer like "50". The "depth" variable controls how many levels deep RiveScript will go when following reply redirections.
Returns true on success, false on error.
name | The variable name. |
value | The variable's value. |
Definition at line 430 of file RiveScript.java.
void com.rosalfred.core.ia.rivescript.RiveScript.setHandler | ( | String | name, |
ObjectHandler< BotReply > | handler | ||
) | [inline] |
Add a handler for a programming language to be used with RiveScript object calls.
name | The name of the programming language. |
handler | An instance of a class that implements an ObjectHandler. |
Definition at line 408 of file RiveScript.java.
boolean com.rosalfred.core.ia.rivescript.RiveScript.setPersonSubstitution | ( | String | pattern, |
String | output | ||
) | [inline] |
Set a person substitution pattern (equivalent to ! person). Person substitutions swap first- and second-person pronouns, so the bot can safely echo the user without sounding too mechanical.
A null value for the output will delete the substitution.
pattern | The pattern to match in the message. |
output | The text to replace it with (must be lowercase, no special characters). |
Definition at line 524 of file RiveScript.java.
boolean com.rosalfred.core.ia.rivescript.RiveScript.setSubstitution | ( | String | pattern, |
String | output | ||
) | [inline] |
Set a substitution pattern (equivalent to ! sub). The user's input (and the bot's reply, in Previous) get substituted using these rules.
A null value for the output will delete the substitution.
pattern | The pattern to match in the message. |
output | The text to replace it with (must be lowercase, no special characters). |
Definition at line 503 of file RiveScript.java.
boolean com.rosalfred.core.ia.rivescript.RiveScript.setUservar | ( | String | user, |
String | name, | ||
String | value | ||
) | [inline] |
Set a variable for one of the bot's users. A null value will delete a variable.
user | The user's ID. |
name | The name of the variable to set. |
value | The value to set. |
Definition at line 543 of file RiveScript.java.
boolean com.rosalfred.core.ia.rivescript.RiveScript.setUservars | ( | String | user, |
HashMap< String, String > | data | ||
) | [inline] |
Set -all- user vars for a user. This will replace the internal hash for the user. So your hash should at least contain a key/value pair for the user's current "topic". This could be useful if you used getUservars to store their entire profile somewhere and want to restore it later.
user | The user's ID. |
data | The full hash of the user's data. |
Definition at line 563 of file RiveScript.java.
void com.rosalfred.core.ia.rivescript.RiveScript.setUservars | ( | ClientManager | clients | ) | [inline] |
Definition at line 573 of file RiveScript.java.
boolean com.rosalfred.core.ia.rivescript.RiveScript.setVariable | ( | String | name, |
String | value | ||
) | [inline] |
Set a bot variable for the interpreter (equivalent to ! var). A bot variable is data about the chatbot, like its name or favorite color.
A null value will delete the variable.
name | The variable name. |
value | The variable's value. |
Definition at line 483 of file RiveScript.java.
void com.rosalfred.core.ia.rivescript.RiveScript.sortReplies | ( | ) | [inline] |
After loading replies into memory, call this method to (re)initialize internal sort buffers. This is necessary for accurate trigger matching.
Definition at line 1079 of file RiveScript.java.
boolean com.rosalfred.core.ia.rivescript.RiveScript.stream | ( | String | code | ) | [inline] |
Stream some RiveScript code directly into the interpreter (as a single string containing newlines in it).
code | A string containing all the RiveScript code. |
Definition at line 379 of file RiveScript.java.
boolean com.rosalfred.core.ia.rivescript.RiveScript.stream | ( | String[] | code | ) | [inline] |
Stream some RiveScript code directly into the interpreter (as a string array, one line per item).
code | A string array containing all the lines of code. |
Definition at line 393 of file RiveScript.java.
String com.rosalfred.core.ia.rivescript.RiveScript.stringTransform | ( | String | format, |
String | text | ||
) | [inline, private] |
Reformats a string in a certain way: formal, uppercase, lowercase, sentence.
format | The format you want the string in. |
text | The text to format. |
Definition at line 2113 of file RiveScript.java.
void com.rosalfred.core.ia.rivescript.RiveScript.trace | ( | IOException | e | ) | [inline, private] |
Print a stack trace to the terminal when debug mode is on.
e | The IOException object. |
Definition at line 2304 of file RiveScript.java.
String com.rosalfred.core.ia.rivescript.RiveScript.triggerRegexp | ( | String | user, |
Client | profile, | ||
String | trigger | ||
) | [inline, private] |
Formats a trigger for the regular expression engine.
user | The user ID of the caller. |
trigger | The raw trigger text. |
Definition at line 1611 of file RiveScript.java.
HashMap<String, Vector<String> > com.rosalfred.core.ia.rivescript.RiveScript.arrays [private] |
new HashMap<String, Vector<String>>()
Definition at line 117 of file RiveScript.java.
ClientManager com.rosalfred.core.ia.rivescript.RiveScript.clients = new ClientManager() [private] |
Definition at line 102 of file RiveScript.java.
final String com.rosalfred.core.ia.rivescript.RiveScript.CMD_CONDITION = "*" [static, private] |
Definition at line 94 of file RiveScript.java.
final String com.rosalfred.core.ia.rivescript.RiveScript.CMD_CONTINUE = "^" [static, private] |
Definition at line 92 of file RiveScript.java.
final String com.rosalfred.core.ia.rivescript.RiveScript.CMD_DEFINE = "!" [static, private] |
Definition at line 88 of file RiveScript.java.
final String com.rosalfred.core.ia.rivescript.RiveScript.CMD_ENDLABEL = "<" [static, private] |
Definition at line 96 of file RiveScript.java.
final String com.rosalfred.core.ia.rivescript.RiveScript.CMD_LABEL = ">" [static, private] |
Definition at line 95 of file RiveScript.java.
final String com.rosalfred.core.ia.rivescript.RiveScript.CMD_PREVIOUS = "%" [static, private] |
Definition at line 90 of file RiveScript.java.
final String com.rosalfred.core.ia.rivescript.RiveScript.CMD_REDIRECT = "@" [static, private] |
Definition at line 93 of file RiveScript.java.
final String com.rosalfred.core.ia.rivescript.RiveScript.CMD_REPLY = "-" [static, private] |
Definition at line 91 of file RiveScript.java.
final String com.rosalfred.core.ia.rivescript.RiveScript.CMD_TRIGGER = "+" [static, private] |
Definition at line 89 of file RiveScript.java.
String com.rosalfred.core.ia.rivescript.RiveScript.currentUser = null [private] |
Definition at line 129 of file RiveScript.java.
boolean com.rosalfred.core.ia.rivescript.RiveScript.debug = false [private] |
Definition at line 75 of file RiveScript.java.
int com.rosalfred.core.ia.rivescript.RiveScript.depth = 50 [private] |
Definition at line 76 of file RiveScript.java.
String com.rosalfred.core.ia.rivescript.RiveScript.error = "" [private] |
Definition at line 77 of file RiveScript.java.
HashMap<String, String> com.rosalfred.core.ia.rivescript.RiveScript.globals [private] |
new HashMap<String, String>()
Definition at line 113 of file RiveScript.java.
HashMap<String, ObjectHandler<BotReply> > com.rosalfred.core.ia.rivescript.RiveScript.handlers [private] |
new HashMap<String, ObjectHandler<BotReply>>()
Definition at line 105 of file RiveScript.java.
HashMap<String, String> com.rosalfred.core.ia.rivescript.RiveScript.objects [protected] |
new HashMap<String, String>()
Definition at line 107 of file RiveScript.java.
HashMap<String, String> com.rosalfred.core.ia.rivescript.RiveScript.person [private] |
new HashMap<String, String>()
Definition at line 123 of file RiveScript.java.
String [] com.rosalfred.core.ia.rivescript.RiveScript.person_s [private] |
null
Definition at line 125 of file RiveScript.java.
Random com.rosalfred.core.ia.rivescript.RiveScript.rand = new Random() [static, private] |
Definition at line 78 of file RiveScript.java.
final double com.rosalfred.core.ia.rivescript.RiveScript.RS_VERSION = 2.0 [static, private] |
Definition at line 87 of file RiveScript.java.
HashMap<String, String> com.rosalfred.core.ia.rivescript.RiveScript.subs [private] |
new HashMap<String, String>()
Definition at line 119 of file RiveScript.java.
String [] com.rosalfred.core.ia.rivescript.RiveScript.subs_s [private] |
null
Definition at line 121 of file RiveScript.java.
TopicManager com.rosalfred.core.ia.rivescript.RiveScript.topics = new TopicManager() [private] |
Definition at line 99 of file RiveScript.java.
final String com.rosalfred.core.ia.rivescript.RiveScript.TYPE_JAVA = "java" [static] |
Definition at line 72 of file RiveScript.java.
final String com.rosalfred.core.ia.rivescript.RiveScript.TYPE_PERL = "perl" [static] |
Definition at line 71 of file RiveScript.java.
final String com.rosalfred.core.ia.rivescript.RiveScript.TYPE_PYTHON = "python" [static] |
Definition at line 70 of file RiveScript.java.
HashMap<String, String> com.rosalfred.core.ia.rivescript.RiveScript.vars [private] |
new HashMap<String, String>()
Definition at line 115 of file RiveScript.java.
final double com.rosalfred.core.ia.rivescript.RiveScript.VERSION = 0.02 [static] |
The version of the RiveScript Java library.
Definition at line 84 of file RiveScript.java.