Time: 1287715063 Author: kirsle Subject: Java RiveScript Beta Avatar: RiveScript-Hazard.png Categories: Java Privacy: public IP: 76.168.205.25 NoEmote: 0 NoReply: 0 I started over from scratch on the RiveScript Java library because I didn't like my old code I had written anymore, and now after a couple weeks of work I'm calling the new library "beta"!

It has a Google Code page (won't be using svn.kirsle.net anymore) available at the following URL:

http://code.google.com/p/rivescript-java/

The library is feature-complete, meaning it supports everything laid out in the Working Draft, but it doesn't support object macros yet (because Java isn't a dynamic language and can't dynamically evaluate Java code). Object macros will be coming in a later update - probably using the Rhino JavaScript library and maybe even a micro Perl interpreter. ;-)

All the important stuff works though: all the directives, tags, user vars, bot vars, %Previous, topics (and inheritance and includes), etc.

I haven't tested it extensively yet and there may still be ways to break it, so if you find a way to break it let me know and I'll fix it. :-)

You can download the code from the Google Code page, or check it out via Subversion:

svn checkout http://rivescript-java.googlecode.com/svn/trunk/ rivescript-java-read-only

Library usage looks quite similar to the Perl version:

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) {
    // 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);

    // 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 {
        String reply = rs.reply("localuser", message);
        System.out.println("Bot> " + reply);
      }
    }
  }
}