-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.js
70 lines (54 loc) · 1.64 KB
/
chat.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
const dicUrl = 'https://api.dictionaryapi.dev/api/v2/entries/en/';
const maxUncert = .3;
let localDic = { any: {} };
async function getWordInfo(word) {
try {
const response = await fetch(dicUrl + word.toLowerCase());
return new Word((await response.json())[0]);
} catch (error) {
return new Word({ word: word });
}
}
function match(wordData) {
for (let key in patternLists) {
const ps = patternLists[key]
let uncert = ps.reduce((u, p) => u * p.check(wordData), 1);
wordData.patterns[key] = uncert;
if (uncert <= maxUncert) {
localDic[key] ||= {};
localDic[key][wordData.word] = wordData;
}
}
return wordData;
}
function typeUncert(words, key) {
return words.reduce((u, w) => u * w.patterns[key], 1);
}
function hasType(words, key) {
return typeUncert(...arguments) <= maxUncert;
}
async function reply(msg) {
let words = msg.toLowerCase().split(' ').map(async w => match(await (getWordInfo(w))));
let rep = "";
for (i in words) {
words[i] = await words[i];
}
if (hasType(words, 'greeting')) {
rep += 'Hi there! '
}
if (hasType(words, 'user')) {
rep += 'That\'s you!'
}
if (hasType(words, 'shitchat')) {
rep += 'That\'s me! '
let start = 0;
let lastI = words.length - 1;
if (words[lastI].patterns.shitchat <= maxUncert) {
rep += msg.split(' ').slice(0, lastI).join(' ') + ' you too! '
}
}
if (words.some(w => w.patterns.bad <= maxUncert)) {
rep += 'Fuck you!'
}
return rep || 'Sorry, I don\'t understand';
}