-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatBotState.js
178 lines (149 loc) · 4.34 KB
/
chatBotState.js
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
class Question {
constructor() {
this.question = [];
this.answer = '';
this.validAnswers = [];
this.filterPhrases = [];
//this.reply = {};
}
setQuestion(newQuestion){
this.question = newQuestion;
}
setValidAnswers(newAnswers){
this.validAnswers = newAnswers;
}
setAnswer(answer){
this.answer = answer;
}
setFilterPhrases(phrases){
this.filterPhrases = phrases;
}
// setReply(replies) {
// this.reply = replies;
// }
getQuestion() {
var mood = getCookieVal("mood");
if (this.question.length == 1) {
return this.question[0];
}
else {
if (mood < -1) {
return this.question[1];
}
else if (mood > 1) {
return this.question[2];
}
else {
return this.question[0];
}
}
}
getAnswer() {
return this.answer;
}
getValidAnswers() {
return this.validAnswers;
}
getFilterPhrases(){
return this.filterPhrases;
}
// getReply(){
// return this.reply;
// }
filter(answer){
var phrases = this.filterPhrases;
for (var i = 0; i < phrases.length; i++) {
if (answer.includes(phrases[i])) {
answer = answer.replace(phrases[i], '');
}
}
return answer;
}
// getReplies(answer) {
// var reply = this.getReply();
// if (Object.keys(reply).length == 0) {
// return [];
// }
// for(var text in reply) {
// if (answer == text || text=="else") {
// return reply[text];
// }
// }
// }
}
class State {
constructor(){
this.questions = [];
this.stateName = "";
this.nextState = null;
this.currentQuestion = 0;
}
setName(newName){
this.stateName = newName;
}
setQuestions(newQuestion){
this.questions.push(newQuestion);
}
setNext(newState){
this.nextState = newState;
}
getQuestions() {
return this.questions;
}
getIndex() {
return this.currentQuestion;
}
incrementIndex() {
this.currentQuestion += 1;
}
checkQuestions(answer){
var questions = this.getQuestions();
var found = false;
var index = this.getIndex();
var validAnswers = questions[index].getValidAnswers();
var filteredAnswer = questions[index].filter(answer);
if (validAnswers.length == 0) {
questions[index].setAnswer(filteredAnswer); //Set the answer to that question
found = true;
}
else {
for (var j = 0; j < validAnswers.length; j++) { //Loop through expected answers for this question
if (filteredAnswer == validAnswers[j]) { //If it's a valid answer
questions[index].setAnswer(filteredAnswer); //Set the answer to that question
found = true;
break;
}
}
if (found == false) {
var confusion = ["I didn't understand that one.", "I don't get what you're saying.", "I'm confused what that means.", "I don't understand that. Try talking better.", "I have no idea what you're talking about.", "I'm not sure what that means. Hey, I'm not a perfect chatbot.", "You've stumped me. Try phrasing differently."];
var response = confusion[Math.floor(Math.random()*confusion.length)];
return response;
}
}
this.incrementIndex();
index += 1;
if (index == questions.length) {
return "";
}
//var response = response.concat(questions[index].getQuestion());
return questions[index].getQuestion();
}
}
class chatBot {
constructor(){
this.states = [];
this.mood = 0;
}
incrementMood(){
this.mood += 1;
}
decrementMood(){
this.mood -= 1;
}
getMood() {
return this.mood;
}
addState(state) {
this.states.push(state);
}
}