Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Chapter 6 — Lab: Knowledge Graphs

Open in Colab

Lab Overview

ExerciseTopicPoints
1From a table to triples10
2Matching triple patterns15
3Queries with variables20
4Schema inference20
5SPARQL with rdflib15
6Scoring and evaluating embeddings20
ChallengeMining a rule and its confidence+10

Complete the code where you see # TODO, then run the check cell under it. In Colab, first run !pip install rdflib.


Exercise 1: From a Table to Triples (10 points)

Complete table_to_triples(rows, key, columns): each row becomes an entity named by its key column; each of the other listed columns becomes a predicate, with the cell as object. Skip empty cells (None): under the open-world assumption, a missing value is not a fact.


Exercise 2: Matching Triple Patterns (15 points)

Complete match(triples, s=None, p=None, o=None), returning the set of triples that agree with every argument that is not None.


Exercise 3: Queries with Variables (20 points)

Complete query(triples, patterns). Each pattern is a triple in which strings starting with ? are variables. Return the list of all bindings (dicts from variable to value) that satisfy every pattern at once. Hint: recursion: match the first pattern with the variables already bound replaced by their values, extend the binding, and solve the remaining patterns.


Exercise 4: Schema Inference (20 points)

Complete infer(triples, schema), returning a new set with all the triples that follow from the rules below, applied repeatedly until nothing new appears:

  1. (A, 'subClassOf', B) and (B, 'subClassOf', C) give (A, 'subClassOf', C)

  2. (x, 'type', A) and (A, 'subClassOf', B) give (x, 'type', B)

  3. (p, 'domain', A) and (x, p, y) give (x, 'type', A)

  4. (p, 'range', A) and (x, p, y) give (y, 'type', A)

  5. (p, 'transitive', True) and (x, p, y), (y, p, z) give (x, p, z)

Question. MA101 has no prerequisite in the data. Under the open-world assumption, may the system conclude “MA101 has no prerequisite”? What would a university database (closed world) conclude?

Your answer:


Exercise 5: SPARQL with rdflib (15 points)

The courses are loaded into an rdflib graph below (prefix ex:). Write the three queries as strings.


Exercise 6: Scoring and Evaluating Embeddings (20 points)

Complete:

  • transe_score(h, r, t): −∥h+r−t∥2-\|\mathbf{h} + \mathbf{r} - \mathbf{t}\|_2 for NumPy vectors;

  • rank(scores, true_index): 1 + the number of candidates with a strictly higher score than the true one;

  • hits_and_mrr(ranks, k): the pair (fraction of ranks ≤k\le k, mean of 1/rank1/\text{rank}).

Question. TransE gives each relation one vector. Why does this fail for a symmetric relation such as siblingOf? (Write the two equations.)

Your answer:


Challenge: Mining a Rule (+10 points)

A path rule r1(x,y)∧r2(y,z)⇒r3(x,z)r_1(x, y) \wedge r_2(y, z) \Rightarrow r_3(x, z) is judged by its support (how many (x,z)(x, z) pairs the body produces that are also r3r_3 facts) and its confidence (support divided by the number of (x,z)(x, z) pairs the body produces). Complete rule_confidence(triples, r1, r2, r3) returning (support, confidence).

The confidence is below 1 only because the fact Mona grandparentOf Lina is missing: the rule proposes exactly that fact. This is how rule mining completes knowledge graphs.


Lab Report

Submit this notebook with every check cell showing ✅ and your answers to the two questions.