-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicChatbot.java
67 lines (57 loc) · 2.18 KB
/
BasicChatbot.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.util.*;
import java.util.regex.Pattern;
import java.util.Scanner;
public class BasicChatbot {
private static Map<String, String> responses = new HashMap<>();
private static List<String> greetings = Arrays.asList("hello", "hi", "hey", "good morning", "good afternoon", "good evening");
private static List<String> farewells = Arrays.asList("bye", "goodbye", "see you", "talk to you later");
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String userText;
System.out.println("Hello! I'm your BasicChatbot. How can I help you?");
// Populate responses
responses.put("how are you", "I'm doing well, thanks for asking!");
responses.put("what is your name", "My name is BasicChatbot.");
// ...one can add more responses
while (true) {
userText = scanner.nextLine().toLowerCase();
if (isGreeting(userText)) {
System.out.println("Hello there!");
} else if (isFarewell(userText)) {
System.out.println("Goodbye!");
break;
} else {
String response = findResponse(userText);
if (response != null) {
System.out.println(response);
} else {
System.out.println("I didn't quite understand that.");
}
}
}
}
private static boolean isGreeting(String text) {
for (String greeting : greetings) {
if (text.contains(greeting)) {
return true;
}
}
return false;
}
private static boolean isFarewell(String text) {
for (String farewell : farewells) {
if (text.contains(farewell)) {
return true;
}
}
return false;
}
private static String findResponse(String text) {
for (Map.Entry<String, String> entry : responses.entrySet()) {
if (text.contains(entry.getKey())) {
return entry.getValue();
}
}
return null;
}
}