Java 聊天机器人镜像和预设回复
Java chatbot mirroring and canned responses
所以我得到了我正在开发的 java 聊天机器人程序。
根据用户所说的内容,如果用户的回复包含关键字之一 I love pizza
--> you love pizza
,聊天机器人会给出预设回复 canned_phrases
或镜像回复.
问题是 聊天机器人没有返回镜像版本。我认为问题在于单词被彼此覆盖,但我不确定如何解决这个问题。
感谢您的帮助!
import java.util.Random;
import java.util.Scanner;
public class Conversation {
public static void main(String[] args) {
String[] canned_phrases = {"Yes",
"Hmmm. Please tell me more news",
"Of course",
"Okay",
"Interesting...",
"Indeed"};
int canned_times = canned_phrases.length;
Scanner conversation_start = new Scanner(System.in);
System.out.println("\nWelcome!\n");
System.out.println("How many rounds of conversation would you like to have?\n");
int rounds = conversation_start.nextInt();
conversation_start.nextLine();
String[] transcript = new String[2 * rounds + 1];
transcript[0] = "Sounds great! How are you doing today?";
System.out.println(transcript[0]);
for (int i = 0; i < rounds; i++) {
String user_words = conversation_start.nextLine();
String mirrored;
String new_version = user_words.replace("I", "you");
new_version = new_version.replace("me", "you");
new_version = new_version.replace("am", "are");
new_version = new_version.replace("you", "I");
new_version = new_version.replace("my", "your");
new_version = new_version.replace("your", "my");
new_version = new_version.replace("my", "you");
if (!new_version.equals(user_words)) {
mirrored = new_version;
}
else {
mirrored = canned_phrases[(int) Math.floor(canned_times * Math.random())];
}
System.out.println(mirrored);
transcript[2 * i + 1] = user_words;
transcript[2 * i + 1] = mirrored;
}
System.out.println("Thank you for chatting with me! Come back soon!");
System.out.println(" ");
System.out.println("TRANSCRIPT ");
for (int i = 0; i <= transcript.length; i++) {
System.out.println(transcript[i]);
}
System.exit(0);
}
}
我对您的代码稍作修改,现在它可以按您的预期运行:
public static void main(String[] args) {
String[] canned_phrases = {"Yes", "Hmmm. Please tell me more news", "Of course", "Okay", "Interesting...", "Indeed"};
int canned_times = canned_phrases.length;
Scanner conversation_start = new Scanner(System.in);
System.out.println("\nWelcome!\n");
System.out.println("How many rounds of conversation would you like to have?\n");
int rounds = conversation_start.nextInt();
conversation_start.nextLine();
String[] transcript = new String[2*rounds+1];
transcript[0] = "Sounds great! How are you doing today?";
System.out.println(transcript[0]);
for (int i = 0; i < rounds; i++) {
String user_words = conversation_start.nextLine();
String mirrored;
String new_version = user_words.replace("I", "you");
new_version = new_version.replace("me","you");
new_version = new_version.replace("am","are");
//1st change as you replaced it above so not swap it again
//new_version = new_version.replace("you","I");
new_version = new_version.replace("my","your");
new_version = new_version.replace("your","my");
new_version = new_version.replace("my","you");
//by commenting the line above, will enter the IF-block
if (!new_version.equals(user_words)) {
mirrored = new_version;
} else {
mirrored = canned_phrases[(int) Math.floor(canned_times * Math.random())];
}
System.out.println(mirrored);
transcript[2*i+1] = user_words;
//2nd change to not overwrite the same index i added 2 instead of 1
transcript[2*i+2] = mirrored;
}
System.out.println("Thank you for chatting with me! Come back soon!");
System.out.println(" ");
System.out.println("TRANSCRIPT ");
//3rd change i removed the = from the loop condition to prevent exception appeared
for (int i = 0; i < transcript.length; i++) {
System.out.println(transcript[i]);
}
System.exit(0);
}
我认为实现你想要的最好方法是将 user-entered 句子拆分成单词并根据你的规则更改单个单词,例如如果单词是 my 然后将其更改为 你的 。在下面的代码中,我按顺序遍历所有单词,根据需要更改每个单词并将它们附加到 StringBuilder
以便在遍历所有单词后,StringBuilder
包含镜像句子。
(代码后有更多注释。)
import java.util.Random;
import java.util.Scanner;
public class Conversation {
public static void main(String[] args) {
String[] cannedPhrases = {"Yes",
"Hmmm. Please tell me more news",
"Of course",
"Okay",
"Interesting...",
"Indeed"};
int cannedTimes = cannedPhrases.length;
Random rand = new Random();
Scanner conversationStart = new Scanner(System.in);
System.out.println("\nWelcome!\n");
System.out.println("How many rounds of conversation would you like to have?\n");
int rounds = conversationStart.nextInt();
conversationStart.nextLine();
String[] transcript = new String[2 * rounds];
transcript[0] = "Sounds great! How are you doing today?";
System.out.println(transcript[0]);
int index = -1;
for (int i = 0; i < rounds; i++) {
String userWords = conversationStart.nextLine();
String mirrored;
StringBuilder result = new StringBuilder();
String[] words = userWords.split(" ");
boolean first = true;
for (String word : words) {
if (first) {
first = false;
}
else {
result.append(' ');
}
switch (word) {
case "I":
word = "you";
break;
case "me":
word = "you";
break;
case "am":
word = "are";
break;
case "you":
word = "I";
break;
case "my":
word = "your";
break;
case "your":
word = "my";
break;
}
result.append(word);
}
String newVersion = result.toString();
if (!newVersion.equals(userWords)) {
mirrored = newVersion;
}
else {
mirrored = cannedPhrases[rand.nextInt(cannedTimes)];
}
System.out.println(mirrored);
transcript[++index] = userWords;
transcript[++index] = mirrored;
}
System.out.println("Thank you for chatting with me! Come back soon!");
System.out.println(" ");
System.out.println("TRANSCRIPT ");
for (int i = 0; i < transcript.length; i++) {
System.out.println(transcript[i]);
}
System.exit(0);
}
}
- 你应该尽量坚持Java naming conventions。我相应地更改了您代码中的变量名称。
- 我没有操纵 [first]
for
循环变量 i
来处理对话回合和成绩单索引,而是使用了一个名为 index
的单独变量作为成绩单.
- Switching on string 已添加到 Java 7.
- 打印成绩单的
for
循环是错误的。终止条件应该是 i < transcript.length
(而不是 i <= transcript.length
)。
- 以上代码假定user-entered 句子由用单个空格分隔的单词组成。如果你想更复杂地处理 user-entered 句子,例如处理标点符号,如逗号、句点等,那么你需要更改 split method's regular expression.
这是示例的输出 运行:
Welcome!
How many rounds of conversation would you like to have?
2
Sounds great! How are you doing today?
OK
Of course
Why do you say that
Why do I say that
Thank you for chatting with me! Come back soon!
TRANSCRIPT
OK
Of course
Why do you say that
Why do I say that
所以我得到了我正在开发的 java 聊天机器人程序。
根据用户所说的内容,如果用户的回复包含关键字之一 I love pizza
--> you love pizza
,聊天机器人会给出预设回复 canned_phrases
或镜像回复.
问题是 聊天机器人没有返回镜像版本。我认为问题在于单词被彼此覆盖,但我不确定如何解决这个问题。
感谢您的帮助!
import java.util.Random;
import java.util.Scanner;
public class Conversation {
public static void main(String[] args) {
String[] canned_phrases = {"Yes",
"Hmmm. Please tell me more news",
"Of course",
"Okay",
"Interesting...",
"Indeed"};
int canned_times = canned_phrases.length;
Scanner conversation_start = new Scanner(System.in);
System.out.println("\nWelcome!\n");
System.out.println("How many rounds of conversation would you like to have?\n");
int rounds = conversation_start.nextInt();
conversation_start.nextLine();
String[] transcript = new String[2 * rounds + 1];
transcript[0] = "Sounds great! How are you doing today?";
System.out.println(transcript[0]);
for (int i = 0; i < rounds; i++) {
String user_words = conversation_start.nextLine();
String mirrored;
String new_version = user_words.replace("I", "you");
new_version = new_version.replace("me", "you");
new_version = new_version.replace("am", "are");
new_version = new_version.replace("you", "I");
new_version = new_version.replace("my", "your");
new_version = new_version.replace("your", "my");
new_version = new_version.replace("my", "you");
if (!new_version.equals(user_words)) {
mirrored = new_version;
}
else {
mirrored = canned_phrases[(int) Math.floor(canned_times * Math.random())];
}
System.out.println(mirrored);
transcript[2 * i + 1] = user_words;
transcript[2 * i + 1] = mirrored;
}
System.out.println("Thank you for chatting with me! Come back soon!");
System.out.println(" ");
System.out.println("TRANSCRIPT ");
for (int i = 0; i <= transcript.length; i++) {
System.out.println(transcript[i]);
}
System.exit(0);
}
}
我对您的代码稍作修改,现在它可以按您的预期运行:
public static void main(String[] args) {
String[] canned_phrases = {"Yes", "Hmmm. Please tell me more news", "Of course", "Okay", "Interesting...", "Indeed"};
int canned_times = canned_phrases.length;
Scanner conversation_start = new Scanner(System.in);
System.out.println("\nWelcome!\n");
System.out.println("How many rounds of conversation would you like to have?\n");
int rounds = conversation_start.nextInt();
conversation_start.nextLine();
String[] transcript = new String[2*rounds+1];
transcript[0] = "Sounds great! How are you doing today?";
System.out.println(transcript[0]);
for (int i = 0; i < rounds; i++) {
String user_words = conversation_start.nextLine();
String mirrored;
String new_version = user_words.replace("I", "you");
new_version = new_version.replace("me","you");
new_version = new_version.replace("am","are");
//1st change as you replaced it above so not swap it again
//new_version = new_version.replace("you","I");
new_version = new_version.replace("my","your");
new_version = new_version.replace("your","my");
new_version = new_version.replace("my","you");
//by commenting the line above, will enter the IF-block
if (!new_version.equals(user_words)) {
mirrored = new_version;
} else {
mirrored = canned_phrases[(int) Math.floor(canned_times * Math.random())];
}
System.out.println(mirrored);
transcript[2*i+1] = user_words;
//2nd change to not overwrite the same index i added 2 instead of 1
transcript[2*i+2] = mirrored;
}
System.out.println("Thank you for chatting with me! Come back soon!");
System.out.println(" ");
System.out.println("TRANSCRIPT ");
//3rd change i removed the = from the loop condition to prevent exception appeared
for (int i = 0; i < transcript.length; i++) {
System.out.println(transcript[i]);
}
System.exit(0);
}
我认为实现你想要的最好方法是将 user-entered 句子拆分成单词并根据你的规则更改单个单词,例如如果单词是 my 然后将其更改为 你的 。在下面的代码中,我按顺序遍历所有单词,根据需要更改每个单词并将它们附加到 StringBuilder
以便在遍历所有单词后,StringBuilder
包含镜像句子。
(代码后有更多注释。)
import java.util.Random;
import java.util.Scanner;
public class Conversation {
public static void main(String[] args) {
String[] cannedPhrases = {"Yes",
"Hmmm. Please tell me more news",
"Of course",
"Okay",
"Interesting...",
"Indeed"};
int cannedTimes = cannedPhrases.length;
Random rand = new Random();
Scanner conversationStart = new Scanner(System.in);
System.out.println("\nWelcome!\n");
System.out.println("How many rounds of conversation would you like to have?\n");
int rounds = conversationStart.nextInt();
conversationStart.nextLine();
String[] transcript = new String[2 * rounds];
transcript[0] = "Sounds great! How are you doing today?";
System.out.println(transcript[0]);
int index = -1;
for (int i = 0; i < rounds; i++) {
String userWords = conversationStart.nextLine();
String mirrored;
StringBuilder result = new StringBuilder();
String[] words = userWords.split(" ");
boolean first = true;
for (String word : words) {
if (first) {
first = false;
}
else {
result.append(' ');
}
switch (word) {
case "I":
word = "you";
break;
case "me":
word = "you";
break;
case "am":
word = "are";
break;
case "you":
word = "I";
break;
case "my":
word = "your";
break;
case "your":
word = "my";
break;
}
result.append(word);
}
String newVersion = result.toString();
if (!newVersion.equals(userWords)) {
mirrored = newVersion;
}
else {
mirrored = cannedPhrases[rand.nextInt(cannedTimes)];
}
System.out.println(mirrored);
transcript[++index] = userWords;
transcript[++index] = mirrored;
}
System.out.println("Thank you for chatting with me! Come back soon!");
System.out.println(" ");
System.out.println("TRANSCRIPT ");
for (int i = 0; i < transcript.length; i++) {
System.out.println(transcript[i]);
}
System.exit(0);
}
}
- 你应该尽量坚持Java naming conventions。我相应地更改了您代码中的变量名称。
- 我没有操纵 [first]
for
循环变量i
来处理对话回合和成绩单索引,而是使用了一个名为index
的单独变量作为成绩单. - Switching on string 已添加到 Java 7.
- 打印成绩单的
for
循环是错误的。终止条件应该是i < transcript.length
(而不是i <= transcript.length
)。 - 以上代码假定user-entered 句子由用单个空格分隔的单词组成。如果你想更复杂地处理 user-entered 句子,例如处理标点符号,如逗号、句点等,那么你需要更改 split method's regular expression.
这是示例的输出 运行:
Welcome!
How many rounds of conversation would you like to have?
2
Sounds great! How are you doing today?
OK
Of course
Why do you say that
Why do I say that
Thank you for chatting with me! Come back soon!
TRANSCRIPT
OK
Of course
Why do you say that
Why do I say that