Skip to content

Commit

Permalink
Added dev dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
tejzpr committed May 1, 2017
1 parent 36fd389 commit 98b05d4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 26 deletions.
26 changes: 11 additions & 15 deletions index-es5.js
@@ -1,20 +1,18 @@
'use strict';

var after = require('after-all');
var mutexify = require('mutexify');
var through = require('through2');
var pump = require('pump');
var b = require('bluebird');
var noop = function noop() {};
var noop = function () {};

var defaultTokenize = function defaultTokenize(str) {
var defaultTokenize = function (str) {
str = str.replace(/[^\w\s]/g, ' ').trim();
return !str ? [] : str.split(/\s+/).map(function (s) {
return s.toLowerCase();
});
};

var get = function get(db, names, cb) {
var get = function (db, names, cb) {
var result = {};

var next = after(function (err) {
Expand All @@ -33,7 +31,7 @@ var get = function get(db, names, cb) {
});
};

var defaultFrequencyTable = function frequencyTable(tokens) {
var defaultFrequencyTable = function (tokens) {
var table = {};

tokens.forEach(function (token) {
Expand All @@ -49,9 +47,9 @@ module.exports = function (db, opts) {
var that = {};
var lock = mutexify();
var tokenize = opts.tokenize || defaultTokenize;
var frequencyTable = opts.frequency || defaultFrequencyTable
var frequencyTable = opts.frequency || defaultFrequencyTable;

var train = function train(category, tokens, cb) {
var train = function (category, tokens, cb) {
if (!cb) cb = noop;
if (typeof tokens === 'string') tokens = tokenize(tokens);

Expand All @@ -61,7 +59,7 @@ module.exports = function (db, opts) {
var catTokens = 'tokens!' + category;
var vocInc = 0;

var put = function put(key, value) {
var put = function (key, value) {
batch.push({
type: 'put',
key: key,
Expand Down Expand Up @@ -129,7 +127,7 @@ module.exports = function (db, opts) {
var freqs = frequencyTable(tokens);
var chosen = null;

var write = function write(data, enc, cb) {
var write = function (data, enc, cb) {
var category = data.key.slice('samples!'.length);
var catSamples = Number(data.value);
var catFreq = 'frequency!' + category + '!';
Expand All @@ -156,7 +154,7 @@ module.exports = function (db, opts) {

if (logProb > maxProp) {
maxProp = logProb;
chosen = {category,match:100+logProb};
chosen = { category, match: 100 + logProb };
}

cb();
Expand All @@ -177,7 +175,7 @@ module.exports = function (db, opts) {
var freqs = frequencyTable(tokens);
var chosen = [];

var write = function write(data, enc, cb) {
var write = function (data, enc, cb) {
var category = data.key.slice('samples!'.length);
var catSamples = Number(data.value);
var catFreq = 'frequency!' + category + '!';
Expand Down Expand Up @@ -210,9 +208,7 @@ module.exports = function (db, opts) {

pump(db.createReadStream({ gt: 'samples!', lt: 'samples!\xff' }), through.obj(write), function (err) {
if (err) return cb(err);
cb(null, chosen.sort(function (a, b) {
return b.logProb - a.logProb;
}));
cb(null, chosen.sort((a, b) => b.logProb - a.logProb));
});
};

Expand Down
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -11,6 +11,7 @@
"through2": "~2.0.0"
},
"devDependencies": {
"babel-cli": "^6.24.1",
"codecov.io": "^0.1.6",
"istanbul": "^0.4.0",
"memdb": "~1.0.1",
Expand All @@ -23,7 +24,8 @@
"prepublish": "babel index.js --out-file index-es5.js"
},
"pre-commit": [
"test", "prepublish"
"test",
"prepublish"
],
"repository": {
"type": "git",
Expand Down
20 changes: 10 additions & 10 deletions test/test.js
Expand Up @@ -12,8 +12,8 @@ tape('postive / negative', function (t) {
t.notOk(err, 'no err')
nb.train('negative', 'terrible, shitty thing. Damn. Sucks!!', err => {
t.notOk(err, 'no err')
nb.classify('awesome, cool, amazing!! Yay.', (err, category) => {
t.same(category, 'positive', 'should be positive')
nb.classify('awesome, cool, amazing!! Yay.', (err, out) => {
t.same(out.category, 'positive', 'should be positive')
t.end()
})
})
Expand All @@ -30,8 +30,8 @@ tape('spam / not spam', function (t) {
t.notOk(err, 'no err')
nb.train('not spam', 'Some more strings that are not spam!', err => {
t.notOk(err, 'no err')
nb.classify('Great opportunity in Nigeria!', (err, category) => {
t.same(category, 'spam', 'should be spam')
nb.classify('Great opportunity in Nigeria!', (err, out) => {
t.same(out.category, 'spam', 'should be spam')
t.end()
})
})
Expand All @@ -50,8 +50,8 @@ tape('train with promise', (t) => {
return nb.trainAsync('negative', 'terrible, shitty thing. Damn. Sucks!!');
})
.then(function () {
return nb.classify('awesome, cool, amazing!! Yay.', (err, category) => {
t.same('positive', category, 'should be positive')
return nb.classify('awesome, cool, amazing!! Yay.', (err, out) => {
t.same('positive', out.category, 'should be positive')
t.end()
})
})
Expand All @@ -67,8 +67,8 @@ tape('que all train promises', (t) => {
];

q.all(thingsToDo).then(() => {
return nb.classify('awesome, cool, amazing!! Yay.', (err, category) => {
t.same('positive', category, 'should be positive')
return nb.classify('awesome, cool, amazing!! Yay.', (err, out) => {
t.same('positive', out.category, 'should be positive')
t.end()
})
})
Expand All @@ -85,8 +85,8 @@ tape('classify can be used as promise', (t) => {

q.all(thingsToDo)
.then(() => nb.classifyAsync('awesome, cool, amazing!! Yay.'))
.then((category) => {
t.same('positive', category, 'should be positive')
.then((out) => {
t.same('positive', out.category, 'should be positive')
t.end()
})
})
Expand Down

0 comments on commit 98b05d4

Please sign in to comment.