forked from ntrouba/CSC120-A3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConversation.java
More file actions
183 lines (167 loc) · 6.01 KB
/
Copy pathConversation.java
File metadata and controls
183 lines (167 loc) · 6.01 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
import java.util.Scanner;
import java.util.Random;
/**
* Conversation class demonstrates a chat bot.
* @author Grace Codd
*/
class Conversation {
/** Array of canned phrases */
private static String [] cannedPhrases = {"Cool!", "Very interesting.",
"Hmm. Tell me more!", "I don't know much about that.",
"Great!", "Oh wow!", "Good to know."};
/** Main method runs program for chat bot
*/
public static void main(String[] arguments) {
/** Number of conversation rounds */
int rounds;
/** Number of elements in cannedPhrases array */
int nCanned = cannedPhrases.length;
//Create Scanner object for input
Scanner keyboard = new Scanner(System.in);
/** Random object. Will be used to generate random phrase from cannedPhrases*/
Random rand = new Random();
//Ask User for number of rounds
System.out.println("Welcome to the chatbot!");
System.out.println("How many rounds?");
//Store rounds
rounds = keyboard.nextInt();
//Skip a line
keyboard.nextLine();
/** Array to store transcript of conversation */
//Spaces aloted for transcript will be (rounds * 2) + 2.
//rounds * 2 is to alot space for each input and response and the extra +2 is to alot space for the greeting and goodbye.
String[] transcript = new String[(rounds * 2) + 2];
//Store intro in first position of array
transcript[0] = "Hello! What's up with you?";
System.out.println(transcript[0]);
/** Index for element in transcript array */
//Initialized to 0 since there is currently only a value stored in first position
int index = 0;
//Do while loop will repeat until rounds == 0
do{
/** Stores user input */
String input = keyboard.nextLine();
/** Stores outputted response to input */
String output;
/** Stores user input as array of words */
String[] words = input.split(" ");
/** Will store response */
//Used StringBuilder instead of String because StringBuilder is mutable
StringBuilder response = new StringBuilder();
//For loop will check each word one by one and increment counter i each time loop repeats
//i is used as index for words[] array
for(int i = 0; i < words.length; i++){
//Call isMirror, passing element at position i of words
//This will check if the word at positon i is a mirror word
if (isMirror(words[i])){
//If isMirror returns true, replace element at position i with value returned from mirrorReplace method
words[i]= mirrorReplace(words[i]);
//Append replaced word to response
response.append(words[i]);
}
else
//Otherwise, append unchanged word
response.append(words[i]);
if((i + 1) < words.length){
//if we have not reached the last word, append a space
response.append(" ");
}
}
//convert response to string, compare to input with equals method
if(response.toString().equals(input)){
//if equals method returns true, response did not contain mirrored words
//store random canned phrase in output
//nextInt method from Random class generates random integer
//nCanned passed as argument so random integer cannot exceed number of canned responses
//canned phrase stored at position of random integer generated will be stored in output
output = cannedPhrases[rand.nextInt(nCanned)];
}
//Otherwise, store response converted to string in output
else
output = response.toString();
//Print output
System.out.println(output);
//Store input and output of this conversation round
//increment index, store input in this position of transcript array
transcript[++index] = input;
//increment index, store output in this position of transcript array
transcript[++index] = output;
//Decrement rounds
rounds--;
} while (rounds > 0); //Loop will stop when rounds == 0
//Increment index, store goodbye message at this position in transcript.
transcript[++index] = "Goodbye!";
//Print goodbye message
System.out.println(transcript[index]);
//Print blank line
System.out.println();
//Print transcript
System.out.println("TRANSCRIPT:");
//Enhanced for loop prints each element in transcript array
for(String text: transcript){
System.out.println(text);
}
//Quit program
System.exit(0);
}
/** Compares string to a number of mirror words
* @param s word to be compared to mirror words
* @return true if string matches one mirror word, false if not
*/
public static boolean isMirror(String s){
if(s.equalsIgnoreCase("I") ||
s.equalsIgnoreCase("you") ||
s.equalsIgnoreCase("me") ||
s.equalsIgnoreCase("my") ||
s.equalsIgnoreCase("your") ||
s.equalsIgnoreCase("we") ||
s.equalsIgnoreCase("our") ||
s.equalsIgnoreCase("yours") ||
s.equalsIgnoreCase("ours") ||
s.equalsIgnoreCase("am")||
s.equalsIgnoreCase("are"))
return true;
else
return false;
}
/** Replaces string with a mirror word
* @param s word to be replaced
* @return mirror word replacement
*/
public static String mirrorReplace(String s){
//Switch compares string s to each case string and replaces string with mirror word
switch(s){
case "I":
s = "you";
break;
case "You":
s = "I";
break;
case "you":
s = "me";
break;
case "am":
s = "are";
break;
case "are":
s = "am";
break;
case "my":
s = "your";
break;
case "your":
s = "my";
break;
case "we":
s = "you";
break;
case "our":
s = "your";
break;
case "ours":
s = "yours";
break;
}
return s;
}
}