gpt4 book ai didi

java - 聊天机器人,最后一部分

转载 作者:行者123 更新时间:2023-12-01 13:44:23 25 4
gpt4 key购买 nike

ChatBot 中仅剩下最后一部分。我需要找到一种修改聊天机器人类的方法,所以它偶尔(比如 30% 的时间)会返回一个随机生成的标准回复,以响应用户输入的至少五个可能回复之一,例如“LOL”、“OMG”、“You don't say”、“Really” ?”,或“我明白了”。

编辑:应用建议的更改:

import java.util.Random;
import java.util.Scanner;
public class ChatBot
{
private int responseCount = 0;
public String getResponse(String value)
{
String X = longestWord(value);
this.responseCount++;
if (responseCount == 10)
{
return "Sorry, but our time is up. I can't talk with you any longer.";
}
if (value.contains("you"))
{
return "I'm not important. Let's talk about you instead.";
}


else if (X.length() <= 3)
{
return "Maybe we should move on. Is there anything else you would like to talk about?";
}
else if (X.length() == 4)
{
return "Tell me more about " + X;
}

else if (X.length() == 5)
{
return "Why do you think " + X + " is important?";
}
else if (X.length() <=9)
{
return "Now we are getting somewhere. How does " + X + " affect you the most?";
}

return getRandomResponse();
}


public String longestWord(String value){
Scanner input = new Scanner (value);
String longest = new String();
longest = "";

while (input.hasNext())
{
String temp = input.next();
if(temp.length() > longest.length())
{
longest = temp;
}
}
return longest;
}

private String getRandomResponse()
{

String [] responses = {"OMG", "LOL", "You don't say", "Really?", "I See"};

return responses [(int)(Math.random() * responses.length)];
}
}

问题是,它不断返回相同的响应,而不是给出的五个响应之一。任何帮助我将不胜感激,谢谢!

编辑:它现在只给出随机响应,并覆盖 getResponse() 方法中的所有其他响应。

最佳答案

根据您的逻辑,您的 getRandomResponse 方法应始终返回“OMG”。这是因为在该方法中第一次运行循环时,counter = 1。因此第一个 if 语句将运行并返回“OMG”退出该方法。更好的等效方法可能会将所有响应放入一个数组中并从中返回一个随机值,而不是通过迭代做一些奇怪的事情:

String[] responses = {"OMG", "LOL", "You don't say", "Really?", "I See"};
return responses[(int)(Math.random() * responses.length)];

关于java - 聊天机器人,最后一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20459154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com