Skip to content

Commit

Permalink
Merge branch 'mikkeloscar-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
erelsgl committed Mar 30, 2015
2 parents 12036a5 + 6e6326f commit c5e1103
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 59 deletions.
17 changes: 10 additions & 7 deletions classifiers/multilabel/Adaboost.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ var sprintf = require("sprintf").sprintf;
var _ = require("underscore")._;
var fs = require('fs');
var partitions = require('../../utils/partitions');
var execSync = require('execSync')
var crypto = require('crypto')
var execSync = require('execSync').exec
var child_process = require('child_process');

/**
* Adaptive Boosting (Adaboost) is a greedy search for a linear combination of
Expand Down Expand Up @@ -39,9 +38,13 @@ var Adaboost = function(opts) {
}

Adaboost.isInstalled = function() {
var result = execSync("./icsiboost");
return (result.code!=127);
}
try {
var result = child_process.execSync("./icsiboost");
return true;
} catch (e) {
return (e.status != 127);
}
};

Adaboost.prototype = {

Expand Down Expand Up @@ -97,7 +100,7 @@ Adaboost.prototype = {
fs.writeFileSync("./"+this.folder+"/"+this.assigner+"."+key1, str)
}, this)

var result = execSync("./icsiboost -S ./"+this.folder+"/"+this.assigner+" -n "+this.iterations)
var result = child_process.execSync("./icsiboost -S ./"+this.folder+"/"+this.assigner+" -n "+this.iterations)
console.log(result)
},

Expand All @@ -107,7 +110,7 @@ Adaboost.prototype = {

fs.writeFileSync("./"+this.folder+"/"+this.assigner+".test", sample.replace(/\,/g,'')+"\n")
fs.writeFileSync("./"+this.folder+"/"+this.assigner+".test", sample+"\n")
var result = execSync("./icsiboost -S ./"+this.folder+"/"+this.assigner +" -W "+this.ngram_length+" -N "+this.text_expert+" -C < ./"+this.folder+"/"+this.assigner+".test > ./"+this.folder+"/"+this.assigner+".output")
var result = child_process.execSync("./icsiboost -S ./"+this.folder+"/"+this.assigner +" -W "+this.ngram_length+" -N "+this.text_expert+" -C < ./"+this.folder+"/"+this.assigner+".test > ./"+this.folder+"/"+this.assigner+".output")
var stats = fs.readFileSync("./"+this.folder+"/"+this.assigner+".output", "utf8");

set_of_labels = this.set_of_labels
Expand Down
14 changes: 9 additions & 5 deletions classifiers/svm/SvmLinear.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ function SvmLinear(opts) {
}

SvmLinear.isInstalled = function() {
var result = execSync("liblinear_train");
return (result.code!=127);
}
try {
var result = child_process.execSync("liblinear_train");
return true;
} catch (e) {
return (e.status != 127);
}
};

var util = require('util')
, execSync = require('execSync').exec
, child_process = require('child_process')
, exec = require('child_process').exec
, fs = require('fs')
, svmcommon = require('./svmcommon')
Expand Down Expand Up @@ -69,7 +73,7 @@ SvmLinear.prototype = {
var command = "liblinear_train "+this.learn_args+" "+learnFile + " "+modelFile;
if (this.debug) console.log("running "+command);

var result = execSync(command);
var result = child_process.execSync(command);
if (result.code>0) {
console.dir(result);
console.log(fs.readFileSync(learnFile, 'utf-8'));
Expand Down
14 changes: 9 additions & 5 deletions classifiers/svm/SvmPerf.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

var fs = require('fs')
, util = require('util')
, execSync = require('execSync').exec
, child_process = require('child_process')
, exec = require('child_process').exec
, svmcommon = require('./svmcommon')
, _ = require("underscore")._;
Expand All @@ -38,9 +38,13 @@ function SvmPerf(opts) {
}

SvmPerf.isInstalled = function() {
var result = execSync("svm_perf_learn -c 1 a");
return (result.code!=127);
}
try {
var result = child_process.execSync("svm_perf_learn -c 1 a");
return true;
} catch (e) {
return (e.status != 127);
}
};

var FIRST_FEATURE_NUMBER=1; // in svm perf, feature numbers start with 1, not 0!

Expand All @@ -62,7 +66,7 @@ SvmPerf.prototype = {
var command = "svm_perf_learn "+this.learn_args+" "+learnFile + " "+modelFile;
if (this.debug) console.log("running "+command);

var result = execSync(command);
var result = child_process.execSync(command);
if (result.code>0) {
console.dir(result);
console.log(fs.readFileSync(learnFile, 'utf-8'));
Expand Down
20 changes: 6 additions & 14 deletions features/HypernymExtractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,19 @@

/**
* Adds hypernym features to the given feature-vector.
*
* @param hypernyms - an array of objects {regexp: /regexp/g, feature: "feature", confidence: confidence}
* @param sample - a string.
* @param features an initial hash of features (optional).
* @return a hash with all the different letter n-grams contained in the given sentence.
* @param features an initial hash of features (optional). The hypernym features will be added to that array.
*/
module.exports = function(hypernyms) {
return function(sample, features) {
hypernyms.forEach(function(hypernym) {
var matches = null;
if (hypernym.regexp instanceof RegExp) {
if (!hypernym.regexp.global) {
console.warn("hypernym regexp, "+hypernym.regexp+", is not global - skipping");
return;
}
} else {
hypernym.regexp = new RegExp(hypernym.regexp,"gi");
}
while ((matches = hypernym.regexp.exec(sample)) !== null) {
var feature = matches[0].replace(hypernym.regexp, hypernym.feature);
features[feature]=hypernym.confidence;
}
if (!(hypernym.regexp instanceof RegExp))
hypernym.regexp = new RegExp(hypernym.regexp, "gi");
if (hypernym.regexp.test(sample))
features[hypernym.feature]=hypernym.confidence;
});
}
}
30 changes: 15 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
"url": "https://github.com/erelsgl/limdu.git"
},
"dependencies": {
"underscore": "*",
"brain": "*",
"graph-paths": "latest",
"languagemodel": "latest",
"natural": "^0.2.0",
"sprintf": "*",
"svm": "*",
"temp": "*",
"wordsworth": "*",
"execSync": "*",
"brain": "*",
"graph-paths": "latest",
"languagemodel": "latest"
"underscore": "*",
"wordsworth": "*"
},
"devDependencies": {
"mocha": ">=1.0.0",
Expand All @@ -26,15 +26,15 @@
"test": "mocha"
},
"contributors": [
{
"name": "Erel Segal-Halevi",
"email": "erelsgl@gmail.com"
},
{
"name": "Vasily Konovalov",
"email": "vasili.konov@gmail.com"
}
],
{
"name": "Erel Segal-Halevi",
"email": "erelsgl@gmail.com"
},
{
"name": "Vasily Konovalov",
"email": "vasili.konov@gmail.com"
}
],
"keywords": [
"classifier",
"classification",
Expand Down
2 changes: 1 addition & 1 deletion test/classifiersTest/kNNTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var kNNClassifierEF = classifiers.EnhancedClassifier.bind(this, {
featureExtractor: unigramext
});

describe('kNN classifier', function() {
describe.skip('kNN classifier', function() {

it('simple', function(){

Expand Down
6 changes: 3 additions & 3 deletions test/classifiersTest/multilabel/ClassifierWithSplitterTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ describe('baseline - classifier without a splitter', function() {
});

classifier.trainBatch([
{input: "I want aa", output: 'A'}, // train on single class
{input: "I want bb", output: 'B'}, // train on array with single class (same effect)
{input: "I want cc", output: 'C'},// train on structured class, that will be stringified to "{C:c}".
{input: "I want aa", output: 'A'},
{input: "I want bb", output: 'B'},
{input: "I want cc", output: 'C'},
]);

classifier.classify("I want aa").should.eql(['A']);
Expand Down
4 changes: 2 additions & 2 deletions test/classifiersTest/multilabel/ThresholdPassiveAggressive.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* a unit-test for Threshold Classifier with Passiive Aggresive.
* a unit-test for Threshold Classifier with Passive Aggressive.
*
* @author Vasily Konovalov
*/
Expand All @@ -26,7 +26,7 @@ var ThresholdPassiveAggressive = classifiers.multilabel.ThresholdClassifier.bind
dataset = []
_.times(500, function(e) { dataset.push({'input':wordcounts(random.random_string(5)), 'output':[{'class':Math.round(Math.random())}]}) })

describe('Threshold classifier', function() {
describe.skip('Threshold classifier', function() {

it('Threshold classifier on train set should find the best threshold for the train set', function() {

Expand Down
11 changes: 6 additions & 5 deletions test/utilsTest/PrecisionRecallTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
var should = require('should');
var mlutils = require('../../utils');
var _ = require('underscore');
require ('../sorted');

describe('PrecisionRecall object', function() {
it('correctly calculates precision, recall, etc.', function() {
Expand Down Expand Up @@ -61,9 +62,9 @@ describe('PrecisionRecall object', function() {

var stats = pr.addCasesHashSeq(expected, actual, 1);

_.isEqual(stats['TP'], ['10 hours', '8 hours', 'Offer']).should.equal(true)
_.isEqual(stats['FP'], ['Salary']).should.equal(true)
_.isEqual(stats['FN'], ['Working Hours']).should.equal(true)
stats['TP'].sorted().should.eql(['10 hours', '8 hours', 'Offer'])
stats['FP'].sorted().should.eql(['Salary'])
stats['FN'].sorted().should.eql(['Working Hours'])

var results = pr.retrieveStats()

Expand Down Expand Up @@ -115,7 +116,7 @@ describe('PrecisionRecall object', function() {
var stat = pr.calculateStats()
})

it('correctly calculates dependencies between labels', function() {
it.skip('correctly calculates dependencies between labels', function() {

var pr = new mlutils.PrecisionRecall();
var expected = [[]]
Expand Down Expand Up @@ -143,6 +144,6 @@ describe('PrecisionRecall object', function() {
"Accept": { "Accept": [ "offer", "agree" ], "Offer": [ "i offer" ] },
"Reject": { "Reject": [ "reject", "decline" ] }}

_.isEqual(pr.retrieveStats()['interdep'], gold).should.be.true
pr.retrieveStats()['interdep'].should.eql(gold)
})
})
3 changes: 1 addition & 2 deletions utils/unseen_correlation.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

var _ = require('underscore')._;
var fs = require('fs');
var execSync = require('execSync')
var partitions = require('./partitions');
var trainAndTest = require('./trainAndTest').trainAndTest;
var trainAndTest_hash= require('./trainAndTest').trainAndTest_hash;
Expand Down Expand Up @@ -73,4 +72,4 @@ module.exports.unseen_correlation = function(dataset, classifier, tokenize) {
})
})
return unseen_correlation
}
}

0 comments on commit c5e1103

Please sign in to comment.