AI Zone Admin Forum Add your forum

NEWS: Chatbots.org survey on 3000 US and UK consumers shows it is time for chatbot integration in customer service!read more..

RiveScript in Java
 
 

Hello,

I recently started learning RiveScript to program a chatbot for a project in school. Since I have no experience with Perl, I want to use the Java libraries that are listed on the rivescript website. I’m quite fluent in Java and know how to integrate libs, but I have no idea how I can use my RiveScript code e.g. in Eclipse.
I already started to create the brain of my bot and wrote a few files in RiveScript, using the Tutorial on the website.
I know how to execute the RSBot which comes shipped with the java libraries, but I don’t know how to create an Interpreter in java.

If anyone knows a good Tutorial or is able to explain it to me himself, I would be really glad.

Thanks in advance

 

 
  [ # 1 ]

there are javascript libraries and they have a demo shell.js - its probably the simplest way to get going and learning JS is not a bad thing in this age…

 

 
  [ # 2 ]

I’m not a very strong Java developer (though I wrote RiveScript-Java), and I don’t know much about Eclipse, but the source of RSBot.java might help?

I’d just make sure the com/rivescript/ package is on my classpath somewhere, and use its API from my own Java code in a way similar to RSBot.java, like

import com.rivescript.RiveScript;

public class 
MyRobot {
 
public static void main (String[] args{
  
// Create a new RiveScript interpreter.
  
RiveScript rs = new RiveScript();

  
// Load and sort replies
  
rs.loadDirectory("./Aiden");
  
rs.sortReplies();

  
// Get a reply from the bot.
  
String reply rs.reply("user""hello bot");
  
System.out.println("Bot: " reply);
 
}

For a real bot what you’d probably do is not deal with RiveScript in your main class but put it in another class file (like Brain.java or something), and have the RiveScript object be a class attribute so it can be loaded into memory once when your bot starts up and then be called on to get a reply. Might look like,

import com.rivescript.RiveScript;

public class 
Brain {
 
private RiveScript rs// class attribute to hold the RiveScript instance

 /**
  * Initialize the RiveScript brain by loading its replies from `path`.
  */
 
public void setUp(String path{
  this
.rs = new RiveScript();
  
this.rs.loadDirectory(path);
  
this.rs.sortReplies();
 
}

 
/**
  * Fetch a reply from the bot.
  */
 
public String reply(String userString message{
  
// Get a reply from the bot.
  
String reply rs.reply(usermessage);
  return 
reply;
 
}

Your main code could then import your Brain.java file and call its `setUp()` method with the path to replies on disk (”./Aiden” or w/e), and its `reply()` method to get a reply. You could extend that with other useful methods, such as one to expose the underlying RiveScript object in case your program needs it for any special cases (getting/setting a user variable for example).

(Disclaimer: I haven’t tested this code so it might not compile as-is, but it should be enough to get you started).

 

 
  login or register to react