0

I have this matrix for a chat bot I am making in Java as a knowledge base of responses:

    String[][] knowledgeBase={
    {"hi","hello","howdy","hey"},//input 1; if you user inputs any of these,
    {"hi","hello","hey"},//output 1; randomly choose between these as a response
    {"how are you", "how r u", "how r you", "how are u"},//input 2; if you user inputs any of these,
    {"good","doing well"},//output 2; randomly choose between these as a response
    {"shut up","i dont care","stop talking"}//if input was in neither array, use one of these as a response

This is working fine when I have the matrix inside the java file. I made a JSON file (I am new to JSON so not very sure if I got the format right) that is similar to the matrix:

{
    "0":{
        "input":[""]
        "output":["shut up","i dont care","stop talking"]
    }
    "1":{ 
        "input":["hi","hello","howdy","hey"]
        "output":["hi","hello","hey"]
    }
    "2":{
        "input":["how are you", "how r u", "how r you", "how are u"]
        "output":["good","doing well"]
    }
}

This is my code for going through the matrix looking for an exact match of the input:

public void actionPerformed(ActionEvent e){
    //get the user input
    String quote=input.getText();
    input.setText("");
    if(!quote.equals("")){
        addText("You:\t"+quote);
        quote.trim();
        while(quote.charAt(quote.length()-1)=='!' || quote.charAt(quote.length()-1)=='.' || quote.charAt(quote.length()-1)=='?'){
            quote=quote.substring(0,quote.length()-1);
        }
        quote.trim();
        byte response=0;
        int j=0;
        //check the knowledgeBase for a match or change topic
        while(response==0){
            //if a match is found, reply with the answer
            if(inArray(quote.toLowerCase(),knowledgeBase[j*2])){
                response=2;
                int r=(int)Math.floor(Math.random()*knowledgeBase[(j*2)+1].length);
                addText("\nPollockoraptor:\t"+knowledgeBase[(j*2)+1][r]);
            }
            j++;
            //if a match is not found, go to change topic
            if(j*2==knowledgeBase.length-1 && response==0){
                response=1;
            }
        }
        //change topic if bot is lost
        if(response==1){
            int r=(int)Math.floor(Math.random()*knowledgeBase[knowledgeBase.length-1].length);
            addText("\nPollockoraptor:\t"+knowledgeBase[knowledgeBase.length-1][r]);
        }
        addText("\n");
    }
}
public boolean inArray(String in,String[] str){
    boolean match=false;
    //look for an exact match
    for(int i=0;i<str.length;i++){
        if(str[i].equals(in)){
            match=true;
        }
    }
    return match;
}

How do I search through the JSON file with similar functionality? Can I also make numbers in the JSON files integers and not strings? Sorry if this is a very obvious question... I spent the past hour trying to figure out how to do this. Thank you for your help :)

  • See if [this post](http://stackoverflow.com/questions/18983185/how-to-create-correct-jsonarray-in-java-using-jsonobject) helps any. – PM 77-1 Oct 07 '14 at 05:07

2 Answers2

0

You can use numbers and String in JSON. But for your case, the outer type should be array and not object:

[
    {
        "input":[""],
        "output":["shut up","i dont care","stop talking"]
    },
    { 
        "input":["hi","hello","howdy","hey"],
        "output":["hi","hello","hey"]
    },
    {
        "input":["how are you", "how r u", "how r you", "how are u"],
        "output":["good","doing well"]
    }
]

That said, JSON isn't a good format to search. It's meant as a simple way to exchange data between programs. You want a "database" instead.

So your approach to read the texts into an "in-memory database" (or data structure) works well unless you have really many texts. When that happens, you should try a simple database like H2. It even supports fulltext search.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • Thank you I will make it into an array. I am using a JSON file because I dont have to install anything to access it. Some people recommended me to use a JSON or a properties file. –  Oct 07 '14 at 13:30
0

First of all I would suggest that you use Jackson to manage your JSON data. It provides ways to serialize and deserialize easily using Java objects. It is pretty stable and I have used it quite a bit. Secondly there is an interesting project called json-path which provides a way to query. A combination of the two would work very well!

For the integer question, the JSON value within quotes is treated as a string. "name":"arun" and for numbers just use "age":30 and boolean is also pretty simple with just "bool":true.