Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从数组中抓取单词_Java_Arrays_Artificial Intelligence - Fatal编程技术网

Java 从数组中抓取单词

Java 从数组中抓取单词,java,arrays,artificial-intelligence,Java,Arrays,Artificial Intelligence,我非常不确定如何做到这一点。我知道我相信你用的是 String[] user {"stuff","other stuff","more stuff"}; 我正在开发一个聊天机器人,我需要它能够识别用户说了什么,如果它在阵列中,它的数据库中,那么它将相应地响应 一些简单的事情让我可以说,你好吗?它会看你怎么样?或者至少与之相近的事物,并相应地用一个随机的肯定词来回应。我通过简单地使用大量if-else语句实现了这个函数,但是这太多了 如果我理解正确,您希望您的机器人对用户的提示做出响应。在这种情

我非常不确定如何做到这一点。我知道我相信你用的是

String[] user {"stuff","other stuff","more stuff"};
我正在开发一个聊天机器人,我需要它能够识别用户说了什么,如果它在阵列中,它的数据库中,那么它将相应地响应


一些简单的事情让我可以说,你好吗?它会看你怎么样?或者至少与之相近的事物,并相应地用一个随机的肯定词来回应。我通过简单地使用大量if-else语句实现了这个函数,但是这太多了

如果我理解正确,您希望您的机器人对用户的提示做出响应。在这种情况下,您可以使用映射来存储查询-答案对

Map<String, String> answers = new HashMap<String, String>();

answers.put("How are you?", "Great!");
answers.put("Where is the cake?", "The cake is a lie");
如果您想要多个可能的答案,请使用地图并从列表中随机选择:

public String answerUser(String query) {
    Random rand = new Random();

    if (answers.containsKey(query)) {
        List<String> ans = answers.get(query);
        int id = rand.nextInt(ans.size());
        return ans.get(id);
    } else {
        return "I don't understand.";
    }
}

这是一个非常大和复杂的主题,你还没有发布足够的信息继续下去

要将字符串拆分为单词,请使用Stringsplit,这将为您提供所需的数组。您可能希望在所有非alpha或所有空格上拆分

然后,您需要定义AI响应的关键字,并扫描数组中的任何关键字

使用某种评分系统,然后确定适当的响应

例如,您可以将字符串的哈希映射到一个既有权重又有意义的类。通读句子,总结你发现的每个动作的得分。根据组合值做出适当的决策


这是一个非常简单的算法,可能会有更好的算法,但它们要困难得多,这将帮助您开始。

您可以使用列表而不是数组,这将为您提供contains方法。例如:

List<String> words = new ArrayList<String>();
words.add("Hello");
words.add("bye");
if(words.contains("hello")) {
    //do something 
}
另一种选择是将短语映射到响应:

Map<String, String> wordMap = new HashMap<String, String>();
wordMap.put("How are you?", "Great");
wordMap.put("Where are you?", "Work");
wordMap.get("How are you?");

如果您愿意,可以将这些内容组合起来,并将一个短语映射到一个响应列表。

智能聊天机器人需要更多的设计,但如果您只是想找到一个解决方案,而我就是这么说的,那么您可以通过多种方式来实现

使用两个数组

既然你知道如何使用一个数组,我想我从这里开始简单一点

String[] userInput {"how are you", "how are you?", "how you doing?"};
String[] response {"good", "terrible", "is that a joke?"};

//Go through each userInput string and see if it matches what you typed in.
    //If it matches, print the corresponding position in the response array.
使用地图

同样的想法,但更适合这种情况

Map<String, String> response = new HashMap<String, String>();
response.add("how are you", "good");

//When you receive input, check the response map using the input as the key.
//Return the value as the response.
//Better documented in sebii's answer.

我认为您在这里寻找的是自然语言处理,这在堆栈上的线程溢出中无法真正解释。我想你可以用正则表达式得到一些简单的东西。这也是一个很大的问题。试着用谷歌搜索java中的正则表达式来开始感谢你,所有这些。加权响应似乎是一个非常好的主意,我已经有了这样的想法,尽管我无法正确地将我的想法集中在一起,所以感谢您添加模拟情绪,根据设置来衡量用户输入,以确定AI的感觉。
Map<String, String> response = new HashMap<String, String>();
response.add("how are you", "good");

//When you receive input, check the response map using the input as the key.
//Return the value as the response.
//Better documented in sebii's answer.