1

I am constructing a small knowledge graph from triples of strings using rdflib. A typical triple would look like: "Bob" "went" "home", and I am adding them to my graph as shown below (I know I should be using standard objects and namespaces, but this is an experiment to construct the most "barebones" graph that I can):

for s, p, o in triples:
    g.add((Literal(s), Literal(p), Literal(o)))

I am attempting to query such a graph using SPARQL, and my query for extracting "Bob" from the above triple looks like:

q = """
       SELECT ?s 
       WHERE { (?s) %s Literal("home"). }
    """ % (Literal('went'))

This gives me the error below, which tells me that the query is malformed:

ParseException: Expected {SelectQuery | ConstructQuery | DescribeQuery | AskQuery}, found 'w'  (at char 23), (line:1, col:24)

I have tried plugging in the actual strings (e.g. "went" instead of Literal("went")), but that doesn't work either. Several posts like this answer and this answer address how to match literals, but that does not seem to help.

So my question is, is it possible to use Literals or simple strings as predicates in SPARQL, and if so, how? Any help would be much appreciated.

Joel Oduro-Afriyie
  • 1,563
  • 14
  • 13
  • 1
    that's not possible, please have a look at the SPARQL specs: https://www.w3.org/TR/sparql11-query/#defn_TriplePattern – UninformedUser Nov 17 '20 at 20:26
  • 2
    It's by the way also not possible in RDF to use literals in subject or predicate position: https://www.w3.org/TR/rdf11-concepts/#section-triples - if rdflib doesn't want to follow the RDF specs then you have to use rdflib specific operations to work on those rdflib specific (non valid RDF) graph, but it's not possible via standard SPARQL – UninformedUser Nov 17 '20 at 20:27
  • Ah, that is very helpful! Thank you @UninformedUser. I will make more use of the W3C specifications! – Joel Oduro-Afriyie Nov 17 '20 at 20:43

0 Answers0