2014-10-07 98 views
0

我有這個矩陣聊天機器人,我用Java製作爲響應的知識庫:通過JSON文件搜索有效

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 

這是工作正常,當我有java文件裏面的基質。我做了一個JSON文件(我是新來的JSON所以不是很肯定,如果我得到了格式正確的),它類似於矩陣:

{ 
    "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"] 
    } 
} 

這是我經歷的矩陣尋找一個精確匹配碼的輸入:

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; 
} 

如何通過具有類似功能的JSON文件進行搜索?我也可以在JSON文件整數而不是字符串中編號嗎?對不起,如果這是一個非常明顯的問題...我花了過去一小時試圖找出如何做到這一點。感謝您的幫助:)

+0

看看[這篇文章](http://stackoverflow.com/questions/18983185/how-to-create-correct-jsonarray-in-java-using-jsonobject)幫助任何。 – 2014-10-07 05:07:13

回答

1

您可以在JSON中使用數字和字符串。但是,對於你的情況下,外部類型應該是array,而不是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"] 
    } 
] 

也就是說,JSON是不是搜索一個很好的格式。這意味着在程序之間交換數據的一種簡單方法。你需要一個「數據庫」。

因此,將文本讀入「內存數據庫」(或數據結構)的方法運行良好,除非您有很多文本。當發生這種情況時,你應該嘗試一個簡單的數據庫,如H2。它甚至支持fulltext search

+0

謝謝我將把它變成一個數組。我正在使用JSON文件,因爲我不需要安裝任何東西來訪問它。有人建議我使用JSON或屬性文件。 – 2014-10-07 13:30:01

0

首先,我建議您使用Jackson來管理您的JSON數據。它提供了使用Java對象輕鬆序列化和反序列化的方法。它非常穩定,我已經用了很多。 其次有一個叫做json-path的有趣項目提供了一種查詢方式。兩者的結合將會非常好!

對於整數問題,引號內的JSON值被視爲字符串。 「name」:「arun」,而數字只是使用「age」:30,布爾值也很簡單,只有「bool」:true。