generated from FacultadInformatica-LinkedData/Template-Curso
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathtask06.py
78 lines (57 loc) · 2.11 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
# -*- coding: utf-8 -*-
"""Copia de Task06.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1NeiN_69qOYYv_WBRhyIlPEDfXXojusNw
**Task 06: Modifying RDF(s)**
"""
!pip install rdflib
github_storage = "https://raw.githubusercontent.com/FacultadInformatica-LinkedData/Curso2021-2022/master/Assignment4/course_materials"
"""Leemos el fichero RDF de la forma que lo hemos venido haciendo"""
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"**
"""
# TO DO
# Visualize the results
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"**"""
# TO DO
# Visualize the results
g.add((ns.Researcher, RDFS.subClassOf, ns.Person))
for s, p, o in g:
print(s,p,o)
"""**TASK 6.3: Create a new individual of Researcher named "Jane Smith"**"""
# TO DO
# Visualize the results
g.add((ns.JaneSmith, RDF.type,ns.Researcher))
for s, p, o in g:
print(s,p,o)
"""**TASK 6.4: Add to the individual JaneSmith the fullName, given and family names**"""
# TO DO
# Visualize the results
vcard = Namespace("http://www.w3.org/2001/vcard-rdf/3.0#")
g.add((ns.JaneSmith, vcard.fullName, Literal("Jane Smith")))
g.add((ns.JaneSmith, vcard.given, Literal("Jane")))
g.add((ns.JaneSmith, vcard.familynames, Literal("Smith")))
for s, p, o in g:
print(s,p,o)
"""**TASK 6.5: Add UPM as the university where John Smith works**"""
# TO DO
# Visualize the results
vcard = Namespace("http://www.w3.org/2001/vcard-rdf/3.0#")
g.add((ns.UPM, RDF.type, ns.University))
g.add((ns.JohnSmith,vcard.work,ns.UPM))
for s, p, o in g:
print(s,p,o)