0

I am starting out learning about SPARQL and rdflib. I stumbled upon this simple example online:

import rdflib
# Now we create a graph, a representaiton of the ontology
g = rdflib.Graph()

# Now define the key words that we will use (the edge weights of the graph)
has_border_with = rdflib.URIRef('http://www.example.org/has_border_with')
located_in = rdflib.URIRef('http://www.example.org/located_in')

# define the things - base level objects that will be the nodes
# In this case first we have countries
germany = rdflib.URIRef('http://www.example.org/country1')
france = rdflib.URIRef('http://www.example.org/country2')
china = rdflib.URIRef('http://www.example.org/country3')
mongolia = rdflib.URIRef('http://www.example.org/country4')

# then we have continents
europa = rdflib.URIRef('http://www.example.org/part1')
asia = rdflib.URIRef('http://www.example.org/part2')

# Having defined the things and the edge weights, now assemble the graph
g.add((germany,has_border_with,france))
g.add((china,has_border_with,mongolia))
g.add((germany,located_in,europa))
g.add((france,located_in,europa))
g.add((china,located_in,asia))
g.add((mongolia,located_in,asia))

# now execute a sparql query to search as an example
q = "select ?country where { ?country <http://www.example.org/located_in> <http://www.example.org/part1> }"
x = g.query(q)
print type(x)
print list(x)

This works fine, but I dont want to retrieve the URIs, I want the actual country names: Germany and France. How do I reference the names from the query URIs?

MadProgrammer
  • 423
  • 1
  • 5
  • 17
  • The question doesn't really match the title. The title is asking about variable madness. The question is asking about (probably) other properties of the values of the variables. – Joshua Taylor Sep 01 '16 at 01:30
  • Thanks for pointing me to this post. A key difference and the reason I did not find the post you suggest is the fact that I am working in RDFLIB in python. Let me try some of this and see if it will work in that library. – MadProgrammer Sep 01 '16 at 12:22
  • @MadProgrammer I don't see why not - SPARQL is SPARQL (or at least - it should be) :) – Jeen Broekstra Sep 05 '16 at 21:32

0 Answers0