This repository has been archived by the owner on May 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
59 lines (51 loc) · 1.7 KB
/
index.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
const p = require('barnard59')
const path = require('path')
function convertCsvw (filename) {
const filenameInput = 'input/' + filename
const filenameMetadata = filenameInput + '-metadata.json'
const filenameOutput = 'target/' + path.basename(filename, '.csv') + '.nt'
return p.rdf.dataset().import(p.file.read(filenameMetadata).pipe(p.jsonld.parse())).then((metadata) => {
return p.run(p.file.read(filenameInput)
.pipe(p.csvw.parse({
baseIRI: 'file://' + filename,
metadata: metadata
}))
.pipe(p.filter((quad) => {
return typeof quad.object.value !== 'undefined'
}))
.pipe(p.map((quad) => {
let subject = quad.subject
let predicate = quad.predicate
let object = quad.object
let quads = []
if (subject.termType === 'NamedNode' && subject.value.startsWith('http://lod.opentransportdata.swiss/rollingStock/')) {
const newUri = subject.value.replace(/(%20|-)/g, '/')
subject = p.rdf.namedNode(newUri)
}
if (predicate.value === 'http://purl.org/dc/terms/identifier') {
if (object.value.length < 5) {
console.error('Wrong didok number: ' + predicate.value)
}
}
return p.rdf.quad(subject, predicate, object)
}))
.pipe(p.ntriples.serialize())
.pipe(p.file.write(filenameOutput)))
})
}
const filenames = [
'didok.csv',
'rollmaterial.csv'
]
p.run(() => {
p.shell.mkdir('-p', 'target/')
}).then(() => {
return p.Promise.serially(filenames, (filename) => {
console.log('convert: ' + filename)
return convertCsvw(filename)
})
}).then(() => {
console.log('done')
}).catch((err) => {
console.error(err.stack)
})