-
Notifications
You must be signed in to change notification settings - Fork 0
/
slidesToRDF.js
89 lines (84 loc) · 2.67 KB
/
slidesToRDF.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
78
79
80
81
82
83
84
85
86
87
88
89
var ndjson = require('ldjson-stream');
var fs = require('fs');
const prefixes = `
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix swV: <http://rdf.slidewiki.org/vocab/> .
@prefix swR: <https://slidewiki.org/slideview/> .
@prefix swTagR: <https://slidewiki.org/tag/> .
@prefix swUserR: <https://slidewiki.org/user/> .
@prefix swDeckR: <https://slidewiki.org/deck/> .
@prefix prv: <http://purl.org/net/provenance/ns#> .
`;
//write prefixes
console.log(prefixes);
fs.createReadStream('data/slides.txt')
.pipe(ndjson.parse({strict: false}))
.on('data', function(obj) {
convertToRDF(obj);
})
.on('error', function(obj) {
console.log('#######Error#########');
});
//list of selected properties
const selected = [
'_id',
'user',
'timestamp',
'lastUpdate',
'license',
'contributors', //esp
'revisions' //esp
];
const idField = '_id';
function convertToRDF(obj) {
//print turtle
let id = obj[idField];
console.log('#######'+id+'#########');
console.log(`
swR:${id} a swV:Slide .
`);
for(let prop in obj){
if(selected.indexOf(prop) !== -1){
//custom properties
if(prop === 'user'){
console.log(`swR:${id} prv:createdBy swUserR:${obj[prop]} .`);
continue;
}
if(prop === 'contributors'){
obj[prop].forEach((item)=>{
console.log(`swR:${id} swV:hasContibutor swUserR:${item.user} .`);
});
continue;
}
if(prop === 'revisions'){
obj[prop].forEach((item)=>{
let dt = new Date(item.timestamp) ;
console.log(`
swR:${id} swV:hasRevision swR:${id}-${item.id} .
swR:${id}-${item.id} a swV:SlideRevision ;
swV:title """${JSON.stringify(item.title)}""" ;
swV:timestamp """${item.timestamp}""" ;
swV:timestampYear "${dt.getFullYear()}" ;
swV:timestampMonth "${dt.getMonth() + 1}" ;
swV:timestampDay "${dt.getDate()}" ;
swV:timestampDate "${dt.toLocaleDateString()}" ;
swV:content """${encodeURIComponent(item.content)}""" ;
swV:speakernotes """${item.speakernotes ? encodeURIComponent(item.speakernotes) : '-'}""" ;
prv:createdBy swUserR:${item.user} .`);
});
continue;
}
//additional triples
if(prop === 'timestamp'){
if(obj[prop]){
let dt = new Date(obj[prop]) ;
console.log(`swR:${id} swV:timestampYear "${dt.getFullYear()}" .`);
console.log(`swR:${id} swV:timestampMonth "${dt.getMonth() + 1}" .`);
console.log(`swR:${id} swV:timestampDay "${dt.getDate()}" .`);
console.log(`swR:${id} swV:timestampDate "${dt.toLocaleDateString()}" .`);
}
}
console.log(`swR:${id} swV:${prop} """${obj[prop] ? obj[prop] : '-'}""" .`);
}
}
}