<jupyter_start><jupyter_text>**A Quick Semantic Technology Tutorial**>  *Author* : [<NAME>](https://rathachai.creatier.pro/) , Ph.D>  *Affiliation* : Computer Engineering, King Mongkut's Institute of Technology Ladkrabang (KMITL)>  *Updated Date* : 2021-01-11--- > This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. **References:***   https://github.com/RDFLib/rdflib*   https://rdflib.readthedocs.io/*   https://owl-rl.readthedocs.io/*   https://rdflib.dev/sparqlwrapper/  Install Libs<jupyter_code>#!pip install rdflib
#!pip install owlrl
#!pip install sparqlwrapper<jupyter_output><empty_output><jupyter_text>To Load used Libraries<jupyter_code>import rdflib
from rdflib import URIRef, BNode, Literal
from rdflib import Namespace
from rdflib.namespace import CSVW, DC, DCAT, DCTERMS, DOAP, FOAF, ODRL2, ORG, OWL, PROF, PROV, RDF, RDFS, SDO, SH, SKOS, SOSA, SSN, TIME, VOID, XMLNS, XSD
from rdflib.plugins import sparql

import owlrl

from SPARQLWrapper import SPARQLWrapper, JSON, XML, N3, TURTLE, JSONLD<jupyter_output><empty_output><jupyter_text>To Create Nodes<jupyter_code>bob = URIRef("http://example.org/people/Bob")
bob
linda = BNode()
linda
name = Literal('Bob')
name
age = Literal(24)
age
height = Literal(76.5)
height<jupyter_output><empty_output><jupyter_text>To Create Nodes from Namespaces<jupyter_code>n = Namespace("http://example.org/people/")
n
n.bob
n.eve
RDF
RDF.type
FOAF.knows<jupyter_output><empty_output><jupyter_text>To Create Triples<jupyter_code>pp = Namespace("http://example.org/people/")
g = rdflib.Graph()
print(g.serialize(format="turtle").decode("utf-8"))
g.add((pp.bob, RDF.type, FOAF.Person))
print(g.serialize(format="turtle").decode("utf-8"))
g.bind("foaf", FOAF)
print(g.serialize(format="turtle").decode("utf-8"))
g.bind("pp", pp)
print(g.serialize(format="turtle").decode("utf-8"))
g.add((pp.bob, FOAF.name, Literal('Bob')))
print(g.serialize(format="turtle").decode("utf-8"))
g.add((pp.bob, FOAF.knows, pp.linda))
print(g.serialize(format="turtle").decode("utf-8"))
g.add((pp.bob, FOAF.knows, pp.mark))
print(g.serialize(format="turtle").decode("utf-8"))<jupyter_output>@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix pp: <http://example.org/people/> .

pp:bob a foaf:Person ;
    foaf:knows pp:linda,
        pp:mark ;
    foaf:name "Bob" .<jupyter_text>To Create a Graph<jupyter_code>g = rdflib.Graph()
g.bind("foaf", FOAF)
g.bind("pp", pp)

g.add((pp.bob, RDF.type, FOAF.Person))
g.add((pp.bob, FOAF.name, name))

g.add((pp.linda, RDF.type, FOAF.Person))
g.add((pp.linda, FOAF.name, Literal("Linda")))

g.add((pp.mark, RDF.type, FOAF.Person))
g.add((pp.mark, FOAF.name, Literal("Mark")))

g.add((pp.bob, FOAF.knows, pp.linda))
g.add((pp.bob, FOAF.knows, pp.mark))
print(g.serialize(format="turtle").decode("utf-8"))
print(g.serialize(format="xml").decode("utf-8"))
print(g.serialize(format="ntriples").decode("utf-8"))<jupyter_output><http://example.org/people/linda> <http://xmlns.com/foaf/0.1/name> "Linda" .
<http://example.org/people/bob> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> .
<http://example.org/people/linda> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> .
<http://example.org/people/mark> <http://xmlns.com/foaf/0.1/name> "Mark" .
<http://example.org/people/bob> <http://xmlns.com/foaf/0.1/knows> <http://example.org/people/mark> .
<http://example.org/people/bob> <http://xmlns.com/foaf/0.1/name> "Bob" .
<http://example.org/people/bob> <http://xmlns.com/foaf/0.1/knows> <http://example.org/people/linda> .
<http://example.org/people/mark> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> .<jupyter_text>To Create a Graph from String<jupyter_code>g = rdflib.Graph()
print(g.serialize(format="turtle").decode("utf-8"))
rdf_text = '''
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix pp: <http://example.org/people/> .

pp:bob a foaf:Person ;
    foaf:knows pp:linda, pp:mark ;
    foaf:name "Bob" .

pp:linda a foaf:Person ;
    foaf:name "Linda" .

pp:mark foaf:name "Mark" .
'''
g.parse( data=rdf_text, format='turtle' )
print(g.serialize(format="turtle").decode("utf-8"))<jupyter_output>@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix pp: <http://example.org/people/> .

pp:bob a foaf:Person ;
    foaf:knows pp:linda,
        pp:mark ;
    foaf:name "Bob" .

pp:linda a foaf:Person ;
    foaf:name "Linda" .

pp:mark foaf:name "Mark" .<jupyter_text>To Save a Graph<jupyter_code>rdf_text = '''
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix pp: <http://example.org/people/> .

pp:bob a foaf:Person ;
    foaf:knows pp:linda, pp:mark ;
    foaf:name "Bob" .

pp:linda a foaf:Person ;
    foaf:name "Linda" .

pp:mark foaf:name "Mark" .
'''

g = rdflib.Graph()
g.parse( data=rdf_text, format='turtle' )
g.serialize('my_rdf.ttl',format='turtle')<jupyter_output><empty_output><jupyter_text>To Read a Graph from a File<jupyter_code>g2 = rdflib.Graph()
g2.parse("/content/my_rdf.ttl", format='turtle')
print(g2.serialize(format="turtle").decode("utf-8"))<jupyter_output>@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix pp: <http://example.org/people/> .

pp:bob a foaf:Person ;
    foaf:knows pp:linda,
        pp:mark ;
    foaf:name "Bob" .

pp:linda a foaf:Person ;
    foaf:name "Linda" .

pp:mark foaf:name "Mark" .<jupyter_text>To Read a Graph from URL<jupyter_code>g = rdflib.Graph()
g.parse("http://bigasterisk.com/foaf.rdf")
print(g.serialize(format="turtle").decode("utf-8"))<jupyter_output>@prefix : <http://www.w3.org/2003/01/geo/wgs84_pos#> .
@prefix admin: <http://webns.net/mvcb/> .
@prefix cert: <http://www.w3.org/ns/auth/cert#> .
@prefix doap: <http://usefulinc.com/ns/doap#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix redfoot: <http://redfoot.net/2005/session#> .
@prefix rsa: <http://www.w3.org/ns/auth/rsa#> .

<https://bigasterisk.com/foaf.rdf> a foaf:PersonalProfileDocument ;
    admin:errorReportsTo <mailto:<EMAIL>> ;
    admin:generatorAgent <http://www.ldodds.com/foaf/foaf-a-matic> ;
    foaf:maker <http://bigasterisk.com/foaf.rdf#drewp> ;
    foaf:primaryTopic <http://bigasterisk.com/foaf.rdf#drewp> .

<http://bblfish.net/people/henry/card#me> a foaf:Person ;
    rdfs:seeAlso <http://bblfish.net/people/henry/card> ;
    foaf:name "<NAME>" .

<http://collectivesource.com/foaf.rdf#nathan> a foaf:Person ;
    rdfs:seeAlso <http://collect[...]<jupyter_text>To Load a Graph from a Resource<jupyter_code>g = rdflib.Graph()
g.load('http://dbpedia.org/resource/IPhone_12_Pro')
print(g.serialize(format="turtle").decode("utf-8")[:1000])
print(" ... ")
print(" ... ")
print(" ... ")
print(g.serialize(format="turtle").decode("utf-8")[-500:])<jupyter_output>@prefix dbo: <http://dbpedia.org/ontology/> .
@prefix dbp: <http://dbpedia.org/property/> .
@prefix dct: <http://purl.org/dc/terms/> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix prov: <http://www.w3.org/ns/prov#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

<http://dbpedia.org/resource/IPhone_12_Pro_Max> dbo:wikiPageRedirects <http://dbpedia.org/resource/IPhone_12_Pro> .

<http://dbpedia.org/resource/IPhone_11_Pro> dbp:successor <http://dbpedia.org/resource/IPhone_12_Pro> .

<http://dbpedia.org/resource/IPhone_12> dbp:related <http://dbpedia.org/resource/IPhone_12_Pro> .

<http://en.wikipedia.org/wiki/IPhone_12_Pro> foaf:primaryTopic <http://dbpedia.org/resource/IPhone_12_Pro> .

<http://dbpedia.org/resource/IPhone_12_Pro> rdfs:label "IPhone 12 Pro"@en ;
    dbo:wikiPageExternalLink <https://apple.com/iphone-12-pro%7CiPhone> ;
    dbo:wikiPageID 65570746 ;
    dbo:[...]<jupyter_text>To Read each Trople from a Graph<jupyter_code>rdf_text = '''
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix pp: <http://example.org/people/> .

pp:bob a foaf:Person ;
    foaf:knows pp:linda, pp:mark ;
    foaf:name "Bob" .

pp:linda a foaf:Person ;
    foaf:name "Linda" .

pp:mark foaf:name "Mark" .
'''

g = rdflib.Graph()
g.parse( data=rdf_text, format='turtle' )
print(g.serialize(format="turtle").decode("utf-8"))
for s, p, o in g:
    print(s, p, o)<jupyter_output>http://example.org/people/linda http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://xmlns.com/foaf/0.1/Person
http://example.org/people/bob http://xmlns.com/foaf/0.1/knows http://example.org/people/linda
http://example.org/people/bob http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://xmlns.com/foaf/0.1/Person
http://example.org/people/bob http://xmlns.com/foaf/0.1/name Bob
http://example.org/people/bob http://xmlns.com/foaf/0.1/knows http://example.org/people/mark
http://example.org/people/mark http://xmlns.com/foaf/0.1/name Mark
http://example.org/people/linda http://xmlns.com/foaf/0.1/name Linda<jupyter_text>Basic Query<jupyter_code>rdf_text = '''
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix pp: <http://example.org/people/> .

pp:bob a foaf:Person ;
    foaf:knows pp:linda, pp:mark ;
    foaf:name "Bob" .

pp:linda a foaf:Person ;
    foaf:name "Linda" .

pp:mark foaf:name "Mark" .
'''

g = rdflib.Graph()
g.parse( data=rdf_text, format='turtle' )
print(g.serialize(format="turtle").decode("utf-8"))
res = g.query('SELECT ?s ?p ?o WHERE { ?s ?p ?o .}')
res
for row in res:
  print(row)
for row in res:
  print(row[0])
for row in res:
  s, p, o = row
  print(s, p, o)
spql = 'SELECT ?o WHERE { ?s foaf:knows ?o .}'

res = g.query(spql, initNs={"foaf":FOAF})
for row in res:
  print(row[0])<jupyter_output>http://example.org/people/linda
http://example.org/people/mark<jupyter_text>Advanced Semantic Technology  Ontology Preparation<jupyter_code>rdf_text = '''
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix pp: <http://example.org/people/> .

pp:bob a foaf:Person ;
    foaf:knows pp:linda, pp:mark ;
    foaf:name "Bob" .

pp:linda a foaf:Person ;
    foaf:name "Linda" .

pp:mark foaf:name "Mark" .
'''

rdf_data = rdflib.Graph()
rdf_data.parse( data=rdf_text, format='turtle' )
onto_text = '''
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

foaf:Person rdfs:subClassOf foaf:Agent .

foaf:knows rdf:type owl:SymmetricProperty .
foaf:knows rdfs:domain foaf:Person .
foaf:knows rdfs:range foaf:Person .

'''

my_onto = rdflib.Graph()
my_onto.parse( data=onto_text, format='turtle' )
print(rdf_data.serialize(format="turtle").decode("utf-8"))
print(my_onto.serialize(format="turtle").decode("utf-8"))
g = my_onto + rdf_data
print(g.serialize(format="turtle").decode("utf-8"))<jupyter_output>@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix pp: <http://example.org/people/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

pp:bob a foaf:Person ;
    foaf:knows pp:linda,
        pp:mark ;
    foaf:name "Bob" .

foaf:knows a owl:SymmetricProperty ;
    rdfs:domain foaf:Person ;
    rdfs:range foaf:Person .

pp:linda a foaf:Person ;
    foaf:name "Linda" .

pp:mark foaf:name "Mark" .

foaf:Person rdfs:subClassOf foaf:Agent .<jupyter_text>Try to Query<jupyter_code># Type of Bob

spql = 'SELECT * WHERE { pp:bob rdf:type ?o .}'
res = g.query(spql, initNs={"foaf":FOAF, "pp":pp})

for row in res:
  print(row)
# Who are Peoson?

spql = 'SELECT * WHERE { ?x rdf:type foaf:Person .}'
res = g.query(spql, initNs={"foaf":FOAF, "pp":pp})

for row in res:
  print(row)
# Who nows whom?

spql = 'SELECT * WHERE { ?x foaf:knows ?y .}'
res = g.query(spql, initNs={"foaf":FOAF, "pp":pp})

for row in res:
  print(row)<jupyter_output>(rdflib.term.URIRef('http://example.org/people/bob'), rdflib.term.URIRef('http://example.org/people/linda'))
(rdflib.term.URIRef('http://example.org/people/bob'), rdflib.term.URIRef('http://example.org/people/mark'))<jupyter_text>RDFS Reasoning<jupyter_code>owlrl.DeductiveClosure(owlrl.RDFS_Semantics, datatype_axioms=True).expand(g)
# Type of Bob

spql = 'SELECT ?o WHERE { pp:bob rdf:type ?o .}'
res = g.query(spql, initNs={"foaf":FOAF, "pp":pp})

for row in res:
  print(row)
# Who are Person?

spql = 'SELECT ?x WHERE { ?x rdf:type foaf:Person .}'
res = g.query(spql, initNs={"foaf":FOAF, "pp":pp})

for row in res:
  print(row[0])
# Who nows whom?

spql = 'SELECT * WHERE { ?x foaf:knows ?y.}'
res = g.query(spql, initNs={"foaf":FOAF, "pp":pp})

for row in res:
  print(row)<jupyter_output>(rdflib.term.URIRef('http://example.org/people/bob'), rdflib.term.URIRef('http://example.org/people/linda'))
(rdflib.term.URIRef('http://example.org/people/bob'), rdflib.term.URIRef('http://example.org/people/mark'))<jupyter_text>OWL Reasoning<jupyter_code>owlrl.DeductiveClosure(owlrl.OWLRL_Extension).expand(g)
# Who nows whom?

spql = 'SELECT * WHERE { ?x foaf:knows ?y.}'
res = g.query(spql, initNs={"foaf":FOAF, "pp":pp})

for row in res:
  print(row)<jupyter_output>(rdflib.term.URIRef('http://example.org/people/bob'), rdflib.term.URIRef('http://example.org/people/linda'))
(rdflib.term.URIRef('http://example.org/people/linda'), rdflib.term.URIRef('http://example.org/people/bob'))
(rdflib.term.URIRef('http://example.org/people/mark'), rdflib.term.URIRef('http://example.org/people/bob'))
(rdflib.term.URIRef('http://example.org/people/bob'), rdflib.term.URIRef('http://example.org/people/mark'))<jupyter_text>Pratical Example  Select Well-known Ontologies<jupyter_code>dc_onto = rdflib.Graph().parse("https://www.dublincore.org/specifications/dublin-core/dcmi-terms/dublin_core_terms.ttl", format="turtle")
foaf_onto = rdflib.Graph().parse("http://xmlns.com/foaf/spec/index.rdf", format="xml")
skos_onto = rdflib.Graph().parse("http://www.w3.org/TR/skos-reference/skos.rdf", format="xml")

print("DC --> ....", dc_onto.serialize(format="turtle").decode("utf-8")[500:800], " ... and more ...")
print("--------------------------------")
print("FOAF --> ....", foaf_onto.serialize(format="turtle").decode("utf-8")[500:800], " ... and more ...")
print("--------------------------------")
print("SKOS --> ....", skos_onto.serialize(format="turtle").decode("utf-8")[500:800], " ... and more ...")<jupyter_output>DC --> .... @en ;
    rdfs:isDefinedBy dcterms: .

dcterms:AgentClass a rdfs:Class ;
    rdfs:label "Agent Class"@en ;
    dcterms:issued "2008-01-14"^^xsd:date ;
    rdfs:comment "A group of agents."@en ;
    rdfs:isDefinedBy dcterms: ;
    rdfs:subClassOf rdfs:Class .

dcterms:BibliographicResource a rdfs:Cla  ... and more ...
--------------------------------
FOAF --> .... al artifact)." ;
    owl:equivalentClass <http://purl.org/dc/terms/Agent> ;
    vs:term_status "stable" .

foaf:Document a rdfs:Class,
        owl:Class ;
    rdfs:label "Document" ;
    rdfs:comment "A document." ;
    rdfs:isDefinedBy foaf: ;
    owl:disjointWith foaf:Organization,
        foaf:Pr  ... and more ...
--------------------------------
SKOS --> .... :hiddenLabel are pairwise disjoint properties."@en ;
    rdfs:isDefinedBy <http://www.w3.org/2004/02/skos/core> ;
    rdfs:subPropertyOf rdfs:label ;
    skos:definition "An alternative lexical label for a resource."@en ;
    skos:example "Acronyms, abbrev[...]<jupyter_text>Create your Customed Ontology<jupyter_code>rc_onto_text = '''
@prefix rc: <http://rathachai.ch/rc/0.1/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix dcterms: <http://purl.org/dc/terms/> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .



rc:write rdfs:domain foaf:Person .
rc:write rdfs:range foaf:Document .
rc:write owl:inverseOf rc:writtenBy .
rc:write rdfs:subPropertyOf dcterms:creator .


'''

rc_onto = rdflib.Graph().parse( data=rc_onto_text, format='turtle' )
print("RC ONTO --> ....",rc_onto.serialize(format="turtle").decode("utf-8"))<jupyter_output>RC ONTO --> .... @prefix dcterms: <http://purl.org/dc/terms/> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rc: <http://rathachai.ch/rc/0.1/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

rc:write rdfs:domain foaf:Person ;
    rdfs:range foaf:Document ;
    rdfs:subPropertyOf dcterms:creator ;
    owl:inverseOf rc:writtenBy .<jupyter_text>Create your RDF Data<jupyter_code>my_rdf_text = '''
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix dcam: <http://purl.org/dc/dcam/> .
@prefix dcterms: <http://purl.org/dc/terms/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

@prefix rc: <http://rathachai.ch/rc/0.1/> .

@prefix pp: <http://example.org/people/> .
@prefix ex: <http://example.org/> .



pp:jkrowling rc:write ex:harry_potter .

ex:harry_potter skos:broaderTransitive ex:Fantacy .
ex:Fantacy skos:broaderTransitive ex:Story .

'''

my_rdf = rdflib.Graph().parse( data=my_rdf_text, format='turtle' )
print(my_rdf.serialize(format="turtle").decode("utf-8"))<jupyter_output>@prefix ex: <http://example.org/> .
@prefix pp: <http://example.org/people/> .
@prefix rc: <http://rathachai.ch/rc/0.1/> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .

pp:jkrowling rc:write ex:harry_potter .

ex:Fantacy skos:broaderTransitive ex:Story .

ex:harry_potter skos:broaderTransitive ex:Fantacy .<jupyter_text>Combine data and ontologies<jupyter_code>g = dc_onto + foaf_onto + skos_onto + rc_onto + my_rdf<jupyter_output><empty_output><jupyter_text>Deductive Closure<jupyter_code>owlrl.DeductiveClosure(owlrl.RDFS_Semantics, datatype_axioms=True).expand(g)
owlrl.DeductiveClosure(owlrl.OWLRL_Extension, datatype_axioms=True).expand(g)<jupyter_output><empty_output><jupyter_text>Query<jupyter_code># What is <NAME>?

spql = 'SELECT ?p ?o WHERE { ex:harry_potter ?p ?o.}'
res = g.query(spql, initNs={"foaf":FOAF, "pp":pp, "ex":"http://example.org/"})

for row in res:
  print(row["p"], row["o"])
# What is <NAME>?

spql = 'SELECT ?p ?o WHERE { pp:jkrowling ?p ?o.}'
res = g.query(spql, initNs={"foaf":FOAF, "pp":pp, "ex":"http://example.org/"})

for row in res:
  print(row["p"], row["o"])<jupyter_output>http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/01/rdf-schema#Resource
http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://xmlns.com/foaf/0.1/Person
http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2002/07/owl#Thing
http://purl.org/dc/terms/contributor http://example.org/harry_potter
http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2000/10/swap/pim/contact#Person
http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://purl.org/dc/terms/Agent
http://rathachai.ch/rc/0.1/write http://example.org/harry_potter
http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2003/01/geo/wgs84_pos#SpatialThing
http://www.w3.org/2002/07/owl#sameAs http://example.org/people/jkrowling
http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://schema.org/Person
http://xmlns.com/foaf/0.1/maker http://example.org/harry_potter
http://purl.org/dc/terms/creator http://example.org/harry_potter
http://purl.org/dc/elements/1.1/creator http://example[...]<jupyter_text>To Query with well-known Terms<jupyter_code># Who are Person?

spql = 'SELECT ?x WHERE { ?x a foaf:Person .}'
res = g.query(spql, initNs={"foaf":FOAF, "pp":pp, "ex":"http://example.org/"})

for row in res:
  print(row["x"])
# What are Documents?

spql = 'SELECT ?x WHERE { ?x a foaf:Document .}'
res = g.query(spql, initNs={"foaf":FOAF, "pp":pp, "ex":"http://example.org/"})

for row in res:
  print(row["x"])
# Who write what?

spql = 'SELECT ?x ?y WHERE { ?x dcterms:creator ?y .}'
res = g.query(spql, initNs={"foaf":FOAF, "pp":pp, "ex":"http://example.org/", "dcterms":DCTERMS})

for row in res:
  print(row["x"], row["y"])
# What are boader Concepts of harry potter

spql = 'SELECT ?x WHERE { ex:harry_potter skos:broaderTransitive ?x .}'
res = g.query(spql, initNs={"foaf":FOAF, "pp":pp, "ex":"http://example.org/", "dcterms":DCTERMS, "skos":SKOS})

for row in res:
  print(row["x"])
# What are boader Concepts of Fantacy

spql = 'SELECT ?x WHERE { ex:Fantacy skos:broaderTransitive ?x .}'
res = g.query(spql, initNs={"foaf":FOAF, "pp":pp, "ex":"http://example.org/", "dcterms":DCTERMS, "skos":SKOS})

for row in res:
  print(row["x"])
# What are narrower Concepts of Fantacy

spql = 'SELECT ?x WHERE { ex:Fantacy skos:narrowerTransitive ?x .}'
res = g.query(spql, initNs={"foaf":FOAF, "pp":pp, "ex":"http://example.org/", "dcterms":DCTERMS, "skos":SKOS})

for row in res:
  print(row["x"])
# What are narrower Concepts of Story

spql = 'SELECT ?x WHERE { ex:Story skos:narrowerTransitive ?x .}'
res = g.query(spql, initNs={"foaf":FOAF, "pp":pp, "ex":"http://example.org/", "dcterms":DCTERMS, "skos":SKOS})

for row in res:
  print(row["x"])
# ASK

spql = 'ASK {ex:harry_potter skos:broaderTransitive ex:Story .}'
res = g.query(spql, initNs={"foaf":FOAF, "pp":pp, "ex":"http://example.org/", "dcterms":DCTERMS, "skos":SKOS})

for row in res:
  print(row)<jupyter_output>True<jupyter_text>To Query from other SPARQL Endpoints<jupyter_code>ns = '''
PREFIX owl: <http://www.w3.org/2002/07/owl#> 
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> 
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
PREFIX foaf: <http://xmlns.com/foaf/0.1/> 
PREFIX dc: <http://purl.org/dc/elements/1.1/> 
PREFIX : <http://dbpedia.org/resource/> 
PREFIX ont: <http://dbpedia.org/ontology/>
PREFIX dbpedia2: <http://dbpedia.org/property/> 
PREFIX dbpedia: <http://dbpedia.org/> 
PREFIX skos: <http://www.w3.org/2004/02/skos/core#> 
'''
sparql = SPARQLWrapper("http://dbpedia.org/sparql")
sparql.setQuery("""
PREFIX ont: <http://dbpedia.org/ontology/>

SELECT ?a 
WHERE {<http://dbpedia.org/resource/Apple_Inc.> ont:product ?a }

""")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()

for result in results["results"]["bindings"]:
    print(result["a"]["value"])
sparql.setQuery("""
PREFIX ont: <http://dbpedia.org/ontology/>

SELECT ?a 
WHERE {
  <http://dbpedia.org/resource/Apple_Inc.> ont:product ?a . 
  ?a ont:operatingSystem <http://dbpedia.org/resource/IOS> .
  }

""")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()

for result in results["results"]["bindings"]:
    print(result["a"]["value"])
sparql.setQuery("""
PREFIX ont: <http://dbpedia.org/ontology/>

ASK {
  <http://dbpedia.org/resource/Apple_Inc.> ont:product <http://dbpedia.org/resource/IPhone> . 
  }

""")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()

print(results["boolean"])
sparql.setQuery("""
PREFIX ont: <http://dbpedia.org/ontology/>

CONSTRUCT { <http://dbpedia.org/resource/Apple_Inc.> ont:product ?a . }
WHERE {<http://dbpedia.org/resource/Apple_Inc.> ont:product ?a }

""")
sparql.setReturnFormat(TURTLE)
results = sparql.query().convert()

g = rdflib.Graph().parse(data=results, format="turtle")

print(g.serialize(format="turtle").decode("utf-8"))<jupyter_output>@prefix dbo: <http://dbpedia.org/ontology/> .
@prefix dbr: <http://dbpedia.org/resource/> .

<http://dbpedia.org/resource/Apple_Inc.> dbo:product dbr:Apple_TV,
        dbr:Apple_Watch,
        dbr:Final_Cut_Pro,
        dbr:GarageBand,
        dbr:HomePod,
        dbr:ILife,
        dbr:IOS,
        dbr:IPad,
        dbr:IPadOS,
        dbr:IPhone,
        dbr:IPod,
        dbr:IWork,
        dbr:Logic_Pro,
        dbr:MacOS,
        dbr:Macintosh,
        <http://dbpedia.org/resource/Shazam_(application)>,
        dbr:Siri,
        dbr:TvOS,
        dbr:WatchOS .
{'fraction_non_alphanumeric': 0.20908326324642557, 'fraction_numerical': 0.02253994953742641, 'mean_word_length': 4.289368327402135, 'pattern_counts': {'":': 56, '<': 221, '<?xml version=': 0, '>': 232, 'https://': 8, 'lorem ipsum': 0, 'www.': 49, 'xml': 37}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '25', 'dir': 'jupyter-structured-clean-dedup', 'id': '467365', 'license': 'MIT', 'n_tokens_mistral': 10419, 'n_tokens_neox': 9899, 'n_words': 1482, 'path': 'semantic-web-101/quick_tutorial_semantic.ipynb', 'repo_name': 'Rathachai/semantic-tech', 'size': '23817'}