<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Diala Abedalqader on Medium]]></title>
        <description><![CDATA[Stories by Diala Abedalqader on Medium]]></description>
        <link>https://medium.com/@dialaqader?source=rss-8f0a85ed63c8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*23ehudLD-VxbIHpkjYphZw.png</url>
            <title>Stories by Diala Abedalqader on Medium</title>
            <link>https://medium.com/@dialaqader?source=rss-8f0a85ed63c8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 16:03:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dialaqader/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Building a Knowledge Graph with RDF and SPARQL: A Hands-On Tutorial]]></title>
            <link>https://medium.com/@dialaqader/building-a-knowledge-graph-with-rdf-and-sparql-a-hands-on-tutorial-0161f7b25153?source=rss-8f0a85ed63c8------2</link>
            <guid isPermaLink="false">https://medium.com/p/0161f7b25153</guid>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[toutrial]]></category>
            <category><![CDATA[sparql]]></category>
            <category><![CDATA[data-science-projects]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Diala Abedalqader]]></dc:creator>
            <pubDate>Sat, 08 Aug 2026 05:31:14 GMT</pubDate>
            <atom:updated>2026-08-08T05:36:52.611Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TbijPcY1x1u4EbXneC_MIg.png" /></figure><p>Most data practitioners store facts in rows and columns. But some facts are really about <em>relationships</em>: this film was directed by that person, this ingredient belongs to that cuisine, this employee reports to that manager. When the relationships matter as much as the values, a table starts to fight you. A knowledge graph doesn’t.</p><p>In this tutorial, you’ll build a small knowledge graph from scratch using RDF and Turtle, load it into memory with Python’s rdflib, and query it with SPARQL. By the end, you&#39;ll be able to model your own domain as a graph and answer questions over it that would take several JOINs in SQL.</p><h3>What you’ll need</h3><ul><li>Python 3.9+</li><li>rdflib (pip install rdflib)</li><li>No database server required, everything in this tutorial runs in memory</li></ul><h3>Why a knowledge graph, and not just a table?</h3><p>Say you’re cataloguing films: title, genre, director, release year. In a spreadsheet, that’s four columns and one row per film. Simple enough, until you need to:</p><ul><li>Ask for every film in a genre that has two different names (“Science Fiction” and “Sci-Fi” both meaning the same thing)</li><li>Add a new attribute to only some films without restructuring the whole table</li><li>Merge your catalogue with someone else’s, where their “genre” column uses different labels than yours</li></ul><p>A knowledge graph handles all three naturally, because every fact is stored as an independent <strong>triple</strong>: <em>subject → predicate → object</em>. “Inception has genre Science Fiction” becomes one triple. “Science Fiction is also called Sci-Fi” becomes another. Nothing forces every subject to have the same shape, and synonyms are just more triples, not a schema migration.</p><h3>Step 1: Model the domain</h3><p>We’ll build a small graph of films: title, genre, director, and release year. Genres and directors are their own resources (not just strings), because we want to describe them too, for example, giving a genre both a formal label and an informal one.</p><p>That’s exactly what <strong>SKOS</strong> (Simple Knowledge Organization System) is for. It’s a lightweight, widely-used vocabulary for exactly this: skos:prefLabel for the canonical name, skos:altLabel for synonyms.</p><h3>Step 2: Write it in Turtle</h3><p>Turtle is RDF’s most readable serialization: subject, predicate, object, one statement per line, semicolons to chain multiple predicates about the same subject.</p><pre>@prefix :     &lt;http://example.org/films/&gt; .<br>@prefix skos: &lt;http://www.w3.org/2004/02/skos/core#&gt; .<br># ---- Genres, each with a formal and an informal label ----<br>:SciFi a :Genre ;<br>    skos:prefLabel &quot;Science Fiction&quot; ;<br>    skos:altLabel  &quot;Sci-Fi&quot; .<br>:Animation a :Genre ;<br>    skos:prefLabel &quot;Animation&quot; ;<br>    skos:altLabel  &quot;Animated&quot; .<br>:Thriller a :Genre ;<br>    skos:prefLabel &quot;Thriller&quot; ;<br>    skos:altLabel  &quot;Suspense&quot; .<br># ---- Directors ----<br>:directorNolan a :Director ; :name &quot;Christopher Nolan&quot; .<br>:directorMiyazaki a :Director ; :name &quot;Hayao Miyazaki&quot; .<br>:directorFincher a :Director ; :name &quot;David Fincher&quot; .<br># ---- Films ----<br>:film001 a :Film ;<br>    :title    &quot;Inception&quot; ;<br>    :genre    :SciFi ;<br>    :director :directorNolan ;<br>    :year     2010 .<br>:film002 a :Film ;<br>    :title    &quot;Spirited Away&quot; ;<br>    :genre    :Animation ;<br>    :director :directorMiyazaki ;<br>    :year     2001 .<br>:film003 a :Film ;<br>    :title    &quot;Se7en&quot; ;<br>    :genre    :Thriller ;<br>    :director :directorFincher ;<br>    :year     1995 .<br>:film004 a :Film ;<br>    :title    &quot;Interstellar&quot; ;<br>    :genre    :SciFi ;<br>    :director :directorNolan ;<br>    :year     2014 .</pre><p>A few things worth noticing:</p><ul><li>a is shorthand for rdf:type. :film001 a :Film reads as &quot;film001 is a Film.&quot;</li><li>Every subject is a URI (:film001), not just a row ID. That means anything else in the world can point <em>at</em> it and it will resolve to the same resource.</li><li>Genres and directors are subjects in their own right, with their own triples, not flat string columns.</li></ul><p>Save this as films.ttl.</p><h3>Step 3: Load it and check it parsed correctly</h3><pre>from rdflib import Graph<br>g = Graph()<br>g.parse(&quot;films.ttl&quot;, format=&quot;turtle&quot;)<br>print(f&quot;Loaded {len(g)} triples&quot;)</pre><p>If there’s a syntax error, rdflib will point at the offending line, so this is always worth running before you write a single query.</p><h3>Step 4: Query it with SPARQL</h3><p>SPARQL looks like SQL’s cousin: SELECT, WHERE, FILTER all show up, but it matches <em>graph patterns</em> instead of table rows.</p><p><strong>Query 1: list every film and its title</strong></p><pre>q1 = &quot;&quot;&quot;<br>PREFIX : &lt;http://example.org/films/&gt;SELECT ?film ?title WHERE {<br>    ?film a :Film ;<br>          :title ?title .<br>}<br>&quot;&quot;&quot;<br>for row in g.query(q1):<br>    print(row.title)<br></pre><p><strong>Query 2: find sci-fi films, but don’t miss the ones tagged with the informal label</strong></p><p>This is the interesting one. If you only filter on skos:prefLabel &quot;Sci-Fi&quot;, you&#39;ll silently get zero results, because the canonical label is &quot;Science Fiction,&quot; not &quot;Sci-Fi.&quot; SPARQL&#39;s | operator lets you match <em>either</em> predicate in a single graph pattern:</p><pre>q2 = &quot;&quot;&quot;<br>PREFIX :    &lt;http://example.org/films/&gt;<br>PREFIX skos: &lt;http://www.w3.org/2004/02/skos/core#&gt;SELECT ?title WHERE {<br>    ?film a :Film ;<br>          :title ?title ;<br>          :genre ?g .<br>    ?g skos:prefLabel|skos:altLabel &quot;Sci-Fi&quot; .<br>}<br>&quot;&quot;&quot;<br>for row in g.query(q2):<br>    print(row.title)<br># Inception<br># Interstellar<br></pre><p>This is a pattern worth keeping: whenever the same real-world thing might be labeled two different ways in your data, query both labels in one pattern rather than trusting that everyone tagged it the “correct” way.</p><p><strong>Query 3: films released after 2005, with their director</strong></p><pre>q3 = &quot;&quot;&quot;<br>PREFIX : &lt;http://example.org/films/&gt;SELECT ?title ?director WHERE {<br>    ?film a :Film ;<br>          :title ?title ;<br>          :year ?year ;<br>          :director ?director .<br>    FILTER (?year &gt; 2005)<br>}<br>ORDER BY ?year<br>&quot;&quot;&quot;<br>for row in g.query(q3):<br>    print(row.title, row.director)<br></pre><p>Notice there was no JOIN clause anywhere. The graph pattern <em>is</em> the join, ?film appears in multiple triple patterns, and SPARQL automatically only returns films where all of them are satisfied.</p><h3>Going further: a real triple store</h3><p>Everything above runs against an in-memory graph, which is perfect for prototyping. In production, you’d load the same Turtle file into a dedicated triple store like <strong>Apache Jena Fuseki</strong>, which exposes a SPARQL endpoint over HTTP so any service can query your graph, not just the Python process that built it. The queries themselves don’t change at all, that’s the appeal of standing on top of a W3C standard: the same SPARQL string that ran against rdflib above runs unmodified against a live endpoint via a library like SPARQLWrapper.</p><h3>Where to take this next</h3><p>Try extending the graph above with a rdfs:subClassOf hierarchy, for example, making both :SciFi and :Thriller subclasses of a broader :Genre category, and write a query that uses the * property path operator to walk that hierarchy automatically. That single operator is what separates a knowledge graph from a glorified lookup table: it lets you ask questions about structure, not just values.</p><p>Knowledge graphs won’t replace your relational tables. But the moment “how are these things connected” becomes as important a question as “what are the values,” they’re worth reaching for.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0161f7b25153" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>