Last active
September 8, 2017 22:44
-
-
Save Echooff3/fa6b58d3180d0b6826d24ae743803ce5 to your computer and use it in GitHub Desktop.
Copy GloVe to redis db
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const path = require('path') | |
const redis = require("redis") | |
var client = redis.createClient() | |
const LineByLineReader = require('line-by-line') | |
const lr = new LineByLineReader(process.argv[2]); | |
var lineCount = 0 | |
var currentWord = "" | |
var filename = path.basename(process.argv[2]).replace('.', '') | |
var status = require('node-status') | |
var pizzas = status.addItem('pizza', { | |
custom: () => { return `Adding: # ${lineCount} - ${currentWord}` } | |
}) | |
console.log(filename) | |
status.start({ | |
invert: false, | |
interval: 200, | |
pattern: 'Doing work: {uptime} | {spinner.cyan} | {pizza.custom}' | |
}) | |
client.on("error", function (err) { | |
console.log("Error " + err) | |
}); | |
lr.on('error', function (err) { | |
// 'err' contains error object | |
client.quit() | |
}); | |
lr.on('line', function (line) { | |
// pause emitting of lines... | |
lr.pause() | |
lineCount++ | |
var tokens = line.split(" ") | |
if (/^[a-zA-Z0-9_.-]*$/.test(tokens[0])) { | |
currentWord = tokens[0] | |
var set = [`${filename}:${lineCount}`, "word", `${tokens[0]}`, "vec", `${tokens.splice(1).join(",")}`]; | |
var idx2word = [`${filename}:idx2word:${lineCount}`, tokens[0]]; | |
var word2idx = [`${filename}:word2idx:${tokens[0]}`, lineCount]; | |
client.hmset(set, (errA, response) => { | |
if (errA) throw errA; | |
//console.log('added '+response+' items.'); | |
client.set(idx2word[0], idx2word[1], (errA, response) => { | |
if (errA) throw errA; | |
client.set(word2idx[0], word2idx[1], (errA, response) => { | |
if (errA) throw errA; | |
lr.resume() | |
}) | |
}) | |
}) | |
} else { | |
lr.resume() | |
} | |
}); | |
lr.on('end', function () { | |
// All lines are read, file is closed now. | |
setTimeout( () => client.quit(), 1000 * 5) //give it a few seconds to finish up | |
pizzas.doneStep({ success: true, message: "Done" }) | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "glove-loader", | |
"version": "1.0.0", | |
"description": "Loads GloVe files from https://nlp.stanford.edu/projects/glove/ to redis", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"line-by-line": "^0.1.5", | |
"node-status": "^1.0.0", | |
"redis": "https://github.com/NodeRedis/node_redis" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TODO FIX: Process hangs when done. Might be status bar.