1

I am making an auto chat client like Cleverbot for school. I have everything working, but I need a way to make a knowledge base of responses. I was going to make a matrix with all the responses that I need the bot to say, but I think it would be hard to edit the code every time I want to add a responses to the bot. This is the code that I have for the knowledge base matrix:

    `String[][] Database={
    {"hi","hello","howdy","hey"},//possible user input
    {"hi","hello","hey"},//response
    {"how are you", "how r u", "how r you", "how are u"},
    {"good","doing well"}`

How would I make a matrix like this from a text file? Is there a better way than reading from a text file to deal with this?

  • Have a look at [the Java file IO tutorial](http://docs.oracle.com/javase/tutorial/essential/io/fileio.html) – eebbesen Oct 03 '14 at 00:12
  • The structure you're looking for is a many-to-many map or something along those lines, where a `List` of keys points to a `List` of values. A matrix implies fixed size (a 2x3 matrix or 4x3 matrix or so on). – ssube Oct 03 '14 at 00:16

2 Answers2

6

You could...

Use a properties file

The properties file is something that can easily be read into (and stored from, but you're not interested in that) Java. The class java.util.Properties can make that easier, but it's fairly simple to load it and then you access it like a Map.

hello.input=hi,hello,howdy,hey
hello.output=hi,hello,hey

Note the matching formats there. This has its own set of problems and challenges to work with, but it lets you easily pull things in to and out of properties files.

Store it in JSON

Lots of things use JSON for a serialization format. And thus, there are lots of libraries that you can use to read and store from it. It would again make some things easier and have its own set of challenges.

{
    "greeting":{ 
        "input":["hi","hello","howdy","hey"],
        "output":["hi","hello","hey"]
    }
}

Something like that. And then again, you read this and store it into your data structures. You could store JSON in a number of places such as document databases (like couch) which would make for easy updates, changes, and access... given you're running that database.

Which brings us to...

Embedded databases

There are lots of databases that you can stick right in your application and access it like a database. Nice queries, proper relationships between objects. There are lots of advantages to using a database when you actually want a database rather than hobbling strings together and doing all the work yourself.

Custom serialization

You could create a class (instead of a 2d array) and then store the data in a class (in which it might be a 2d array, but that's an implementation detail). At this point, you could implement Serializable and write the writeObject and readObject methods and store the data somehow in a file which you could then read back into the object directly. If you have the administration ability of adding new things as part of this application (or another that uses the same class) you could forgo the human readable aspect of it and use the admin tool (that you write) to update the object.

Lots of others

This is just the tip of the iceberg. There are lots of ways to go about this.

P.S.

Please change the name of the variable from Database to something in lower case that better describes it such as input2output or the like. Upper case names are typically reserved for class names (unless its all upper case, in which case it's a final static field)

Community
  • 1
  • 1
  • 1
    If you want a simple way to use properties or JSON, check out the [typesafe config](https://github.com/typesafehub/config) library. It can read both and a slightly enhanced ".conf" format, and is great for read-only, thread-safe applications. – ssube Oct 03 '14 at 00:43
1

A common solution would be to dump the data in to a properties file, and then load it with the standard Properties.load(...) method.

Once you have your data like that, you can then access the data by a map-like interface.

You could find different ways of storing the data in the file like:

userinput=hi,hello,howdy,hey
response=hi,hello,hey
...

Then, when you read the file, you can split the values on the comma:

String[] expectHello = properties.getProperty("userinput").split(",");
rolfl
  • 17,539
  • 7
  • 42
  • 76