Skip to content


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

Build a medium size KG from a CSV dataset

First let's initialize the KG object as we did previously:

import kglab

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

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

Here's a way to describe the namespaces that are available to use:

prefix namespace
0 dct
1 owl
2 prov
3 rdf
4 rdfs
5 schema
6 sh
7 xsd
8 wtm
9 ind
10 skos

Next, we'll define a dictionary that maps (somewhat magically) from strings (i.e., "labels") to ingredients defined in the vocabulary:

common_ingredient = {
    "water": kg.get_ns("ind").Water,
    "salt": kg.get_ns("ind").Salt,
    "pepper": kg.get_ns("ind").BlackPepper,
    "black pepper": kg.get_ns("ind").BlackPepper,
    "dried basil": kg.get_ns("ind").Basil,

    "butter": kg.get_ns("ind").Butter,
    "milk": kg.get_ns("ind").CowMilk,
    "egg": kg.get_ns("ind").ChickenEgg,
    "eggs": kg.get_ns("ind").ChickenEgg,
    "bacon": kg.get_ns("ind").Bacon,

    "sugar": kg.get_ns("ind").WhiteSugar,
    "brown sugar": kg.get_ns("ind").BrownSugar,
    "honey": kg.get_ns("ind").Honey,
    "vanilla": kg.get_ns("ind").VanillaExtract,
    "vanilla extract": kg.get_ns("ind").VanillaExtract,

    "flour": kg.get_ns("ind").AllPurposeFlour,
    "all-purpose flour": kg.get_ns("ind").AllPurposeFlour,
    "whole wheat flour": kg.get_ns("ind").WholeWheatFlour,

    "olive oil": kg.get_ns("ind").OliveOil,
    "vinegar": kg.get_ns("ind").AppleCiderVinegar,

    "garlic": kg.get_ns("ind").Garlic,
    "garlic clove": kg.get_ns("ind").Garlic,
    "garlic cloves": kg.get_ns("ind").Garlic,

    "onion": kg.get_ns("ind").Onion,
    "onions": kg.get_ns("ind").Onion,
    "cabbage": kg.get_ns("ind").Cabbage,
    "carrot": kg.get_ns("ind").Carrot,
    "carrots": kg.get_ns("ind").Carrot,
    "celery": kg.get_ns("ind").Celery,
    "potato": kg.get_ns("ind").Potato,
    "potatoes": kg.get_ns("ind").Potato,
    "tomato": kg.get_ns("ind").Tomato,
    "tomatoes": kg.get_ns("ind").Tomato,

    "baking powder": kg.get_ns("ind").BakingPowder,
    "baking soda": kg.get_ns("ind").BakingSoda,

This is where use of NLP work to produce annotations begins to overlap with KG pratices.

Now let's load our dataset of recipes – the dat/recipes.csv file in CSV format – into a pandas dataframe:

import pandas as pd

df = pd.read_csv("../dat/recipes.csv")
id name minutes tags description ingredients
0 164636 1 1 1 tempura batter 5 ['15-minutes-or-less', 'time-to-make', 'course... i use this everytime i make onion rings, hot p... ['egg', 'flour', 'water']
1 144841 2 step pound cake for a kitchen aide mixer 110 ['time-to-make', 'course', 'preparation', 'occ... this recipe was published in a southern living... ['flour', 'sugar', 'butter', 'milk', 'eggs', '...
2 189437 40 second omelet 25 ['30-minutes-or-less', 'time-to-make', 'course... you'll need an "inverted pancake turner" for t... ['eggs', 'water', 'butter']
3 19104 all purpose dinner crepes batter 90 ['weeknight', 'time-to-make', 'course', 'main-... this basic crepe recipe can be used for all yo... ['eggs', 'salt', 'flour', 'milk', 'butter']
4 64793 amish friendship starter 14405 ['weeknight', 'time-to-make', 'course', 'cuisi... this recipe was given to me years ago by a fri... ['sugar', 'flour', 'milk']

Then iterate over the rows in the dataframe, representing a recipe in the KG for each row:

import rdflib

for index, row in df.iterrows():
    recipe_id = row["id"]
    node = rdflib.URIRef("{}".format(recipe_id))
    kg.add(node, kg.get_ns("rdf").type, kg.get_ns("wtm").Recipe)

    recipe_name = row["name"]
    kg.add(node, kg.get_ns("skos").definition, rdflib.Literal(recipe_name))

    cook_time = row["minutes"]
    cook_time_literal = "PT{}M".format(int(cook_time))
    code_time_node = rdflib.Literal(cook_time_literal, datatype=kg.get_ns("xsd").duration)
    kg.add(node, kg.get_ns("wtm").hasCookTime, code_time_node)

    ind_list = eval(row["ingredients"])

    for ind in ind_list:
        ingredient = ind.strip()
        ingredient_obj = common_ingredient[ingredient]
        kg.add(node, kg.get_ns("wtm").hasIngredient, ingredient_obj)

Notice how the xsd:duration literal is now getting used to represent cooking times.

We've structured this example such that each of the recipes in the CSV file has a known representation for all of its ingredients. There are nearly 250K recipes in the full dataset from so the common_ingredient dictionary would need to be extended quite a lot to handle all of those possible ingredients.

At this stage, our graph has grown by a couple orders of magnitude, so its visualization should be more interesting now. Let's take a look:

    "wtm": {
        "color": "orange",
        "size": 20,
        "color": "blue",
        "size": 35,

subgraph = kglab.SubgraphTensor(kg)
pyvis_graph = subgraph.build_pyvis_graph(notebook=True, style=VIS_STYLE)



Given the defaults for this kind of visualization, there's likely a dense center mass of orange (recipes) at the center, with a close cluster of common ingredients (dark blue), surrounded by less common ingredients and cooking times (light blue).

Performance analysis of serialization methods

Let's serialize this recipe KG constructed from the CSV dataset to a local TTL file, while measuring the time and disk space required:

import time

write_times = []

t0 = time.time()
write_times.append(round((time.time() - t0) * 1000.0, 2))

Let's also serialize the KG into the other formats that we've been using, to compare relative sizes for a medium size KG:

t0 = time.time()
kg.save_rdf("tmp.xml", format="xml")
write_times.append(round((time.time() - t0) * 1000.0, 2))

t0 = time.time()
write_times.append(round((time.time() - t0) * 1000.0, 2))

t0 = time.time()
write_times.append(round((time.time() - t0) * 1000.0, 2))
import pandas as pd
import os

file_paths = ["tmp.ttl", "tmp.xml", "tmp.jsonld", "tmp.parquet"]
file_sizes = [os.path.getsize(file_path) for file_path in file_paths]

df = pd.DataFrame({"file_path": file_paths, "file_size": file_sizes, "write_time": write_times})
df["ms_per_byte"] = df["write_time"] / df["file_size"]
file_path file_size write_time ms_per_byte
0 tmp.ttl 56780 239.19 0.004213
1 tmp.xml 159397 78.95 0.000495
2 tmp.jsonld 131850 112.60 0.000854
3 tmp.parquet 14545 106.94 0.007352

Notice the relative sizes and times? Parquet provides for compression in a way that works well with RDF. The same KG stored as a Parquet file is ~10% the size of the same KG stored as JSON-LD. Also the XML version is quite large.

Looking at the write times, Parquet is relatively fast (after its first invocation) and its reads are faster. The eponymous Turtle format is human-readable although relatively slow. XML is fast to write, but much larger on disk and difficult to read. JSON-LD is interesting in that any JSON library can read and use these files, without needing semantic technologies, per se; however, it's also large on disk.


Exercise 1:

Select another ingredient in the vocabulary that is not in the common_ingredient dictionary, for which you can find at least one simple recipe within searches. Then add this recipe into the KG.

Last update: 2021-04-10