-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.coffee
More file actions
67 lines (60 loc) · 1.9 KB
/
main.coffee
File metadata and controls
67 lines (60 loc) · 1.9 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
#!/usr/bin/env node
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
when "false" then value = false
when "true" then value = true
if Array.isArray(JSON.parse(value)) then value = JSON.parse(value)
options[key] = value
i += 2
extractConcepts = (corpus) ->
res = BPromise.all(corpus.map((doc) => metaMap.getConcepts(doc, options))).then((data) =>
console.log(data)
outputJSON = if program.pretty then JSON.stringify(data, null, 2) else JSON.stringify(data)
if program.output
fs.writeFileSync(program.output, outputJSON)
else
console.log(outputJSON)
)
corpus
delim = program.delim
if program.list
delim = delim or ";"
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
when "text/plain"
delim = delim or " "
corpus = String(data).replace(/\r\n?/g, "\n").split(delim).clean("")
extractConcepts(corpus)
when "text/csv"
csv.parse(String(data), (err, output) =>
corpus = output.map( (d) => d[0] )
extractConcepts(corpus)
)