-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.js
63 lines (56 loc) · 1.96 KB
/
backend.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
//compare and fill reply with a basic answer
function compare(promptsArray, repliesArray, string) {
let reply;
let replyFound = false;
for (let x = 0; x < promptsArray.length; x++) {
for (let y = 0; y < promptsArray[x].length; y++) {
if (promptsArray[x][y] === string) {
let replies = repliesArray[x];
reply = replies[Math.floor(Math.random() * replies.length)];
replyFound = true;
// Stop inner loop when input value matches prompts
break;
}
}
if (replyFound) {
// Stop outer loop when reply is found instead of interating through the entire array
break;
}
}
return reply;
}
//compare keywords and return the good keyword object in the List
//param text is a string where a keyword is inside or not
//list is the list of keywords objects that might be in the text
function keycompare(text, list) {
for (let i = 0; i < list.length; i++) {
let words = list[i].get_wordlist();
for (let j = 0; j < words.length; j++) {
if (text.match(words[j])) {
return list[i];
}
}
}
return null;
}
//the function returns an object response to be used by the front-end
//param input is the string input from user
function keyword_to_response(input) {
let product;
let response;
let text = input.toLowerCase();
if (compare(prompts, replies, text)) {
// Search for exact match in `prompts`
product = compare(prompts, replies, text);
response = new Response(product, "Logo-bonjour.jpg", "");
}
else if ((keyword = keycompare(input, list_of_keywords)) != null){
response = new Response(keyword.get_text(), keyword.get_pictogram(), keyword.get_link());
}
else {
// did not understand
product = alternative[Math.floor(Math.random() * alternative.length)];
response = new Response(product, "Logo-incomprehension.jpg", "");
}
return response;
}