forked from harthur/brain
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Redis backend for BayesianClassifier
Redis backend functional, but not correct seperate sync and async calls for diff backends memory backend moved bayesian into sep. folder, fix bugs README, more fixes
- Loading branch information
Showing
8 changed files
with
345 additions
and
147 deletions.
There are no files selected for viewing
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
var _ = require("underscore")._, | ||
sys = require("sys"); | ||
|
||
var MemoryBackend = function(options) { | ||
this.catCounts = {}; | ||
this.wordCounts = {}; | ||
} | ||
|
||
MemoryBackend.prototype = { | ||
async : false, | ||
|
||
incCounts : function(catIncs, wordIncs) { | ||
_(catIncs).each(function(inc, cat) { | ||
this.catCounts[cat] = this.catCounts[cat] + inc || inc; | ||
}, this); | ||
|
||
_(wordIncs).each(function(wordCounts, word) { | ||
_(wordCounts).each(function(inc, cat) { | ||
this.wordCounts[word] = this.wordCounts[word] || {}; | ||
this.wordCounts[word][cat] = this.wordCounts[word][cat] + inc || inc; | ||
}, this); | ||
}, this); | ||
}, | ||
|
||
getCats : function() { | ||
return this.catCounts; | ||
}, | ||
|
||
getWordCounts : function(words, cats) { | ||
return this.wordCounts; | ||
} | ||
} | ||
|
||
exports.MemoryBackend = MemoryBackend; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
var redis = require("redis"), | ||
_ = require("underscore")._, | ||
sys = require("sys"); | ||
|
||
var RedisBackend = function(options) { | ||
options = options || {}; | ||
var port = options.port || 6379; | ||
var host = options.hostname || "localhost"; | ||
var opts = options.options || {}; | ||
|
||
var name = options.name || Math.floor(Math.random() * 10000); | ||
this.catsKey = 'brain_bayes_cats_' + name; | ||
this.wordsKey = 'brain_bayes_words_' + name; | ||
|
||
this.client = redis.createClient(port, host, opts); | ||
} | ||
|
||
RedisBackend.prototype = { | ||
async : true, | ||
|
||
key : function(word, cat) { | ||
return word + "____" + cat; // flatten word count hash | ||
}, | ||
|
||
pair : function(key) { | ||
return /(.*)____(.*)/.exec(key).slice(1); | ||
}, | ||
|
||
incCounts : function(catIncs, wordIncs, callback) { | ||
this.client.multi(); // make multi so we can get one callback | ||
|
||
_(catIncs).each(function(inc, cat) { | ||
this.client.hincrby(this.catsKey, cat, inc); | ||
}, this); | ||
|
||
_(wordIncs).each(function(wordCounts, word) { | ||
_(wordCounts).each(function(inc, cat) { | ||
this.client.hincrby(this.wordsKey, this.key(word, cat), inc); | ||
}, this); | ||
}, this); | ||
|
||
var that = this; | ||
this.client.exec(function(err, ret) { | ||
that.client.close(); | ||
callback(ret); | ||
}); | ||
}, | ||
|
||
getCats : function(callback) { | ||
var that = this; | ||
this.client.hgetall(this.catsKey, function(err, cats) { | ||
_(cats).each(function(val, cat) { | ||
cats[cat] = parseInt(val); // redis lib gives strings back | ||
}); | ||
that.client.close(); | ||
callback(cats); | ||
}); | ||
}, | ||
|
||
getWordCounts : function(words, cats, callback) { | ||
var keys = _(words).reduce(function(memo, word) { | ||
return memo.concat(_(cats).map(function(count, cat) { | ||
return this.key(word, cat); | ||
},this)); | ||
}, [], this); | ||
|
||
var that = this; | ||
var hmgetArgs = [this.wordsKey].concat(keys); | ||
hmgetArgs.push(function(err, vals) { | ||
var counts = {}; | ||
keys.map(function(key, i) { | ||
var pair = that.pair(key); | ||
var word = pair[0], cat = pair[1]; | ||
counts[word] = counts[word] ? counts[word] : {}; | ||
counts[word][cat] = parseInt(vals[i]) || 0; | ||
}, this); | ||
that.client.close(); | ||
callback(counts); | ||
}); | ||
|
||
this.client.hmget.apply(this.client, hmgetArgs); | ||
}, | ||
} | ||
|
||
exports.RedisBackend = RedisBackend; |
Oops, something went wrong.