annotate rdfpy/writeMarsyasOnto.py @ 18:d5012016bf64 tip

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