-
Notifications
You must be signed in to change notification settings - Fork 11
/
wdtaxonomy.js
executable file
·77 lines (69 loc) · 2.45 KB
/
wdtaxonomy.js
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
#!/usr/bin/env node
const program = require('./lib/program')
const { normalizeId } = require('wikidata-sdk')
const { queryTaxonomy, serializeTaxonomy, sparqlQueries } = require('./index.js')
const { pruneHierarchy } = require('./lib/jskos')
program
.version(require('./package.json').version)
.arguments('<id>')
.option('-b, --brief', 'omit counting instances and sites')
.option('-c, --children', 'get direct subclasses only')
.option('-d, --descr', 'include item descriptions')
.option('-R, --prune <criteria>', 'prune hierarchy (e.g. mappings)')
.option('-i, --instances', 'include instances')
.option('-I, --no-instancecount', 'omit counting instances')
.option('-L, --no-labels', 'omit all labels')
.option('-m, --mappings <ids>', 'mapping properties (e.g. P1709)')
.option('-P, --property <id>', 'hierarchy property (e.g. P279)')
.option('-r, --reverse', 'get superclasses instead')
.option('-S, --no-sitecount', 'omit counting sites')
.option('-t, --total', 'count total number of instances')
.description('extract taxonomies from Wikidata')
program.run({
formats: ['txt', 'csv', 'tsv', 'json', 'ndjson'],
serializer: serializeTaxonomy,
action: function (wid, env, error) {
wid = wid.replace(/^.*[^0-9A-Z]([QP][0-9]+)([^0-9].*)?$/i, '$1')
if (env.brief) {
env.instancecount = false
env.sitecount = false
}
try {
var id = normalizeId(wid)
} catch (err) {
error(1, 'invalid id: %s', wid)
}
const serializeOptions = {
chalk: env.chalk,
uris: env.uris,
instancecount: env.instancecount,
sitecount: env.sitecount
}
env.property = env.property || ''
var match = env.property.match(/^([pP]?([0-9]+))?([/,][pP]?([0-9]+))?/)
if (match) {
var qid = id.substr(0, 1) === 'Q'
env.property = [
'P' + (match[2] || (qid ? '279' : '1647')),
'P' + (match[4] || '31') // TODO: default value for properties (?)
]
} else {
error(1, 'property must be specified like P279 or P279,P31')
}
if (env.sparql) {
var queries = sparqlQueries(id, env)
env.out().write(queries.join('\n') + '\n')
} else {
queryTaxonomy(id, env)
.then(taxonomy => {
if (env.prune) {
pruneHierarchy(taxonomy, env.prune)
}
env.serialize(taxonomy, env.out(), serializeOptions)
})
.catch(e => {
error(2, env.verbose && e.stack ? e.stack : e.message)
})
}
}
})