forked from aichaos/rivescript-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRSBot.java
84 lines (76 loc) · 2.39 KB
/
RSBot.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.lang.String;
import com.rivescript.RiveScript;
public class RSBot {
public static void main (String[] args) {
// Print a fancy banner.
System.out.println(""
+ " . . \n"
+ " .:...:: RiveScript Java // RSBot\n"
+ " .:: ::. Version: " + com.rivescript.RiveScript.VERSION + "\n"
+ " ..:;;. ' .;;:.. \n"
+ " . ''' . Type '/quit' to quit.\n"
+ " :;,:,;: Type '/help' for more options.\n"
+ " : : \n"
);
// Let the user specify debug mode!
boolean debug = false;
for (int i = 0; i < args.length; i++) {
if (args[i].equals("--debug") || args[i].equals("-d")) {
debug = true;
}
}
// Create a new RiveScript interpreter.
System.out.println(":: Creating RS Object");
RiveScript rs = new RiveScript(debug);
// Create a handler for Perl as an object macro language.
rs.setHandler("perl", new com.rivescript.lang.Perl(rs, "./lang/rsp4j.pl"));
// Define an object macro in Java.
rs.setSubroutine("javatest", new ExampleMacro());
// Load and sort replies
System.out.println(":: Loading replies");
rs.loadDirectory("./Aiden");
rs.sortReplies();
// Enter the main loop.
InputStreamReader converter = new InputStreamReader(System.in);
BufferedReader stdin = new BufferedReader(converter);
while (true) {
System.out.print("You> ");
String message = "";
try {
message = stdin.readLine();
}
catch (IOException e) {
System.err.println("Read error!");
}
// Quitting?
if (message.equals("/quit")) {
System.exit(0);
}
else if (message.equals("/dump topics")) {
rs.dumpTopics();
}
else if (message.equals("/dump sorted")) {
rs.dumpSorted();
}
else if (message.equals("/last")) {
System.out.println("You last matched: "
+ rs.lastMatch("localuser"));
}
else if (message.equals("/help")) {
System.out.println("Available commands:\n"
+ " /last Print the last matched trigger.\n"
+ " /dump topics Pretty-print the topic structure.\n"
+ " /dump sorted Pretty-print the sorted trigger structure.\n"
+ " /help Show this message.\n"
+ " /quit Exit the program.\n");
}
else {
String reply = rs.reply("localuser", message);
System.out.println("Bot> " + reply);
}
}
}
}