Created
September 7, 2015 20:56
-
-
Save rxw1/b93f8453a4ba61582d18 to your computer and use it in GitHub Desktop.
Query dbpedia
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fetch from 'node-fetch'; | |
let prefix = ` | |
PREFIX : <http://dbpedia.org/resource/> | |
PREFIX dbpedia: <http://dbpedia.org/> | |
PREFIX dbpedia2: <http://dbpedia.org/property/> | |
PREFIX dc: <http://purl.org/dc/elements/1.1/> | |
PREFIX foaf: <http://xmlns.com/foaf/0.1/> | |
PREFIX owl: <http://www.w3.org/2002/07/owl#> | |
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> | |
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> | |
PREFIX skos: <http://www.w3.org/2004/02/skos/core#> | |
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> | |
` | |
const person = '<http://dbpedia.org/ontology/Person>'; | |
function ontology(val) { | |
return `?person <http://dbpedia.org/ontology/${val}> ?${val}.`; | |
} | |
let query = `SELECT * WHERE { | |
?person a ${person} . | |
${ontology('nationality')} | |
${ontology('birthDate')} | |
${ontology('birthPlace')} | |
${ontology('deathDate')} | |
${ontology('deathPlace')} | |
${ontology('almaMater')} | |
${ontology('influenced')} | |
?influenced a ${person}. | |
}` | |
function getUrl(prefix, query, limit = 500) { | |
const baseurl = 'http://dbpedia.org'; | |
const urlpath = '/sparql'; | |
const params = { | |
'default-graph-uri': 'http://dbpedia.org', | |
query: encodeURIComponent([prefix, query, `LIMIT ${limit}`].join(' ')), | |
output: 'json' | |
}; | |
const paramString = Object.keys(params).map(x => `${x}=${params[x]}`).join('&'); // <3 | |
return `${baseurl}${urlpath}?${paramString}`; | |
} | |
fetch(getUrl(prefix, query)) | |
.then(response => response.json()) | |
.then(json => json.results) | |
.then(results => results.bindings) | |
.then(bindings => console.log(JSON.stringify(bindings, null, ' '))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
babel dbpedia.js | node