Skip to content

Instantly share code, notes, and snippets.

@rxw1
Created September 7, 2015 20:56
Show Gist options
  • Save rxw1/b93f8453a4ba61582d18 to your computer and use it in GitHub Desktop.
Save rxw1/b93f8453a4ba61582d18 to your computer and use it in GitHub Desktop.
Query dbpedia
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, ' ')));
@rxw1
Copy link
Author

rxw1 commented Sep 7, 2015

babel dbpedia.js | node

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment