-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotmath.js
314 lines (248 loc) · 7.32 KB
/
notmath.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// fun section lol
var possibilites = function(v = 3) {
var k = (1 << v);
var truths = [];
for (var i = 0; i < k; ++i) {
truths[i] = [];
for (var j = 0; j < v; ++j) {
var value = (i >> j) & 1; // extract the j-th bit of i
truths[i][j] = value;
}
}
return truths;
};
var truthTable = function(str, str2) {
var characters = (str + ' ' + str2).match(/[A-Za-z]+/g).filter((letter, index, arr) => index === arr.indexOf(letter));
var tests = [
{a: true, b: true, c: true},
{a: true, b: true, c: false},
{a: true, b: false, c: true},
{a: true, b: false, c: false},
{a: false, b: true, c: true},
{a: false, b: true, c: false},
{a: false, b: false, c: true},
{a: false, b: false, c: false},
];
if (!str2) {
console.log('a', 'b', 'c', 'result');
for (var i = 0; i < tests.length; ++i) {
console.log(tests[i].a, tests[i].b, tests[i].c, eval('var a = ' + tests[i].a + ';var b = ' + tests[i].b + ';var c = ' + tests[i].c + ';' + str));
}
} else {
for (var i = 0; i < tests.length; ++i) {
if (eval('var a = ' + tests[i].a + ';var b = ' + tests[i].b + ';var c = ' + tests[i].c + ';' + str) !== eval('var a = ' + tests[i].a + ';var b = ' + tests[i].b + ';var c = ' + tests[i].c + ';' + str2)) {
return false;
}
}
return true;
}
};
var Discord = require('discord.js');
var words = require('an-array-of-english-words');
var esrever = require('esrever');
// implementation of levenshtein distance algorithm
var levenshtein = function(s, t) {
var d = []; // 2D matrix
// Step 1
var n = s.length;
var m = t.length;
if (n == 0) {
return m;
}
if (m == 0) {
return n;
}
for (var i = n; i >= 0; --i) {
d[i] = [];
}
// Step 2
for (var i = n; i >= 0; --i) {
d[i][0] = i;
}
for (var j = m; j >= 0; --j) {
d[0][j] = j;
}
// Step 3
for (var i = 1; i <= n; ++i) {
var s_i = s.charAt(i - 1);
// Step 4
for (var j = 1; j <= m; ++j) {
// check jagged ld total so far
if (i == j && d[i][j] > 4) {
return n;
}
var t_j = t.charAt(j - 1);
var cost = (s_i == t_j) ? 0 : 1; // Step 5
// calculate minimum
var mi = d[i - 1][j] + 1;
var b = d[i][j - 1] + 1;
var c = d[i - 1][j - 1] + cost;
if (b < mi) {
mi = b;
}
if (c < mi) {
mi = c;
}
d[i][j] = mi; // Step 6
// damerau transposition
if (i > 1 && j > 1 && s_i == t.charAt(j - 2) && s.charAt(i - 2) == t_j) {
d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + cost);
}
}
}
// Step 7
return d[n][m];
};
// returns an object containing the counts of every character in a string
var counts = function(str) {
var counts = {};
for (var i = 0; i < str.length; ++i) {
if (counts[str[i]] === undefined) {
counts[str[i]] = 1;
} else {
++counts[str[i]];
}
}
return counts;
};
// given an object containing the counts of the user input word and the counts of a word, check if inCounts can be unscrambled into dictCounts
var inputInDict = function(inCounts, dictCounts) {
// all keys (letters) in dictCounts MUST be present in inCounts
if (Object.keys(dictCounts).find(function(key) {
return inCounts[key] === undefined;
}) !== undefined) {
return false;
}
// all keys in inCounts MUST appear the same amount or more times as its corresponding key in dictCounts
if (Object.keys(inCounts).find(function(key) {
return inCounts[key] < dictCounts[key];
}) !== undefined) {
return false;
}
return true;
};
module.exports = {
// converts every L / R in an input to W
aegyo: (input) => input.replace(/[LR]/g, 'W').replace(/[lr]/g, 'w'),
// find the words closest to the input (spell check)
closest: function(input) {
var foundWords = [];
var min = Number.MAX_SAFE_INTEGER;
words.forEach(function(dictWord) {
var dist = levenshtein(input, dictWord);
if (dist < min) {
foundWords = [dictWord];
min = dist;
} else if (dist === min) {
foundWords.push(dictWord);
}
});
return foundWords;
},
// parses a string of properties that describe an embed
embed: function(str, users) {
var properties = [...str.matchAll(/\s*([^,]+?)\s*\:\s*([^,]+)/g)];
var embed = new Discord.MessageEmbed();
properties.forEach(function(p) {
if (p[1] === 'author.name') {
if (embed.author === null) {
embed.author = {};
}
embed.author.name = p[2];
} else if (p[1] === 'author.icon') {
if (embed.author === null) {
embed.author = {};
}
if (/(?:(?:https?|ftp):\/\/|\b(?:[a-z\d]+\.))(?:(?:[^\s()<>]+|\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))?\))+(?:\((?:[^\s()<>]+|(?:\(?:[^\s()<>]+\)))?\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))?/g.test(p[2])) {
embed.author.iconURL = p[2];
} else if (/<@!?\d+>/.test(p[2])) {
var user = users.resolve(p[2].match(/\d+/)[0]);
embed.author.iconURL = user.displayAvatarURL();
} else {
var user = users.cache.find((user) => user.username === p[2]);
embed.author.iconURL = user.displayAvatarURL();
}
} else if (p[1] === 'author.url') {
if (embed.author === null) {
embed.author = {};
}
embed.author.url = p[2];
} else if (p[1] === 'color') {
embed[p[1]] = Discord.resolveColor(p[2]);
} else {
embed[p[1]] = p[2];
}
});
console.log(embed);
return embed;
},
// implementation of levenshtein distance algorithm
levenshtein: levenshtein,
// randon number
random: function(min, max) {
if (max === undefined) { // 0 to max
return Math.floor(Math.random() * Math.floor(min));
} else { // min to max
return Math.floor(Math.random() * Math.floor(max - min)) + min;
}
},
// reverse a string (unicode-aware)
reverse: function(str) {
var result = esrever.reverse(str);
// fix mentions
result = result.replace(/>(\d+)!?(@|#)</g, function(match, id, symbol) {
return '<' + symbol + id.split('').reverse().join('') + '>';
});
return result;
},
// scramble a word
scramble: function(word) {
word = word.split('');
var num = 0;
var scramble = '';
while (word.length > 0) {
num = word.length * Math.random() << 0;
scramble += word[num];
word.splice(num, 1);
}
return scramble;
},
// randomly capitalizes a string
uglify: function(str) {
var arr = str.split('');
var mode = 0;
for (var i = 0; i < arr.length; ++i) {
if (/[A-Za-z]/.test(arr[i])) {
if (mode === 0) {
arr[i] = arr[i].toLowerCase();
mode = 1;
} else {
arr[i] = arr[i].toUpperCase();
mode = 0;
}
}
}
return arr.join('');
},
// unscramble a word
unscramble: function(word, wordLength) {
if (wordLength === undefined) {
wordLength = word.length;
}
wordLength = Number(wordLength);
word = word.toLowerCase();
var matches = [];
var sortedWord = word.split('').sort().join('');
var sortedCounts = counts(sortedWord);
words.forEach(function(dictWord) {
if (matches.length >= 100) {
return;
}
var dictCounts = counts(dictWord);
if (dictWord.length === wordLength && inputInDict(sortedCounts, dictCounts)) {
matches.push(dictWord);
}
});
return matches;
},
};