-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.js
More file actions
93 lines (79 loc) · 2.44 KB
/
main.js
File metadata and controls
93 lines (79 loc) · 2.44 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
var BPromise, corpus, data, delim, extractConcepts, fs, i, key, metaMap, mime, mime_type, options, program, value;
program = require('commander');
mime = require('mime');
fs = require('fs');
metaMap = require('./lib/metaMap');
BPromise = require('bluebird');
/*
Command-Line-Interface:
*/
program.version('0.1.1').option('-i, --input [value]', 'Load data from disk').option('-l, --list <items>', 'A list of input texts').option('-o, --output [value]', 'Write results to file').option('-d, --delim [value]', 'Delimiter to split text into documents').option('-p, --pretty', 'Pretty print of JSON output').parse(process.argv);
options = {};
if (program.args.length > 0) {
if (program.args.length % 2 === 1) {
throw new Error("Wrong number of supplied arguments (always pass key-value pairs)");
} else {
i = 0;
while (i < program.args.length) {
key = program.args[i];
value = program.args[i + 1];
switch (value) {
case "false":
value = false;
break;
case "true":
value = true;
}
if (Array.isArray(JSON.parse(value))) {
value = JSON.parse(value);
}
options[key] = value;
i += 2;
}
}
}
extractConcepts = function(corpus) {
var res;
return res = BPromise.all(corpus.map((function(_this) {
return function(doc) {
return metaMap.getConcepts(doc, options);
};
})(this))).then((function(_this) {
return function(data) {
var outputJSON;
console.log(data);
outputJSON = program.pretty ? JSON.stringify(data, null, 2) : JSON.stringify(data);
if (program.output) {
return fs.writeFileSync(program.output, outputJSON);
} else {
return console.log(outputJSON);
}
};
})(this));
};
corpus;
delim = program.delim;
if (program.list) {
delim = delim || ";";
corpus = program.list.split(delim);
extractConcepts(corpus);
} else if (program.input) {
data = fs.readFileSync(program.input);
mime_type = mime.lookup(program.input);
switch (mime_type) {
case "text/plain":
delim = delim || " ";
corpus = String(data).replace(/\r\n?/g, "\n").split(delim).clean("");
extractConcepts(corpus);
break;
case "text/csv":
csv.parse(String(data), (function(_this) {
return function(err, output) {
corpus = output.map(function(d) {
return d[0];
});
return extractConcepts(corpus);
};
})(this));
}
}