Skip to content

Commit 03475a6

Browse files
committed
fix svmlinear - use execfilesync instead of execsync
1 parent 92c045d commit 03475a6

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

classifiers/svm/SvmLinear.js

+11-16
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@
1010
* @since 2013-09-09
1111
*
1212
* @param opts options: <ul>
13-
* <li>learn_args - a string with arguments for liblinear_train
13+
* <li>learn_args - an array with arguments for liblinear_train
1414
* <li>model_file_prefix - prefix to path to model file (optional; the default is to create a temporary file in the system temp folder).
1515
* <li>bias - constant (bias) factor (default: 1).
1616
* <li>multiclass - if true, the 'classify' function returns an array [label,score]. If false (default), it returns only a score.
1717
*/
1818

19-
var util = require('util')
20-
, child_process = require('child_process')
21-
, exec = require('child_process').exec
19+
var child_process = require('child_process')
2220
, fs = require('fs')
2321
, svmcommon = require('./svmcommon')
2422
, _ = require('underscore')._
@@ -27,7 +25,7 @@
2725

2826

2927
function SvmLinear(opts) {
30-
this.learn_args = opts.learn_args || "";
28+
this.learn_args = opts.learn_args || [];
3129
this.model_file_prefix = opts.model_file_prefix || null;
3230
this.bias = opts.bias || 1.0;
3331
this.multiclass = opts.multiclass || false;
@@ -45,7 +43,7 @@ function SvmLinear(opts) {
4543

4644
SvmLinear.isInstalled = function() {
4745
try {
48-
var result = child_process.execSync('liblinear_train .');
46+
child_process.execFileSync('liblinear_train .');
4947
} catch (err) {
5048
return false
5149
}
@@ -85,22 +83,18 @@ SvmLinear.prototype = {
8583
this.allLabels = _(dataset).map(function(datum){return datum.output});
8684
this.allLabels = _.uniq(_.flatten(this.allLabels))
8785

88-
// dataset = _.map(dataset, function(datum){
89-
// datum.output = this.allLabels.indexOf(datum.output)
90-
// return datum });
91-
9286
if (this.allLabels.length==1) // a single label
9387
return;
94-
//console.log(util.inspect(dataset,{depth:1}));
9588
if (this.debug) console.log("trainBatch start");
9689
var learnFile = svmcommon.writeDatasetToFile(
9790
dataset, this.bias, /*binarize=*/false, this.model_file_prefix+"_"+this.timestamp, "SvmLinear", FIRST_FEATURE_NUMBER);
9891
var modelFile = learnFile.replace(/[.]learn/,".model");
9992

100-
var command = this.train_command+" "+this.learn_args+" "+learnFile + " "+modelFile;
101-
console.log("running "+command);
93+
var command = this.train_command
94+
var args = this.learn_args.concat([learnFile, modelFile])
95+
console.log("running "+command+" "+args.join(" "));
10296

103-
var result = child_process.execSync(command);
97+
var result = child_process.execFileSync(command, args);
10498
if (result.code>0) {
10599
console.dir(result);
106100
console.log(fs.readFileSync(learnFile, 'utf-8'));
@@ -143,9 +137,10 @@ SvmLinear.prototype = {
143137
var testFile = svmcommon.writeDatasetToFile(
144138
trainset, this.bias, /*binarize=*/false, "/tmp/test_"+this.timestamp, "SvmLinear", FIRST_FEATURE_NUMBER);
145139

146-
var command = this.test_command+" "+testFile + " " + this.modelFileString + " /tmp/out_" + this.timestamp;
140+
var command = this.test_command
141+
var args = [testFile, this.modelFileString, "/tmp/out_" + this.timestamp];
147142

148-
var output = child_process.execSync(command)
143+
var output = child_process.execFileSync(command, args)
149144
console.log(command)
150145

151146
var result = fs.readFileSync("/tmp/out_" + this.timestamp, "utf-8").split("\n")

0 commit comments

Comments
 (0)