-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMagpie4.java
More file actions
293 lines (266 loc) · 10.5 KB
/
Magpie4.java
File metadata and controls
293 lines (266 loc) · 10.5 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
Aditya Bhatia
10/01/2019
Magpie Lab
*/
import java.lang.Math;
import java.util.Scanner;
import java.util.Random;
public class Magpie4
{
/**
* Get a default greeting
*
* @return a greeting
*/
public String getGreeting()
{
return "Hello, let's talk.";
}
/**
* Gives a response to a user statement
*
* @param statement
* the user statement
* @return a response based on the rules given
*/
public String getResponse(String statement)
{
String response = "";
if (statement.length() == 0)
{
response = "Say something, please.";
}
else if (findKeyword(statement, "no") >= 0)
{
response = "Why so negative?";
}
else if (findKeyword(statement, "mother") >= 0
|| findKeyword(statement, "father") >= 0
|| findKeyword(statement, "sister") >= 0
|| findKeyword(statement, "brother") >= 0)
{
response = "Tell me more about your family.";
}
else if (statement.indexOf("dog") >= 0
|| statement.indexOf("cat") >= 0)
{
response = "Tell me more about your pets.";
}
else if (statement.indexOf("Mr. Gaw") >= 0)
{
response = "He sounds like a good teacher.";
}
else if (statement.trim().length() == 0)
{
response = "Say something, please.";
}
else if (statement.indexOf("school") >= 0)
{
response = "Tell me more about school.";
}
else if (statement.indexOf("food") >= 0)
{
response = "I'm hungry";
}
else if (statement.indexOf("Earth") >= 0)
{
response = "Mars is superior";
}
// Responses which require transformations
else if (findKeyword(statement, "I want to", 0) >= 0)
{
response = transformIWantToStatement(statement);
}
// Responses which require transformations
else if (findKeyword(statement, "I want", 0) >= 0)
{
response = transformIWantStatement(statement);
}
else
{
// Look for a two word (you <something> me)
// pattern
int psn = findKeyword(statement, "you", 0);
int psn1 = findKeyword(statement, "i", 0);
if (psn >= 0
&& findKeyword(statement, "me", psn) >= 0)
{
response = transformYouMeStatement(statement);
}
else if (psn1 >= 0 && findKeyword(statement, "you", psn1) >=0)
{
response = transformIYouStatement(statement);
}
else
{
response = getRandomResponse();
}
}
return response;
}
/**
* Search for one word in phrase. The search is not case
* sensitive. This method will check that the given goal
* is not a substring of a longer string (so, for
* example, "I know" does not contain "no").
*
* @param statement
* the string to search
* @param goal
* the string to search for
* @param startPos
* the character of the string to begin the
* search at
* @return the index of the first occurrence of goal in
* statement or -1 if it's not found
*/
private int findKeyword(String statement, String goal,
int startPos)
{
String phrase = statement.trim();
// The only change to incorporate the startPos is in
// the line below
int psn = phrase.toLowerCase().indexOf(
goal.toLowerCase(), startPos);
// Refinement--make sure the goal isn't part of a
// word
while (psn >= 0)
{
// Find the string of length 1 before and after
// the word
String before = " ", after = " ";
if (psn > 0)
{
before = phrase.substring(psn - 1, psn)
.toLowerCase();
}
if (psn + goal.length() < phrase.length())
{
after = phrase.substring(
psn + goal.length(),
psn + goal.length() + 1)
.toLowerCase();
}
/* determine the values of psn, before, and after at this point */
//System.out.println(psn + ", " + before + ", " + after);
// If before and after aren't letters, we've
// found the word
if (((before.compareTo("a") < 0) || (before
.compareTo("z") > 0)) // before is not a
// letter
&& ((after.compareTo("a") < 0) || (after
.compareTo("z") > 0)))
{
return psn;
}
// The last position didn't work, so let's find
// the next, if there is one.
psn = phrase.indexOf(goal.toLowerCase(),
psn + 1);
}
return -1;
}
private String transformIWantToStatement(String statement)
{
// Remove the final period, if there is one
statement = statement.trim();
String lastChar = statement.substring(statement
.length() - 1);
if (lastChar.equals("."))
{
statement = statement.substring(0, statement
.length() - 1);
}
int psn = findKeyword (statement, "I want to", 0);
String restOfStatement = statement.substring(psn + 9).trim();
return "What would it mean to " + restOfStatement + "?";
}
private String transformIWantStatement(String statement)
{
// Remove the final period, if there is one
statement = statement.trim();
String lastChar = statement.substring(statement
.length() - 1);
if (lastChar.equals("."))
{
statement = statement.substring(0, statement
.length() - 1);
}
int psn = findKeyword (statement, "I want", 0);
String restOfStatement = statement.substring(psn + 7);
return "Would you really be happy if you had " + restOfStatement + "?";
}
private String transformYouMeStatement(String statement)
{
// Remove the final period, if there is one
statement = statement.trim();
String lastChar = statement.substring(statement
.length() - 1);
if (lastChar.equals("."))
{
statement = statement.substring(0, statement
.length() - 1);
}
int psnOfYou = findKeyword (statement, "you", 0);
int psnOfMe = findKeyword (statement, "me", psnOfYou + 3);
String restOfStatement = statement.substring(psnOfYou + 3, psnOfMe).trim();
return "What makes you think that I " + restOfStatement + " you?";
}
private String transformIYouStatement(String statement)
{
statement = statement.trim();
String lastChar = statement.substring(statement.length() - 1);
if (lastChar.equals("."))
{
statement = statement.substring(0, statement.length() - 1);
}
int psnOfI = findKeyword(statement, "I", 0);
int psnOfYou = findKeyword(statement, "You", psnOfI + 1);
String restOfStatement = statement.substring(psnOfI + 1, psnOfYou).trim();
return "Why do you " + restOfStatement + " me?";
}
/**
* Search for one word in phrase. The search is not case
* sensitive. This method will check that the given goal
* is not a substring of a longer string (so, for
* example, "I know" does not contain "no"). The search
* begins at the beginning of the string.
*
* @param statement
* the string to search
* @param goal
* the string to search for
* @return the index of the first occurrence of goal in
* statement or -1 if it's not found
*/
private int findKeyword(String statement, String goal)
{
return findKeyword(statement, goal, 0);
}
/**
* Pick a default response to use if nothing else fits.
*
* @return a non-committal string
*/
private String getRandomResponse()
{
Random r = new Random ();
return randomResponses [r.nextInt(randomResponses.length)];
}
private String [] randomResponses = {
"Interesting, tell me more.",
"Hmmm.",
"Do you really think so?",
"You don't say.",
"That sounds cool.",
"Awesome!"
};
public static void main(String[] args)
{
Magpie4 maggie = new Magpie4();
String statement = "I like chicken.";
System.out.println("Statement: " + statement);
System.out.println("Response: " + maggie.getResponse(statement));
}
}