forked from Kitsumi/cypress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatbot.js
68 lines (54 loc) · 1.61 KB
/
chatbot.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
const Markov = require("./markov.js");
const removeDiacritics = require("./diacritics.js");
const fs = require('fs');
var db = {};
try {
let data = fs.readFileSync("./memory.json", "utf8");
db = JSON.parse(data);
console.log("Loaded memory file.");
} catch (e) {
console.log("Memory file dead.");
}
let badResponses = [
"i dont understand",
"im unfamiliar with those words",
"what was that",
"im confused",
"what",
"im not sure that makes sense"
];
let lastMessage = "hello";
function getResponse(string) {
let cleaned = removeDiacritics(string).toLowerCase().match(/([\w\d\s]+|\<\@\![\d]+\>|\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])/gi);
if (cleaned && cleaned.length < 1) {
lastMessage = badResponses[Math.floor(Math.random() * badResponses.length)];
return lastMessage;
}
cleaned = cleaned.join("");
let splitMessage = lastMessage.split(" ");
for (var i = 0; i < splitMessage.length; i++) {
let word = splitMessage[i];
if (!db.hasOwnProperty(word)) {
db[word] = [];
}
db[word].push(cleaned);
}
fs.writeFileSync("./memory.json", JSON.stringify(db, null, 4), "utf8");
let words = cleaned.split(" ");
let dictionary = [];
for (var i = 0; i < words.length; i++) {
let word = words[i];
if (db.hasOwnProperty(word)) {
dictionary = dictionary.concat(db[word]);
}
}
if (dictionary.length < 1) {
lastMessage = badResponses[Math.floor(Math.random() * badResponses.length)];
} else {
lastMessage = Markov(Math.floor(Math.random() * 10) + 5, dictionary);
}
return lastMessage;
}
module.exports = {
getResponse: getResponse
};