forked from hola/challenge_word_classifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.js
76 lines (68 loc) · 2 KB
/
solution.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
function unfrcode(buf, cb) {
const LOCATE_MAGIC = '\x00LOCATE02\x00';
let offset = LOCATE_MAGIC.length;
if (buf.toString('ascii', 0, offset) != LOCATE_MAGIC) {
throw 'unfrcode: locatedb file header mismatch: ' + buf[0];
}
let l = 0, path = '';
while (offset < buf.length) {
let dl = buf.readInt8(offset);
++offset;
if (dl == 128) {
dl = buf.readInt16LE(offset);
offset += 2;
}
l += dl;
let nullOffset = buf.indexOf('\x00', offset, 'utf-8');
if (nullOffset == -1) {
nullOffset = buf.length;
}
path = path.slice(0, l) + buf.toString('utf-8', offset, nullOffset);
offset = nullOffset + 1;
cb(path);
}
}
function unarchive(buf, cb) {
const SIMPLE_ARCHIVE_FORMAT_MAGIC = '\x00JSONARCHIVE\x00';
let offset = SIMPLE_ARCHIVE_FORMAT_MAGIC.length;
if (buf.toString('ascii', 0, offset) != SIMPLE_ARCHIVE_FORMAT_MAGIC) {
throw 'simple archive format header mismatch';
}
let descriptorSize = buf.readUInt32LE(offset);
offset += 4;
// descriptor = [["filename", len], ...]
let descriptor = JSON.parse(buf.toString('utf-8', offset, offset + descriptorSize));
offset += descriptorSize;
descriptor.forEach(function(entry, i) {
let entryName = entry[0];
let entrySize = entry[1];
cb(entryName, buf.slice(offset, offset+entrySize));
offset += entrySize;
});
}
let pfxs = {}, sfxs = {};
let vowel_re = /[aeiouy]/g;
function init(data) {
unarchive(data, function(filename, buf) {
switch (filename) {
case "d1": unfrcode(buf, function(line) {
sfxs[line] = 1;
}); break;
case "d2": unfrcode(buf, function(line) {
pfxs[line] = 1;
}); break;
}
});
}
function test(word) {
if (word.length < 5 || word.length > 12) {
return false;
}
word = word.toLowerCase();
if (word.slice(-2) == "'s") {
word = word.slice(0, -2);
}
word = word.replace(vowel_re, '');
return pfxs.hasOwnProperty(word.slice(0, 4)) && sfxs.hasOwnProperty(word.slice(2));
}
module.exports = { "test": test, "init": init };