-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
296 lines (257 loc) Β· 8.55 KB
/
app.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
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
294
295
296
const submit= document.querySelector('.submit');
const text=document.querySelector('.text');
var count=0;
var check=[4,8,12,16,20,24,28,32,36,40,44,48,52];
// for clicking of the submit button
submit.addEventListener('click', function() {
let text=document.querySelector('.text').value;
// playing the notification
document.getElementById('user-notif').play();
count = count+ 1;
output(text);
document.querySelector(".text").value = "";
if (check.includes(count)) {
clear();
console.log('done');
}
})
// for pressing the enter key
text.addEventListener('keydown', function(e) {
if (e.code === "Enter"){
let text=document.querySelector('.text').value;
count = count+ 1;
// playing the notification
document.getElementById('user-notif').play();
output(text);
document.querySelector(".text").value = "";
if (check.includes(count)) {
clear();
console.log('done');
}
}
})
function output(input) {
// removing all the characters except word, digits and spaces
let text = input.toLowerCase().replace(/[.,\/#!$%\^&\*;:{}=\-_`~()?']/g,"").replace(/^\s+|\s+$/g, "").replace(/\s+/g, " ");
// changing different styles of text into simple understandable text
// like
// 'tell me a story' -> 'tell me story'
// 'i feel happy' -> 'happy'
text = text
.replace(/ a /g, " ")
.replace(/i feel /g, "")
.replace(/whats/g, "what is")
.replace(/please /g, "")
.replace(/please/g, "")
.replace(/so/,"")
.replace(/so /,"");
if (compare(trigger, reply, text)) {
product = compare(trigger, reply, text);
} else if (text.match(/robot/gi)) {
product = robot[Math.floor(Math.random() * robot.length)];
} else {
product = alternative[Math.floor(Math.random() * alternative.length)];
}
// update DOM
create__chat(input, product);
// todo:make the bot speak
}
const mobile__screen= document.querySelector('.mobile__screen__chats');
function create__chat(input,product) {
let user__chat= document.createElement('div');
user__chat.className= "mobile__screen__chats__left chats";
user__chat.id="user";
user__chat.innerHTML=input;
// setting up dynamic width
user__chat.style.width=((input.length + 1) * 8) + 'px';
mobile__screen.appendChild(user__chat);
let bot__chat= document.createElement('div');
bot__chat.className= "mobile__screen__chats__right chats";
bot__chat.id="bot";
bot__chat.innerHTML=product;
// todo: change the width according to the input
// setting up dynamic width
bot__chat.style.width=((product.length + 1) * 8) + 'px';
// setting a delay for the bot reply
setTimeout(bot_reply,1000);
}
function bot_reply() {
// playing the notification
document.getElementById('bot-notif').play();
let bot__chat= document.createElement('div');
bot__chat.className= "mobile__screen__chats__right chats";
bot__chat.id="bot";
bot__chat.innerHTML=product;
// todo: change the width according to the input
// setting up dynamic width
bot__chat.style.width=((product.length + 1) * 8) + 'px';
mobile__screen.appendChild(bot__chat);
}
// clears the chat area after four chats
function clear() {
const user= document.querySelectorAll("#user");
const bot=document.querySelectorAll("#bot");
for (var i=0; i<user.length-1;i++) {
user[i].style.display="none";
bot[i].style.display="none";
}
count=0;
}
// some triggers for the conversation
const robot = ["i am not a robot", "hi, fellow human what are you doing ?"]
const trigger = [
//0
["hi", "hey", "hello","yo"],
//1
["how are you"],
//2
["what is going on", "what is up"],
//3
["happy", "good", "well", "fantastic", "cool", "great","same","nothing much","thats like a good boy"],
//4
["bad", "bored", "tired", "sad","meh"],
//5
["tell me story", "tell me joke"],
//6
["thanks", "thank you"],
//7
["bye", "good bye", "goodbye"],
// 8
["fuck","holyshit","bullshit","motherfucker","bs","wtf"],
// 9
["what is your name"],
// 10
["good morning"],
// 11
["good night"],
// 12
["good evening"],
//13
["abe sale","sale","chal nikal"],
//14
["type to kar sakta h"]
];
const reply = [
//0
["Hello π", "Hi π", "Hey π", "Hi there π"],
//1
[
"Fine... how are you ?",
"Pretty well, how are you ?",
"Fantastic, how are you ?"
],
//2
[
"Nothing much"
],
//3
["Glad to hear it"],
//4
["Cheer up buddy π","Things will get better π"],
//5
["What about ?", "Once upon a time..."],
//6
["You're welcome", "No problem"],
//7
[" Goodbye", "See you later"],
// 8
["You disappointed me π"],
// 9
["RohChat, what's your's ?"],
// 10
["Good Morning π"],
// 11
["Good Night π"],
// 12
["Good Evening π"],
//13
["Bol Sale"],
//14
["Ha, lekin mood nehi hain"]
];
const alternative = [
" π€",
"Ok",
"Yup",
"Nope"
];
function compare(triggerArray, replyArray, text) {
let item;
for (let x = 0; x < triggerArray.length; x++) {
for (let y = 0; y < replyArray.length; y++) {
if (triggerArray[x][y] == text) {
items = replyArray[x];
item = items[Math.floor(Math.random() * items.length)];
}
}
}
return item;
}
// menu
const open= document.querySelector('#open');
const close= document.querySelector('#close');
const nav= document.querySelector('.mobile__navbar');
open.addEventListener('click', function() {
nav.classList.add("show");
open.style.display="none";
close.style.display="block";
})
close.addEventListener('click', function() {
nav.classList.remove('show');
open.style.display="block";
close.style.display="none";
})
const screen_drop= document.querySelector('#screen__drop') ;
const layout_drop= document.querySelector('#layout__drop') ;
screen__drop.addEventListener('click', function() {
document.querySelector('.screen__colors').classList.toggle('appear');
layout__drop.classList.toggle('click_one_at_a_time');
})
layout__drop.addEventListener('click', function() {
document.querySelector('.layout__colors').classList.toggle('appear');
screen__drop.classList.toggle('click_one_at_a_time');
})
// about
document.querySelector('.about').addEventListener('click', function() {
document.querySelector('.about__cloud').classList.toggle('show');
})
// changing colors
const mobile_layout= document.querySelector('.mobile__layout');
const mobile_screen= document.querySelector('.mobile__screen');
const mobile_edge= document.querySelector('.mobile__screen__edge');
// for layout
document.querySelector('.layout__black__icon').addEventListener('click', function() {
mobile_layout.style="background:black";
mobile_edge.style="background:black";
})
document.querySelector('.layout__purple__icon').addEventListener('click', function() {
mobile_layout.style="background:#7F00FF";
mobile_edge.style="background:#7F00FF";
})
document.querySelector('.layout__green__icon').addEventListener('click', function() {
mobile_layout.style="background:greenyellow";
mobile_edge.style="background:greenyellow";
})
document.querySelector('.layout__pink__icon').addEventListener('click', function() {
mobile_layout.style="background:pink";
mobile_edge.style="background:pink";
})
// for screen
document.querySelector('.screen__black__icon').addEventListener('click', function() {
mobile_screen.style="background:black";
})
document.querySelector('.screen__purple__icon').addEventListener('click', function() {
mobile_screen.style="background:#7F00FF";
})
document.querySelector('.screen__green__icon').addEventListener('click', function() {
mobile_screen.style="background:greenyellow";
})
document.querySelector('.screen__pink__icon').addEventListener('click', function() {
mobile_screen.style="background:pink";
})
// intro
const intro= document.querySelector('.intro');
const start= document.querySelector('#get__started');
start.addEventListener('click', function() {
intro.classList.add('fade__intro');
})