-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMagpie2.java
More file actions
198 lines (183 loc) · 4.67 KB
/
Magpie2.java
File metadata and controls
198 lines (183 loc) · 4.67 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
/*
Aditya Bhatia
10/01/2019
Magpie Lab
*/
import java.lang.Math;
public class Magpie2
{
/**
* 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.indexOf("no") >= 0)
{
response = "Why so negative?";
}
else if (statement.indexOf("mother") >= 0
|| statement.indexOf("father") >= 0
|| statement.indexOf("sister") >= 0
|| statement.indexOf("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";
}
else
{
response = getRandomResponse();
}
return response;
}
/**
* Pick a default response to use if nothing else fits.
* @return a non-committal string
*/
private String getRandomResponse()
{
final int NUMBER_OF_RESPONSES = 6;
double r = Math.random();
int whichResponse = (int)(r * NUMBER_OF_RESPONSES);
String response = "";
if (whichResponse == 0)
{
response = "Interesting, tell me more.";
}
else if (whichResponse == 1)
{
response = "Hmmm.";
}
else if (whichResponse == 2)
{
response = "Do you really think so?";
}
else if (whichResponse == 3)
{
response = "You don't say.";
}
else if (whichResponse == 4)
{
response = "That sounds cool";
}
else if (whichResponse == 5)
{
response = "Awesome!";
}
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();
}
// 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;
}
/**
* 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);
}
}