Skip to content


To run this notebook in JupyterLab, load examples/ex4_0.ipynb

Running SPARQL queries

SPARQL is a standard query language for RDF. Note: there are also other approaches for queries, such SHACL the Shapes Constraint Language.

In any case, if you've worked with SQL queries before, SPARQL should seem quite familiar.


"If you build it, they will SPARQL"

First, let's define a small graph in RDF:

ttl_text = """
@prefix : <> .
@prefix foaf: <'> .

:peep0 a foaf:Person ;
    foaf:givenName "Alice" ;
    foaf:familyName "Nakamoto" ;
    foaf:phone <tel:+1.555.222.2222> ;
    foaf:mbox <mailto:[email protected]> .

:peep1 a foaf:Person ;
    foaf:givenName "Bob" ;
    foaf:familyName "Patel" ;
    foaf:phone <tel:+1.555.666.5150> ;
    foaf:mbox <mailto:[email protected]> .

:peep2 a foaf:Person ;
    foaf:givenName "Dhanya" ;
    foaf:familyName "O'Neill" ;
    foaf:phone <tel:+1.555.123.9876> ;
    foaf:mbox <mailto:[email protected]> .

Then construct a graph using rdflib:

from icecream import ic
import rdflib

g = rdflib.Graph()
g.parse(data=ttl_text, format="ttl")
<Graph identifier=N0cd5f736c22c4aa88bdab1d94cd01624 (<class 'rdflib.graph.Graph'>)>

Now we'll define a SPARQL query to extract the surnames and email addresses for each person in the graph, in descending order by surname:

sparql = """
    SELECT ?person ?surname ?email
    WHERE {
        ?person foaf:familyName ?surname .
        ?person foaf:mbox ?email .
    ORDER BY DESC(?surname)

Then run the SPARQL query and iterate over its result set:

for row in g.query(sparql):
ic| row.asdict(): {'email': rdflib.term.URIRef('mailto:[email protected]'),
                   'person': rdflib.term.URIRef(''),
                   'surname': rdflib.term.Literal('Patel')}
ic| row.asdict(): {'email': rdflib.term.URIRef('mailto:[email protected]'),
                   'person': rdflib.term.URIRef(''),
                   'surname': rdflib.term.Literal("O'Neill")}
ic| row.asdict(): {'email': rdflib.term.URIRef('mailto:[email protected]'),
                   'person': rdflib.term.URIRef(''),
                   'surname': rdflib.term.Literal('Nakamoto')}

To access the URLs and literals from those results in more readable form, use the n3() function with the graph's namespaces to extract formatted text from each term:

for row in g.query(sparql):
    person = row["person"].n3(g.namespace_manager)
    surname = eval(row["surname"].n3())
    email = row["email"].n3()

    ic(person, surname, email)
ic| person: ':peep1'
    surname: 'Patel'
    email: '<mailto:[email protected]>'
ic| person: ':peep2'
    surname: "O'Neill"
    email: '<mailto:[email protected]>'
ic| person: ':peep0'
    surname: 'Nakamoto'
    email: '<mailto:[email protected]>'

SPARQL queries with kglab abstractions

Now we'll load our medium size KG from the previous example. Note that an additional namespace with the nom: prefix gets added at this point. We'll use that in the following examples:

import kglab

namespaces = {
    "nom":  "",
    "wtm":  "",
    "ind":  "",
    "skos": "",

kg = kglab.KnowledgeGraph(
    name = "A recipe KG example based on",
    base_uri = "",
    namespaces = namespaces,

<kglab.kglab.KnowledgeGraph at 0x10d031bd0>

Identify recipes with special ingredients

Let's define a query based on SPARQL 1.1 to look for recipes that use eggs:

sparql = """
SELECT ?recipe ?definition ?time
      ?recipe rdf:type wtm:Recipe .
      ?recipe skos:definition ?definition .
      ?recipe wtm:hasIngredient ind:ChickenEgg .
      ?recipe wtm:hasCookTime ?time
  ORDER BY DESC(?time)

Let's take a look at the structure of this query. We can visualize it using kglab:

pyvis_graph = kg.visualize_query(sparql, notebook=True)



Now we'll run the query and iterate through its result set:

import pandas as pd
pd.set_option("max_rows", None)

df = kg.query_as_df(sparql)
recipe definition time
0 <> classic cooked eggnog 1 days 00:30:00
1 <> safe eggnog 0 days 12:15:00
2 <> mrs corbins butter cookies 0 days 06:15:00
3 <> rooster cookies sugar cookies 0 days 04:10:00
4 <> marti s homemade noodles 0 days 02:40:00
5 <> hungarian noodles homemade 0 days 02:00:00
6 <> 2 step pound cake for a kitchen aide mixer 0 days 01:50:00
7 <> crepes alton brown 0 days 01:40:00
8 <> wallis crepes 0 days 01:40:00
9 <> fannie farmer s pound cake 0 days 01:35:00
10 <> easy homemade noodles 0 days 01:30:00
11 <> basic dessert crepes batter 0 days 01:30:00
12 <> wonton wrappers 0 days 01:30:00
13 <> miss ellen s pound cake 0 days 01:30:00
14 <> all purpose dinner crepes batter 0 days 01:30:00
15 <> linda s perfect popovers 0 days 01:20:00
16 <> magic cake 0 days 01:20:00
17 <> norwegian krumkake 0 days 01:15:00
18 <> buttery pound cake 0 days 01:15:00
19 <> dane dumplings 0 days 01:15:00

That's interesting – notice the outliers based on cooking times, i.e., recipes for eggnog or special kinds of cookies and noodles. Some of these take hours or even days to prepare.

Using SPARQL queries and post-processing to create annotations

Let's begin to add annotations to the KG. In other words, we'll add descriptions to the recipes to help classify them based on an ontology.

We've created a brief controlled vocabulary in RDF called NOM in the dat/nom.ttl file, which provides part of a simple taxonomy for our recipes. Take a look at those definitions – mostly these are distinctions between two kind of flour-based recipes:

  • "pancakes" -- which use batter in their preparation
  • "noodles" -- which use kneading in their preparation

Let's include these definitions in our KG:

<kglab.kglab.KnowledgeGraph at 0x10d031bd0>

Now we'll query the KG, specifically looking for recipes that use flour, eggs, salt among their ingredients:

sparql = """
SELECT ?recipe ?definition
    ?recipe rdf:type wtm:Recipe .
    ?recipe skos:definition ?definition .
    ?recipe wtm:hasIngredient ind:ChickenEgg .
    ?recipe wtm:hasIngredient ind:AllPurposeFlour .
    ?recipe wtm:hasIngredient ind:Salt

While looping through a query result set, we can perform some post-processing which would have been quite difficult to express in the SPARQL query language, although relatively simple within a Python loop. For instance, we can compare two points for each recipe in the results:

  • Is the word noodle or its close synonyms in the recipe description?
  • Does the recipe use butter?

Our dat/nom.ttl vocabulary that we've just added to the KG distinguishes between a nom:Noodle and a nom:Pancake as two categories of flour-based recipes. Let's run this query to look through the flour-based recipes and determine whether we might be able to craft specific rules to distinguish between noodle-ish foods and pancake-ish foods. For example, using butter is rare among noodle recipes that also use eggs, though more common for pancakes.

tally = []

for row in kg.query(sparql):
    url = row.recipe
    recipe_name = row.definition

    # collect two flags to evaluate our noodle vs. pancake rules
    has_butter = (url, kg.get_ns("wtm").hasIngredient, kg.get_ns("ind").Butter) in kg.rdf_graph()
    sez_noodle =  any([x in recipe_name for x in ["noodle", "spaetzle", "dumpling", "pasta"]])
    tally.append(( has_butter, sez_noodle ))

    if not has_butter and not sez_noodle:
        print("{}\n {}\n {} {}\n".format(url, recipe_name, has_butter, sez_noodle))
 wonton wrappers made at home
 False False
 False False
 german pancakes  from the mennonite treasury of recipes
 False False
 yorkhire pudding aka popovers
 False False
 egg filling for sushi
 False False
 False False
 german pancake shreds  for clear soups
 False False
 single crepe
 False False
 False False
 jamie oliver s huge yorkshire puddings
 False False
 paula deen s popovers
 False False
 uncle don s swedish pancakes
 False False
 never fail popovers
 False False
 old fashioned sponge cake
 False False
 no beat popovers
 False False
 basic crepes mix in blender
 False False
 unleavened bread
 False False
 easy basic crepes
 False False
 wonton wrappers
 False False
 egg roll wrappers
 False False
 anya s dutch pancakes
 False False
 norwegian pancakes   pannekaken
 False False
 plain popovers
 False False
 perfect boiled custard
 False False
 small batch popovers
 False False
 fresh made manicotti or cannelloni crepes shells
 False False
 muffin tin popovers
 False False

Let's tally and visualize a contingency table based on what we observed from the query results:

from collections import Counter
from matplotlib import pyplot as plt
from import mosaic

# warning: `mosaic` expects data in (x_axis, y_axis), so revert
# the axis so that this contingency table has the same order
contingency_table = dict(Counter(tally))

fig, ax = plt.subplots(1, 1)

fig, _ = mosaic(
    index=[1, 0], 
    # improvde default cmap
    properties=lambda key: {"color": "yellow" if key[1] == "True" else "gray"},



Note the conditional probabilities in this sample:

  • P(sez_noodle|has_butter) at approximately 1:2
  • P(has_butter|sez_noodle) at approximately 1:4

The latter is significantly lower, so these conditions seem reasonably good to use as filters for identifying which recipes to annotate. Of course, not every recipe is either a kind of pancake or noodle, since there are recipes for omelette, Yorkshire pudding, etc.

We'll revisit this kind of problem later with more formal, semi-automated methods that leverage machine learning and human-in-the-loop approaches.

For now let's add annotations based on our manual review:

noodle_ids = { "400", "86710", "331765", "508734", "320154", "220361", "148900", "317697", "252783", "137357", "1975", "31041", "441475", "261361", "124106", "78459", "358908", "103964", "91311", "497918", "328388" }
pancake_ids = { "277824", "489", "4643", "272746", "12055", "124131", "40772", "459", "48178", "124176", "61108", "111008", "262038", "458", "440398", "157638" }
for id in noodle_ids:
    url = f"{id}"
    node = rdflib.URIRef(url)
    kg.add(node, kg.get_ns("rdf").type, kg.get_ns("nom").Noodle)

for id in pancake_ids:
    url = f"{id}"
    node = rdflib.URIRef(url)
    kg.add(node, kg.get_ns("rdf").type, kg.get_ns("nom").Pancake)


Now open the tmp.ttl file. Notice the new annotations for nom:Noodle and nom:Pancake added to the recipes?


Exercise 1:

A third category of recipes that use flour, eggs, salt among their ingredients would be popovers (US usage) which are much the same as Yorkshire pudding (UK usage).

Extend the nom.ttl vocabulary to include the definition of a popover

Exercise 2:

Revise the queries and post-processing above to identify popover recipes.

Exercise 3:

Construct a popover_ids list for the identified recipes.

Exercise 4:

Annotate the KG to include nom:popover definitions for popovers.

Last update: 2021-05-09