comparison rdfpy/af-marsyas.py @ 18:d5012016bf64 tip

added rdfpy and rdfonto directories
author nothing@tehis.net
date Tue, 23 Apr 2013 11:49:20 +0100
parents
children
comparison
equal deleted inserted replaced
17:2b5c292ad12f 18:d5012016bf64
1 import rdflib
2 from rdflib import Graph, RDF, RDFS, plugin, URIRef, Literal, OWL, Namespace
3
4 basedir = '/Users/alo/MusicOntology/features/'
5
6 local = 'http://sovarr.c4dm.eecs.qmul.ac.uk/features/'
7
8 DC = Namespace(u"http://purl.org/dc/elements/1.1/")
9
10 graph = Graph()
11 graph.bind('af', URIRef(local))
12 graph.bind('dc', URIRef('http://purl.org/dc/elements/1.1/'))
13 graph.bind('owl', URIRef('http://www.w3.org/2002/07/owl#'))
14
15 graph.add((
16 URIRef(''),
17 RDF.type,
18 OWL.Ontology
19 ))
20
21 graph.add((
22 URIRef(''),
23 DC['title'],
24 Literal("Marsyas Ontology")
25 ))
26
27 graph.add((
28 URIRef(''),
29 OWL.versionInfo,
30 Literal("Version 0.1")
31 ))
32
33 graph.add((
34 URIRef(''),
35 DC['description'],
36 Literal("This is an ontology derived from Marsyas feature extraction tools for the Audio Features engineering process")
37 ))
38
39
40 source = Graph()
41 source.parse(basedir+'rdfonto/af-Marsyas.n3', format='n3')
42
43 categories = []
44
45 for su, ob in source.subject_objects(URIRef('file:///Users/alo/MusicOntology/features/rdf/type')):
46 if not ob in categories:
47 categories.append(ob)
48
49 graph.add((
50 URIRef(local+'Analysis'),
51 RDF.type,
52 OWL.Class
53 ))
54
55 for category in categories:
56 graph.add((
57 URIRef(local+category),
58 RDF.type,
59 OWL.Class
60 ))
61 graph.add((
62 URIRef(local+category),
63 RDFS.subClassOf,
64 URIRef(local+'Analysis'),
65 ))
66
67 for su in source.subjects(RDF.type, RDFS.Resource):
68 idref = URIRef(local+su.split('/')[-1])
69 # graph.add((
70 # idref,
71 # RDF.type,
72 # OWL.Class
73 # ))
74
75 graph.add((
76 idref,
77 RDF.type,
78 URIRef(local+"AudioFeature")
79 ))
80
81 count = sum(1 for _ in source.objects(su,URIRef('file:///Users/alo/MusicOntology/features/rdf/type')))
82
83 if count > 0:
84 for ob in source.objects(su,URIRef('file:///Users/alo/MusicOntology/features/rdf/type')):
85 graph.add((
86 idref,
87 RDFS.subClassOf,
88 URIRef(local+ob)
89 ))
90 else:
91 graph.add((
92 idref,
93 RDFS.subClassOf,
94 URIRef(local+'Analysis'),
95 ))
96
97 for ob in source.objects(su,URIRef('http://purl.org/dc/elements/1.1/description')):
98 graph.add((
99 idref,
100 URIRef('http://purl.org/dc/elements/1.1/description'),
101 ob
102 ))
103
104 graph.serialize(basedir + 'rdfonto/Marsyas-onto.rdf')
105 graph.serialize(basedir + 'rdfonto/Marsyas-onto.n3', format='n3')