-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodejs_main.js
More file actions
226 lines (173 loc) · 6.16 KB
/
nodejs_main.js
File metadata and controls
226 lines (173 loc) · 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
* Copyright (c) 2015 TextGlass
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
var textglass = require('./js/textglass');
var textglasstest = require('./js/textglasstest');
textglass.debugLevel = 1;
var args = process.argv.slice(2);
textglass.debug(0, 'TextGlass Javascript Client ' + textglass.version);
var pattern;
var attribute;
var patternPatch;
var attributePatch;
var tests = [];
var testString;
var warmup;
var failure = false;
//PARSE THE COMMAND LINE
for(var i = 0; i < args.length; i++) {
var option = args[i];
if(option === '-h') {
printHelp();
process.exit(0);
} else if(option === '-p') {
if(pattern) {
throw 'pattern file already defined';
}
pattern = getParam(args, ++i, '-p file parameter missing');
} else if(option === '-a') {
if(attribute) {
throw 'attribute file already defined';
}
attribute = getParam(args, ++i, '-a file parameter missing');
} else if(option === '-pp') {
if(patternPatch) {
throw 'pattern patch file already defined';
}
patternPatch = getParam(args, ++i, '-pp file parameter missing');
} else if(option === '-ap') {
if(attributePatch) {
throw 'attribute patch file already defined';
}
attributePatch = getParam(args, ++i, '-ap file parameter missing');
} else if(option === '-t') {
tests.push(getParam(args, ++i, '-t file parameter missing'));
} else if(option.indexOf('-') !== 0 && !testString) {
testString = option;
} else if(option === '-w') {
warmup = getParam(args, ++i, '-w iterations missing');
} else if(option === '-q') {
textglass.debugLevel = 0;
} else if(option === '-v') {
textglass.debugLevel = 2;
} else if(option === '-vv') {
textglass.debugLevel = 3;
} else {
printHelp();
throw 'unknown option: ' + option;
}
}
if(!pattern) {
printHelp();
throw 'Pattern file required';
}
if(warmup > 0) {
runWarmup(warmup, pattern, patternPatch, attribute, attributePatch, tests);
}
textglass.debug(1, 'Pattern file: \'' + pattern + '\'');
if(patternPatch) {
textglass.debug(1, 'Pattern patch file: \'' + patternPatch + '\'');
}
if(attribute) {
textglass.debug(1, 'Attribute file: \'' + attribute + '\'');
}
if(attributePatch) {
textglass.debug(1, 'Attribute patch file: \'' + attributePatch + '\'');
}
//BUILD THE TEXTGLASS CLIENT
var start = Date.now();
var patternFile = require('./' + pattern);
var attributeFile = attribute ? require('./' + attribute) : undefined;
var patternPatchFile = patternPatch ? require('./' + patternPatch) : undefined;
var attributePatchFile = attributePatch ? require('./' + attributePatch) : undefined;
var result = textglass.loadObjects(patternFile, attributeFile, patternPatchFile, attributePatchFile);
var time = Date.now() - start;
if(!result || result.error) {
throw result ? result.msg : 'Unknown error';
}
textglass.debug(1, result.msg);
textglass.debug(0, 'Domain load time: ' + time + 'ms');
//DO THE TESTS
for(var i = 0; i < tests.length; i++) {
var test = tests[i];
textglass.debug(1, 'Test file: \'' + test + '\'');
var testFile = require('./' + test);
var testResult = textglasstest.loadObject(testFile);
if(!testResult || testResult.error) {
failure = true;
}
if(testResult && testResult.msg) {
textglass.debug(0, testResult.msg);
}
}
if(testString) {
textglass.debug(1, 'Test string: \'' + testString + '\'');
var start = Date.now();
var testResult = textglass.domains[result.name].classify(testString);
var time = Date.now() - start;
textglass.debug(0, 'Test result:', testResult);
textglass.debug(0, 'Test time: ' + time + 'ms');
}
if(failure) {
throw 'One or more tests failed';
}
function printHelp() {
textglass.debug(0, 'Usage: ' + process.argv[1] + ' [OPTIONS] [STRING]\n');
textglass.debug(0, ' -p <file> load TextGlass pattern file (REQUIRED)');
textglass.debug(0, ' -a <file> load TextGlass attribute file');
textglass.debug(0, ' -pp <file> load TextGlass pattern patch file');
textglass.debug(0, ' -ap <file> load TextGlass attribute patch file');
textglass.debug(0, ' -t <file> load TextGlass test file');
textglass.debug(0, ' -h print help');
textglass.debug(0, ' -w <iterations> run warmup');
textglass.debug(0, ' -q quiet');
textglass.debug(0, ' -v verbose');
textglass.debug(0, ' -vv very verbose');
textglass.debug(0, ' STRING test string');
textglass.debug(0, '');
}
function getParam(args, pos, error) {
if(pos >= args.length) {
throw error;
} else if(!args[pos] || args[pos].indexOf('-') === 0) {
throw error;
} else {
return args[pos];
}
}
function runWarmup(warmup, pattern, patternPatch, attribute, attributePatch, tests) {
textglass.debug(0, 'Warmup ' + warmup + ' iterations(s)...');
var origDebug = textglass.debugLevel;
textglass.debugLevel = -1;
var iterations = 0;
while(iterations < warmup) {
var patternFile = require('./' + pattern);
var attributeFile = attribute ? require('./' + attribute) : undefined;
var patternPatchFile = patternPatch ? require('./' + patternPatch) : undefined;
var attributePatchFile = attributePatch ? require('./' + attributePatch) : undefined;
var result = textglass.loadObjects(patternFile, attributeFile, patternPatchFile, attributePatchFile);
for(var i = 0; i < tests.length; i++) {
var test = tests[i];
var testFile = require('./' + test);
textglasstest.loadObject(testFile);
}
delete textglass.domains[result.domain];
iterations++;
}
textglass.debugLevel = origDebug;
textglass.debug(0, 'Warmup completed');
}