- Download a copy of the demonstration notebook (contents displayed below).
- Download a copy of the RDF from the Linked Open Data page on ctext.org, unzip the file, and place the extracted file in the same folder as the Jupyter notebook.
In [1]:
# Very simple example of how to load and navigate the ctext.org RDF data with rdflib
# https://digitalsinology.org/ctext-org-rdf
from rdflib import Graph
from rdflib import URIRef
from rdflib.namespace import RDF
from rdflib import BNode
g = Graph()
# Before running this, download and unzip the latest RDF dump from here and update the filename below:
# https://ctext.org/tools/linked-open-data
g.parse("ctext_datawiki-2021-09-08.ttl", format="n3")
# Recommended prefixes (from the RDF dump):
# @prefix ctext: <https://data.ctext.org/entity/> .
# @prefix claim: <https://data.ctext.org/claim/> .
# @prefix cprop: <https://data.ctext.org/property/> .
# @prefix cqual: <https://data.ctext.org/qualifier/> .
# @prefix ctype: <https://data.ctext.org/type/> .
Out[1]:
<Graph identifier=Nbf11733aa147489aa059b5822952b701 (<class 'rdflib.graph.Graph'>)>
In [2]:
# Suppose we have a particular entity in mind, e.g. ctext:238711
# We can get its URI from the entity page on the user inferface (https://ctext.org/datawiki.pl?if=en&res=238711)
# URI: https://data.ctext.org/entity/238711
exampleEntity = URIRef("https://data.ctext.org/entity/238711")
In [3]:
# We can list the statements for which this entity is a subject
# N.B. All of these will be blank nodes! This is because (in principle) any of these claims could have qualifiers.
for s, p, o in g.triples((exampleEntity, None, None)):
print(f"{s} {p} {o}")
https://data.ctext.org/entity/238711 https://data.ctext.org/claim/type ub1bL185212C25 https://data.ctext.org/entity/238711 https://data.ctext.org/claim/creator ub1bL185218C28 https://data.ctext.org/entity/238711 https://data.ctext.org/claim/indexed-in ub1bL185231C31 https://data.ctext.org/entity/238711 https://data.ctext.org/claim/indexed-in ub1bL185226C31 https://data.ctext.org/entity/238711 https://data.ctext.org/claim/ctext-work ub1bL185215C31 https://data.ctext.org/entity/238711 https://data.ctext.org/claim/name ub1bL185209C25 https://data.ctext.org/entity/238711 https://data.ctext.org/claim/indexed-in ub1bL185221C31
In [4]:
# Repeat the above, doing the same for any blank nodes encountered:
for s, p, o in g.triples((exampleEntity, None, None)):
print(f"{s} {p} {o}")
if o:
for s2, p2, o2 in g.triples((o, None, None)):
print(f" {s2} {p2} {o2}")
https://data.ctext.org/entity/238711 https://data.ctext.org/claim/type ub1bL185212C25 ub1bL185212C25 https://data.ctext.org/property/type work https://data.ctext.org/entity/238711 https://data.ctext.org/claim/creator ub1bL185218C28 ub1bL185218C28 https://data.ctext.org/property/creator https://data.ctext.org/entity/427467 https://data.ctext.org/entity/238711 https://data.ctext.org/claim/indexed-in ub1bL185231C31 ub1bL185231C31 https://data.ctext.org/property/indexed-in https://data.ctext.org/entity/947121 ub1bL185231C31 https://data.ctext.org/qualifier/juan-size 6 https://data.ctext.org/entity/238711 https://data.ctext.org/claim/indexed-in ub1bL185226C31 ub1bL185226C31 https://data.ctext.org/qualifier/juan-size 6 ub1bL185226C31 https://data.ctext.org/qualifier/stated-category 易 ub1bL185226C31 https://data.ctext.org/property/indexed-in https://data.ctext.org/entity/882634 https://data.ctext.org/entity/238711 https://data.ctext.org/claim/ctext-work ub1bL185215C31 ub1bL185215C31 https://data.ctext.org/property/ctext-work ctp:work:wb61195 https://data.ctext.org/entity/238711 https://data.ctext.org/claim/name ub1bL185209C25 ub1bL185209C25 https://data.ctext.org/property/name 易小傳 https://data.ctext.org/entity/238711 https://data.ctext.org/claim/indexed-in ub1bL185221C31 ub1bL185221C31 https://data.ctext.org/qualifier/juan-size 6 ub1bL185221C31 https://data.ctext.org/qualifier/stated-category 易 ub1bL185221C31 https://data.ctext.org/property/indexed-in https://data.ctext.org/entity/545989
In [5]:
# Instantiations of properties use the "cprop" prefix; so the URI for instantiating 'ctext-work' is:
work = URIRef("https://data.ctext.org/property/ctext-work")
In [6]:
# Find all entities that have a "ctext-work" value
for s, p, o in g.triples((None, work, None)):
print(f"{s} {p} {o}")
ub1bL649395C31 https://data.ctext.org/property/ctext-work ctp:work:wb196671 ub1bL450933C31 https://data.ctext.org/property/ctext-work ctp:work:wb502592 ub1bL42902C30 https://data.ctext.org/property/ctext-work ctp:work:wb941044 ub1bL111862C31 https://data.ctext.org/property/ctext-work ctp:work:wb468560 ub1bL232964C31 https://data.ctext.org/property/ctext-work ctp:work:wb243258 ub1bL316990C31 https://data.ctext.org/property/ctext-work ctp:work:wb772381 ub1bL45811C30 https://data.ctext.org/property/ctext-work ctp:work:wb702064 ...