Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Echooff3
Last active September 8, 2017 22:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Echooff3/fa6b58d3180d0b6826d24ae743803ce5 to your computer and use it in GitHub Desktop.
Save Echooff3/fa6b58d3180d0b6826d24ae743803ce5 to your computer and use it in GitHub Desktop.
Copy GloVe to redis db
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" })
});
{
"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"
}
}
@Echooff3
Copy link
Author

Echooff3 commented Sep 8, 2017

TODO FIX: Process hangs when done. Might be status bar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment