Sunday 15 April 2012

java - Searching through JSON file efficiently -



java - Searching through JSON file efficiently -

i have matrix chat bot making in java knowledge base of operations of responses:

string[][] knowledgebase={ {"hi","hello","howdy","hey"},//input 1; if user inputs of these, {"hi","hello","hey"},//output 1; randomly take between these response {"how you", "how r u", "how r you", "how u"},//input 2; if user inputs of these, {"good","doing well"},//output 2; randomly take between these response {"shut up","i dont care","stop talking"}//if input in neither array, utilize 1 of these response

this working fine when have matrix within java file. made json file (i new json not sure if got format right) similar matrix:

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

this code going through matrix looking exact match of input:

public void actionperformed(actionevent e){ //get 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 knowledgebase match or alter topic while(response==0){ //if match found, reply reply 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 match not found, go alter topic if(j*2==knowledgebase.length-1 && response==0){ response=1; } } //change topic if bot 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 exact match for(int i=0;i<str.length;i++){ if(str[i].equals(in)){ match=true; } } homecoming match; }

how search through json file similar functionality? can create numbers in json files integers , not strings? sorry if obvious question... spent past hr trying figure out how this. give thanks help :)

you can utilize numbers , string in json. case, outer type should array , not object:

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

that said, json isn't format search. it's meant simple way exchange info between programs. want "database" instead.

so approach read texts "in-memory database" (or info structure) works unless have many texts. when happens, should seek simple database h2. supports fulltext search.

java json input matrix io

No comments:

Post a Comment