-
Notifications
You must be signed in to change notification settings - Fork 0
/
load-data.js
62 lines (54 loc) · 1.45 KB
/
load-data.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
/**
* Load stuff into redis.
*/
var fs = require("fs");
var Lazy=require("lazy");
var redis = require("redis");
var underscore = require("./static/lib/underscore-1.3.3-min");
var db = redis.createClient();
db.on("error", function (err) {
console.error("REDIS ERROR (server): " + err);
});
var addToDb = function(score, str) {
var s = str.toLowerCase().trim();
console.log("Adding: " + score + " => " + s);
db.zscore("nsp:words", s, function(err, res) {
if (!res) {
// don't re-add
db.zadd("nsp:words", score, s);
} else {
console.log("skipping (re-add): " + s);
}
});
};
// made this words list from the 12 dictionaries source,
// 2+2 google, then 2007neo
var readNeol = function() {
var stream = fs.createReadStream('data/words');
var i = 0;
new Lazy(stream)
.lines
.forEach(function(lineAsBytes) {
i++;
var line = lineAsBytes.toString();
if (line.match(/^----/)) {
// do nothing
console.log("skipping: ", line);
} else if (line.match(/^ /) || line.match(/^[a-zA-Z]* -> /)) {
underscore.each(line.split(/[^a-zA-Z]/), function(word) {
if (word.length > 0) {
addToDb(i, word);
}
});
} else if (line.match(/ /)) {
// do nothing
console.log("skipping: ", line);
} else if (line.length > 0) {
addToDb(i, line);
}
// console.log(i + " => " + line.toString().toLowerCase());
// db.zadd("nsp:words", i, line.toString().toLowerCase());
});
};
readNeol();
// process.exit(code=0);