Artificial Intelligence Scripting Language

Category: Java

RiveScript-Python Released

Posted by Kirsle on Friday, Apr 20 2012 @ 4:25 PM
I've released the Python version of RiveScript!

Get it at GitHub!

It should be as feature-complete as the Perl version, though obviously it hasn't been tested for quite as many years yet. ;) It will let you write RiveScript objects using Python code, and there's an included example for how you would set up a JavaScript handler too.

The module, rivescript.py can also be executed as a stand-alone Python script. In this case, it will behave exactly like the rivescript command that ships with the Perl version: you'll be able to interactively test out your RiveScript bot. It also supports JSON Mode, so a C++ program for example could still use rivescript.py to be able to get responses from your bot.

The bugs I found in the Perl version while writing this have also been fixed. The Perl version has also moved to github: rivescript-perl. Furthermore, the Java version of RiveScript is on github now too: rivescript-java.

Categories: Python , Perl , Java

[ 0 comments | Add comment | Permalink ]

Perl for RiveScript-Java!

Posted by Kirsle on Monday, Oct 25 2010 @ 12:19 PM
I've just checked in an update to the RiveScript-java code:

It supports Perl objects now!

So you can use your existing RiveScript Perl objects with the Java library now. Example:

+ encode * in md5
- "<star> in MD5 is: <call>md5 <star></call>

> object md5 perl
   my $rs = shift;

   use Digest::MD5 qw(md5_hex);
   return md5_hex(join(" ", @_));
< object

How does it work? There's a Perl script names "rsp4j.pl" that the Java library calls on. Java sends a JSON-encoded data structure to the Perl script containing the user's ID, message, user variables, and the source code to the Perl object. The Perl script loads the RiveScript Perl module and uses it to evaluate a response to the user. It then outputs a new JSON structure containing the user's reply and their new user variables (in case the Perl object called any setUservars methods on the $rs object).

The 0.02 version of rivescript-java has the Perl support. You can see its usage in the RSBot.java example script, or more generally here's all you need to do to get Perl support:

RiveScript rs = new RiveScript();
rs.setHandler("perl", new com.rivescript.lang.Perl(rs, "/path/to/rsp4j.pl");

Categories: General , Perl , Java

[ 0 comments | Add comment | Permalink ]

Java RiveScript Beta

Posted by Kirsle on Thursday, Oct 21 2010 @ 7:37 PM
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);
      }
    }
  }
}

Categories: Java

[ 0 comments | Add comment | Permalink ]