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