-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatbotDriver.java
More file actions
executable file
·131 lines (111 loc) · 3.65 KB
/
Copy pathChatbotDriver.java
File metadata and controls
executable file
·131 lines (111 loc) · 3.65 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// version 1.0
import java.io.*;
import java.util.*;
import java.util.regex.*;
public class ChatbotDriver {
private static List<String> vocabulary;
private static void readVocabulary() {
File file = new File("vocabulary.txt");
vocabulary = new ArrayList<String>();
vocabulary.add("OOV"); // 0 -> OOV
try (Scanner fin = new Scanner(file)) {
while (fin.hasNextLine()) {
vocabulary.add(fin.nextLine());
}
} catch (FileNotFoundException ex) {
System.out.println("File not found.");
}
}
public static String generateCommand(String userInput) {
StringBuilder sb = new StringBuilder("java Chatbot 700 -1 ");
List<Integer> prefix = new ArrayList<Integer>();
String response = "";
// TODO: Students could add customized rules to teach the Chatbot :)
if (userInput.matches("(?i)(.*)(opinion on)(.*)")) {
Pattern pattern = Pattern.compile("(?i)(.*)(opinion on)(.*)");
Matcher matcher = pattern.matcher(userInput);
while (matcher.find())
response = removeFinalPunctuation(pronounChange(matcher.group(3)));
}
else if (userInput.matches("(?i)(.*)(what is)(.*)")) {
Pattern pattern = Pattern.compile("(?i)(.*)(what is)(.*)");
Matcher matcher = pattern.matcher(userInput);
while (matcher.find())
response = removeFinalPunctuation(pronounChange(matcher.group(3)));
}
else if (userInput.matches("(?i)(tell me a joke)(.*)") ||
userInput.matches("(?i)(say anything)(.*)")) {
response = "";
}
else {
response = removeFinalPunctuation(pronounChange(userInput));
}
for (String s : response.split(" ")) {
int idx = vocabulary.indexOf(s);
if (idx == -1) // OOV
prefix.add(0);
else
prefix.add(idx);
}
sb.append(prefix.size() + " ");
for (int i : prefix) {
sb.append(i + " ");
}
System.out.print(response + " ");
return sb.toString();
}
public static String removeFinalPunctuation(String input) {
if (input.length() > 1 && (input.charAt(input.length() - 1) == '.' || input.charAt(input.length() - 1) == '?'
|| input.charAt(input.length() - 1) == '!'))
input = input.substring(0, input.length() - 1);
return input.trim();
}
public static String pronounChange(String input) {
String[] tokens = input.split(" ");
StringBuilder sb = new StringBuilder();
for (String token : tokens) {
if (token.equalsIgnoreCase("i"))
sb.append("you ");
else if (token.equalsIgnoreCase("me"))
sb.append("you ");
else if (token.equalsIgnoreCase("my"))
sb.append("your ");
else if (token.equalsIgnoreCase("mine"))
sb.append("yours ");
else if (token.equalsIgnoreCase("you"))
sb.append("i ");
else if (token.equalsIgnoreCase("your"))
sb.append("my ");
else if (token.equalsIgnoreCase("yours"))
sb.append("mine ");
else if (token.equalsIgnoreCase("am"))
sb.append("are ");
else if (token.equalsIgnoreCase("are"))
sb.append("am ");
else
sb.append(token + " ");
}
return sb.toString().trim();
}
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
readVocabulary();
System.out.print("You: ");
while (in.hasNextLine()) {
String input = in.nextLine().trim().toLowerCase();
System.out.print("Chatbot: ");
String command = generateCommand(input);
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(command);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line;
while ((line = stdInput.readLine()) != null) {
String response = vocabulary.get(Integer.parseInt(line));
System.out.print(removeFinalPunctuation(response) + " ");
}
System.out.println();
System.out.print("You: ");
}
in.close();
}
}