【问题标题】:Grab words out of arrays从数组中抓取单词
【发布时间】:2013-12-20 22:23:11
【问题描述】:

我非常不确定如何做到这一点。我了解(我相信)您使用

String[] user {"stuff","other stuff","more stuff"};

我正在开发一个聊天机器人,我需要它能够识别用户所说的内容以及它是否在数组内部(它是“数据库”),然后它会做出相应的响应。

一些简单的事情,这样我就可以说,“你好吗?”它会寻找“你好吗?”或至少接近它的东西,并相应地用一个随机的肯定词做出回应。我已经通过简单地使用大量 if-else 语句来实现此功能,但这是太多的编码。

【问题讨论】:

  • 我认为您在这里寻找的是自然语言处理,并且无法在堆栈溢出的线程中真正解释。我冒昧地猜测你可以用正则表达式得到一些简单的东西,正则表达式。这又是一个很大的话题。尝试谷歌搜索“java中的正则表达式”开始

标签: java arrays artificial-intelligence


【解决方案1】:

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

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) {
    if (answers.containsKey(query)) {
        return answers.get(query);
    } else {
        return "I don't understand.";
    }
}

如果您想要多个可能的答案,请使用Map&lt;String, List&lt;String&gt;&gt; 并从列表中随机选择:

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

【讨论】:

    【解决方案2】:

    这是一个非常大而复杂的主题,您还没有真正发布足够的内容。

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

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

    使用某种评分系统来确定适当的响应。

    例如,您可以将 String 的 HashMap 用于具有权重和含义的类。通读句子,总结你找到的每一个动作的分数。根据综合价值做出适当的决定。

    这是一个非常简单的算法,并且可能有更好的算法,但它们要困难得多,这会让你开始。

    【讨论】:

    • 感谢所有这些。加权响应似乎是一个非常好的主意,我已经有了类似的想法(尽管我无法正确地将我的想法放在一起,所以谢谢你)添加模拟情绪,权衡用户输入设置是什么来确定人工智能的“感觉”。
    【解决方案3】:

    您可以使用列表而不是数组,这将为您提供 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?");
    

    如果需要,可以组合这些并将短语映射到响应列表。

    【讨论】:

      【解决方案4】:

      智能聊天机器人需要更多设计,但如果您只是想要一个解决方案,“我说这个,然后你说那个”,你可以通过几种方式做到这一点。 p>

      使用两个数组

      由于您知道如何使用一个数组,所以为了简单起见,我想从这里开始。

      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.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-20
        相关资源
        最近更新 更多