java - 高效搜索 JSON 文件

标签 java json input matrix io

我有一个用 Java 制作的聊天机器人矩阵,作为响应的知识库:

    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 文件中的数字设为整数而不是字符串吗?抱歉,如果这是一个非常明显的问题......我花了一个小时试图弄清楚如何做到这一点。谢谢您的帮助:)

最佳答案

您可以在 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 .

关于java - 高效搜索 JSON 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26228775/

相关文章:

java - Gradle 在依赖解析期间更改分类器

java - java 是否包含名为 "Item"的数据类型?

java - Android客户端PC服务器java套接字连接失败

javascript - 如何为 highcharts 创建 Json 格式的数据

javascript - 在列表中呈现的主干嵌套 json 对象

javascript - 显示来自输入标签的图像,无法检索 src 中的数据

java - 使用 hibernate 对 pojos 进行逆向工程的最佳方法

javascript - Express JS 以 JSON 格式显示所有事件

java - 字符串内容和 user_input 不允许我输入多个单词

HTML 5 功能检测 - 输入字段上的必需属性 - 它是否正常降级?