Skip to content

Commit

Permalink
Redis backend for BayesianClassifier
Browse files Browse the repository at this point in the history
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
harthur committed Sep 14, 2010
1 parent 40c1394 commit 12e6bc9
Show file tree
Hide file tree
Showing 8 changed files with 345 additions and 147 deletions.
25 changes: 12 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# brain

brain is a limited JavaScript supervised machine learning library. Neural network example:
brain is a limited JavaScript supervised machine learning library. [Full API here](http://harthur.github.com/brain). Neural network example:
var net = new brain.NeuralNetwork();
net.train([{input: [0, 0], output: [0]},
{input: [0, 1], output: [1]},
Expand All @@ -12,22 +12,16 @@ brain is a limited JavaScript supervised machine learning library. Neural networ
The output will be `[0.987]` or something close like that. There's no reason to use a neural network to figure out XOR, but it's a small example (-:

Naive Bayesian classifier example:
var classifier = new brain.BayesianClassifier();
var bayes = new brain.BayesianClassifier();

classifier.train("cheap replica watches", "spam");
classifier.train("I don't know if this works on Windows", "not");
bayes.train("cheap replica watches", "spam");
bayes.train("I don't know if this works on Windows", "not");

var category = classifier.classify("free watches");


# API
[http://harthur.github.com/brain#api](http://harthur.github.com/brain)
var category = bayes.classify("free watches");

# using in the browser
The neural network works in the browser. Download the latest [brain.js](http://github.com/harthur/brain/downloads). If you can you should train the network offline (or on a Worker) and use the `toFunction()` or `toJSON()` options to plug the trained network in to your website.

# using as a commonJS package
To use this as a commonJS package (node/narwhal) checkout or download the code, it is a commonJS package. If you have [node](http://nodejs.org/) and [npm](http://github.com/isaacs/npm) you can:
To use this as a commonJS package (node/narwhal) checkout or download the code, it's a commonJS package. If you have [node](http://nodejs.org/) and [npm](http://github.com/isaacs/npm) you can:

npm install brain@latest

Expand All @@ -36,7 +30,12 @@ then:
var brain = require("brain");
var net = new brain.NeuralNetwork();

If you didn't install with npm, you can specify the path to the brain.js file, like `require("./lib/brain")`.
If you didn't install with npm, you can specify the path to the brain.js file, like `require("./lib/brain")`. If you're using the Redis backend for the Bayesian classifier, you'll also need to install [redis-node](http://github.com/bnoguchi/redis-node) as "redis".


# using in the browser
The `NeuralNetwork` works in the browser. Download the latest [brain.js](http://github.com/harthur/brain/downloads). If you can you should train the network offline (or on a Worker) and use the `toFunction()` or `toJSON()` options to plug the trained network in to your website.


# tests
Running the tests requires [node.js](http://nodejs.org/). To run the suite of API tests:
Expand Down
129 changes: 0 additions & 129 deletions lib/bayesian.js

This file was deleted.

34 changes: 34 additions & 0 deletions lib/bayesian/backends/memory-backend.js
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;
85 changes: 85 additions & 0 deletions lib/bayesian/backends/redis-backend.js
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;
Loading

0 comments on commit 12e6bc9

Please sign in to comment.