-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestion-ask.js
47 lines (38 loc) · 1.34 KB
/
question-ask.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
let parsedQuestion = {
words: []
};
const generateResponse = (knowledge) => {
let previousWordIndex = 0;
let response = {
phrase: '',
bestSequence: 0,
currentSequence: 0
};
for (const topic in knowledge) {
let topicWords = topic.split(" ");
for (const word in parsedQuestion.words) {
let parsedWord = parsedQuestion.words[word];
switch(true) {
case topicWords.indexOf(parsedWord) > previousWordIndex:
response.currentSequence += 1;
previousWordIndex = topicWords.indexOf(parsedWord);
break;
case response.currentSequence > response.bestSequence:
response.bestSequence = response.currentSequence;
response.phrase = knowledge[topic];
break;
case !topicWords.includes(parsedWord):
response.currentSequence -= 2;
break;
case topicWords.indexOf(parsedWord) < previousWordIndex:
response.currentSequence -= 1;
break;
}
}
}
};
const ask = (question, knowledge) => {
parsedQuestion.words = question.split(" ");
return generateResponse(knowledge);
};
module.exports.ask = ask;