-
Notifications
You must be signed in to change notification settings - Fork 1
/
builder.js
66 lines (56 loc) · 1.58 KB
/
builder.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
const zlib = require('zlib')
const fs = require('fs')
const stringifyObject = require('stringify-object')
const Trie = require('./trie.js')
const tr = {
a: 2, b: 2, c: 2,
d: 3, e: 3, f: 3,
g: 4, h: 4, i: 4,
j: 5, k: 5, l: 5,
m: 6, n: 6, o: 6,
p: 7, r: 7, s: 7,
t: 8, u: 8, v: 8,
w: 9, x: 9, y: 9
}
const trie = new Trie([], [])
const words = {}
const lineReader = require('readline').createInterface({
input: process.stdin
})
lineReader.on('line', w => {
if (w.indexOf('q') !== -1 || w.indexOf('z') !== -1) {
return
}
const key = [...w].map(c => tr[c]).join('')
if (!words[key]) {
words[key] = []
}
words[key].push(w)
})
lineReader.on('close', outputTrie)
function outputTrie () {
// Convert the list of words into a trie
Object.keys(words).forEach(key => trie.set(key.split('').map(n => +n), words[key]))
// Create minimal, zippable version for browsers
const code = 'module.exports = '
const minimal = code + stringifyObject(trie, {
singleQuotes: true
}).replace(/([\t\n ])/gm, '')
fs.writeFileSync('min-data.js', minimal)
// And a less-minimal version for fast JSON parsing in Node.
const f = fs.openSync('data.js', 'w')
fs.writeSync(f, `const zlib = require('zlib')
const encodedtrie = \`
`)
const raw = zlib.gzipSync(JSON.stringify(trie)).toString('base64')
for (let i = 0; i < raw.length; i++) {
if (i && i % 73 === 0) {
fs.writeSync(f, '\n')
}
fs.writeSync(f, raw[i])
}
fs.writeSync(f, `\`
module.exports = JSON.parse(zlib.gunzipSync(Buffer.from(encodedtrie, 'base64')).toString('ascii'))
`)
fs.closeSync(f)
}