generated from FacultadInformatica-LinkedData/Template-Curso
-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathtask06.py
89 lines (63 loc) · 2.56 KB
/
task06.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# -*- coding: utf-8 -*-
"""Task06.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1AGtv0oZs6zvOpAedq4hq_UOZhX-gMxN5
**Task 06: Modifying RDF(s)**
"""
!pip install rdflib
github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2024-2025/master/Assignment4/course_materials"
"""Read the RDF file as shown in class"""
from rdflib import Graph, Namespace, Literal
from rdflib.namespace import RDF, RDFS
g = Graph()
g.namespace_manager.bind('ns', Namespace("http://somewhere#"), override=False)
g.namespace_manager.bind('vcard', Namespace("http://www.w3.org/2001/vcard-rdf/3.0#"), override=False)
g.parse(github_storage+"/rdf/example5.rdf", format="xml")
"""Create a new class named Researcher"""
ns = Namespace("http://somewhere#")
g.add((ns.Researcher, RDF.type, RDFS.Class))
for s, p, o in g:
print(s,p,o)
"""**TASK 6.1: Create a new class named "University"**
"""
g.add((ns.University, RDF.type, RDFS.Class))
for s, p, o in g:
print(s,p,o)
"""**TASK 6.2: Add "Researcher" as a subclass of "Person"**"""
g.add((ns.Researcher, RDFS.subClassOf, ns.Person))
# Visualize the results
for s, p, o in g:
print(s, p, o)
"""**TASK 6.3: Create a new individual of Researcher named "Jane Smithers"**"""
# TO DO
jane_smithers = ns.JaneSmithers
g.add((jane_smithers, RDF.type, ns.Researcher))
for s, p, o in g:
print(s,p,o)
"""**TASK 6.4: Add to the individual JaneSmithers the email address, fullName, given and family names. Use the https://schema.org vocabulary**"""
# TO DO
schema = Namespace("https://schema.org/Person")
g.add((jane_smithers, schema.email, Literal("[email protected]")))
g.add((jane_smithers, schema.name, Literal("Jane Smithers")))
g.add((jane_smithers, schema.givenName, Literal("Jane")))
g.add((jane_smithers, schema.familyName, Literal("Smithers")))
for s, p, o in g:
print(s, p, o)
"""**TASK 6.5: Add UPM as the university where John Smith works. Use the "https://example.org/ namespace**"""
# TO DO
ex = Namespace("https://example.org/")
upm = ns.UPM
g.add((upm, RDF.type, ns.University))
g.add((ns.JohnSmith, ex.worksAt, upm))
# Visualize the results
for s, p, o in g:
print(s, p, o)
"""**Task 6.6: Add that Jown knows Jane using the FOAF vocabulary. Make sure the relationship exists.**"""
# TO DO
foaf = Namespace("http://xmlns.com/foaf/0.1/")
g.add((ns.JohnSmith, foaf.knows, jane_smithers))
# Visualize the results
for s, p, o in g:
if s == ns.JohnSmith and p == foaf.knows and o == jane_smithers:
print(f"{s} {p} {o} - Relationship exists")