-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusersToRDF.js
62 lines (59 loc) · 1.63 KB
/
usersToRDF.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
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/user/> .
`;
//write prefixes
console.log(prefixes);
fs.createReadStream('data/users.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',
'username',
'forename',
'surname',
'country',
'picture',
'description',
'organization',
'registered'
];
const idField = '_id';
//todo: list of mapping to other vocabs
const mappings ={
'picture': 'foaf:picture'
}
function convertToRDF(obj) {
//print turtle
let id = obj[idField];
console.log(`swR:${id} a swV:User .`);
for(let prop in obj){
if(selected.indexOf(prop) !== -1){
if(prop === 'picture'){
console.log(`swR:${id} swV:${prop} """${obj[prop]}""" .`);
continue;
}
//additional triples
if(prop === 'registered'){
if(obj[prop]){
let dt = new Date(obj[prop]) ;
console.log(`swR:${id} swV:registeredYear "${dt.getFullYear()}" .`);
console.log(`swR:${id} swV:registeredMonth "${dt.getMonth() + 1}" .`);
console.log(`swR:${id} swV:registeredDay "${dt.getDate()}" .`);
console.log(`swR:${id} swV:registeredDate "${dt.toLocaleDateString()}" .`);
}
}
console.log(`swR:${id} swV:${prop} """${obj[prop] ? obj[prop] : '-'}""" .`);
}
}
console.log('################');
}