Forked from simonw/wikipedia_abstract_from_dbpedia.py
Created
January 21, 2010 15:19
-
-
Save lprsd/282868 to your computer and use it in GitHub Desktop.
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
from xml.etree import ElementTree as ET | |
import urllib | |
dbpedia_endpoint = "http://dbpedia.org/sparql?" | |
ns = {'ns': '{http://www.w3.org/2005/sparql-results#}'} | |
result_xpath ='%(ns)sresults/%(ns)sresult/%(ns)sbinding/%(ns)sliteral' % ns | |
def wikipedia_abstract_from_dbpedia(dbpedia_key): | |
sparql = """ | |
SELECT ?abstract | |
WHERE { | |
{ <http://dbpedia.org/resource/%s> <http://dbpedia.org/property/abstract> ?abstract . | |
FILTER langMatches( lang(?abstract), 'en') } | |
} | |
""" % dbpedia_key | |
url = dbpedia_endpoint + urllib.urlencode({'query': sparql}) | |
et = ET.parse(urllib.urlopen(url)) | |
return et.find(result_xpath).text |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment