10
10
* @since 2013-09-09
11
11
*
12
12
* @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
14
14
* <li>model_file_prefix - prefix to path to model file (optional; the default is to create a temporary file in the system temp folder).
15
15
* <li>bias - constant (bias) factor (default: 1).
16
16
* <li>multiclass - if true, the 'classify' function returns an array [label,score]. If false (default), it returns only a score.
17
17
*/
18
18
19
- var util = require ( 'util' )
20
- , child_process = require ( 'child_process' )
21
- , exec = require ( 'child_process' ) . exec
19
+ var child_process = require ( 'child_process' )
22
20
, fs = require ( 'fs' )
23
21
, svmcommon = require ( './svmcommon' )
24
22
, _ = require ( 'underscore' ) . _
27
25
28
26
29
27
function SvmLinear ( opts ) {
30
- this . learn_args = opts . learn_args || "" ;
28
+ this . learn_args = opts . learn_args || [ ] ;
31
29
this . model_file_prefix = opts . model_file_prefix || null ;
32
30
this . bias = opts . bias || 1.0 ;
33
31
this . multiclass = opts . multiclass || false ;
@@ -45,7 +43,7 @@ function SvmLinear(opts) {
45
43
46
44
SvmLinear . isInstalled = function ( ) {
47
45
try {
48
- var result = child_process . execSync ( 'liblinear_train .' ) ;
46
+ child_process . execFileSync ( 'liblinear_train .' ) ;
49
47
} catch ( err ) {
50
48
return false
51
49
}
@@ -85,22 +83,18 @@ SvmLinear.prototype = {
85
83
this . allLabels = _ ( dataset ) . map ( function ( datum ) { return datum . output } ) ;
86
84
this . allLabels = _ . uniq ( _ . flatten ( this . allLabels ) )
87
85
88
- // dataset = _.map(dataset, function(datum){
89
- // datum.output = this.allLabels.indexOf(datum.output)
90
- // return datum });
91
-
92
86
if ( this . allLabels . length == 1 ) // a single label
93
87
return ;
94
- //console.log(util.inspect(dataset,{depth:1}));
95
88
if ( this . debug ) console . log ( "trainBatch start" ) ;
96
89
var learnFile = svmcommon . writeDatasetToFile (
97
90
dataset , this . bias , /*binarize=*/ false , this . model_file_prefix + "_" + this . timestamp , "SvmLinear" , FIRST_FEATURE_NUMBER ) ;
98
91
var modelFile = learnFile . replace ( / [ . ] l e a r n / , ".model" ) ;
99
92
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 ( " " ) ) ;
102
96
103
- var result = child_process . execSync ( command ) ;
97
+ var result = child_process . execFileSync ( command , args ) ;
104
98
if ( result . code > 0 ) {
105
99
console . dir ( result ) ;
106
100
console . log ( fs . readFileSync ( learnFile , 'utf-8' ) ) ;
@@ -143,9 +137,10 @@ SvmLinear.prototype = {
143
137
var testFile = svmcommon . writeDatasetToFile (
144
138
trainset , this . bias , /*binarize=*/ false , "/tmp/test_" + this . timestamp , "SvmLinear" , FIRST_FEATURE_NUMBER ) ;
145
139
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 ] ;
147
142
148
- var output = child_process . execSync ( command )
143
+ var output = child_process . execFileSync ( command , args )
149
144
console . log ( command )
150
145
151
146
var result = fs . readFileSync ( "/tmp/out_" + this . timestamp , "utf-8" ) . split ( "\n" )
0 commit comments