--- a/src/hdalab/management/commands/query_dbpedia.py Sun Nov 16 18:09:42 2014 +0100
+++ b/src/hdalab/management/commands/query_dbpedia.py Sun Nov 16 19:35:12 2014 +0100
@@ -69,6 +69,12 @@
help='the tag to query'),
)
+ def query_dbpedia(self, query, fmt=N3):
+ endpoint = SPARQLWrapper(settings.DBPEDIA_URI_TEMPLATE % ( 'sparql', '' ), returnFormat=fmt)
+ endpoint.setQuery(query)
+ return Graph().parse(data=endpoint.queryAndConvert(), format=fmt)
+
+
def handle_noargs(self, **options):
self.style = no_style()
@@ -126,9 +132,6 @@
print "dbpedia query cancelled"
return
- #endpoint = SPARQLWrapper("http://dbpedia.org/sparql", returnFormat=RDF)
- endpoint = SPARQLWrapper(settings.DBPEDIA_URI_TEMPLATE % ( 'sparql', '' ), returnFormat=N3)
-
writer = None
for i,tag in enumerate(queryset):
writer = show_progress(i+1, count, tag.label, 50, writer)
@@ -145,27 +148,21 @@
labels = {}
thumbnail = None
with transaction.commit_on_success():
- endpoint.setQuery("select distinct ?y where {<%s> <http://dbpedia.org/ontology/abstract> ?y}" % (tag.dbpedia_uri))
- res_abstracts = Graph().parse(data=endpoint.queryAndConvert(), format='n3')
+ res_abstracts = self.query_dbpedia("select distinct ?y where {<%s> <http://dbpedia.org/ontology/abstract> ?y}" % (tag.dbpedia_uri), N3)
for _,_,o in res_abstracts.triples((None, URIRef('http://www.w3.org/2005/sparql-results#value'), None)):
abstracts[o.language] = (unicode(o), True)
logger.debug("Abstracts: %r" % abstracts)
- endpoint.setQuery("select distinct ?y where {<%s> <http://www.w3.org/2000/01/rdf-schema#label> ?y}" % (tag.dbpedia_uri))
- #res_labels = endpoint.queryAndConvert()
- res_labels = Graph().parse(data=endpoint.queryAndConvert(), format='n3')
+ res_labels = self.query_dbpedia("select distinct ?y where {<%s> <http://www.w3.org/2000/01/rdf-schema#label> ?y}" % (tag.dbpedia_uri), N3)
for _,_,o in res_labels.triples((None, URIRef('http://www.w3.org/2005/sparql-results#value'), None)):
labels[o.language] = (unicode(o), True)
logger.debug("Labels: %r" % labels)
- endpoint.setQuery("select distinct ?y where {<%s> <http://dbpedia.org/ontology/thumbnail> ?y} limit 1" % (tag.dbpedia_uri))
- res_thumbnails = Graph().parse(data=endpoint.queryAndConvert(), format='n3')
+ res_thumbnails = self.query_dbpedia("select distinct ?y where {<%s> <http://dbpedia.org/ontology/thumbnail> ?y} limit 1" % (tag.dbpedia_uri), N3)
for _,_,o in res_thumbnails.triples((None, URIRef('http://www.w3.org/2005/sparql-results#value'), None)):
thumbnail = unicode(o)
- #endpoint.setQuery('select distinct ?y where { <%s> ?p ?y . FILTER regex(?y, "^http://dbpedia.org/resource")}' % (tag.dbpedia_uri))
- endpoint.setQuery('select distinct ?y where { <%s> ?p ?y . FILTER regex(?y, "^%s")}' % (tag.dbpedia_uri, settings.DBPEDIA_URI_TEMPLATE % ( 'resource', '' )))
- res_links = Graph().parse(data=endpoint.queryAndConvert(), format='n3')
+ res_links = self.query_dbpedia('select distinct ?y where { <%s> ?p ?y . FILTER regex(?y, "^%s")}' % (tag.dbpedia_uri, settings.DBPEDIA_URI_TEMPLATE % ( 'resource', '' )), N3)
for _,_,o in res_links.triples((None, URIRef('http://www.w3.org/2005/sparql-results#value'), None)):
tagqs = Tag.objects.filter(dbpedia_uri=unicode(o))
if tagqs: