Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 53 lines (47 sloc) 1.7 KB
import argparse
import urllib2
import json
from urllib import urlencode
from datetime import date
def dbpedia(query, binding=None):
'''Query the DBpedia SPARQL endpoint'''
data = urlencode({'format': 'application/sparql-results+json', 'query': query})
result = json.loads(urllib2.urlopen('', data).read())\
if binding is not None:
result = [value[binding]['value'] for value in result]
return result
# Get the birth date from the command line arguments.
parser = argparse.ArgumentParser()
args = parser.parse_args()
birth_date = date(*map(int, args.d.split('-')))
# Select all people whose death date is identical to the provided birth date,
# ordered by the birth date.
query = '''
PREFIX foaf: <>
PREFIX rdfs: <>
PREFIX dbpedia-owl: <>
SELECT ?person ?birth_date ?label
?person a foaf:Person ;
dbpedia-owl:deathDate ?death_date .
OPTIONAL {{ ?person rdfs:label ?label }}
OPTIONAL {{ ?person dbpedia-owl:birthDate ?birth_date }}
FILTER regex( ?death_date, '^{0}' )
FILTER langMatches( lang(?label), "EN" )
ORDER BY ?birth_date'''.format(birth_date)
for p in dbpedia(query):
person = {'uri': p['person']['value'],
'birth_date': date(*map(int, p['birth_date']['value'].split('-')))}
query = '''
PREFIX foaf: <>
SELECT ?name
<{0}> foaf:name ?name .
for name in dbpedia(query, 'name'):
print name
print u'{birth_date} <{uri}>'.format(**person)