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.