annotate rdfpy/writeMIRToolboxOnto.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, os
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("MIR Toolbox 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 MIR Toolbox for the Audio Features engineering process")
nothing@0 37 ))
nothing@0 38
nothing@0 39 source = Graph()
nothing@0 40 source.parse(basedir+'rdfonto/af-MIRToolbox.rdf')
nothing@0 41
nothing@0 42 graph.add((
nothing@0 43 URIRef(local+'Operator'),
nothing@0 44 RDF.type,
nothing@0 45 OWL.Class
nothing@0 46 ))
nothing@0 47 graph.add((
nothing@0 48 URIRef(local+'FeatureExtractor'),
nothing@0 49 RDF.type,
nothing@0 50 OWL.Class
nothing@0 51 ))
nothing@0 52 graph.add((
nothing@0 53 URIRef(local+'HighLevelFeature'),
nothing@0 54 RDF.type,
nothing@0 55 OWL.Class
nothing@0 56 ))
nothing@0 57
nothing@0 58 for name in ['Structure', 'Statistics', 'Predictions', 'Similarity']:
nothing@0 59 idref = URIRef(local+name)
nothing@0 60 graph.add((
nothing@0 61 idref,
nothing@0 62 RDF.type,
nothing@0 63 OWL.Class
nothing@0 64 ))
nothing@0 65 graph.add((
nothing@0 66 idref,
nothing@0 67 RDFS.subClassOf,
nothing@0 68 URIRef(local+'HighLevelFeature')
nothing@0 69 ))
nothing@0 70
nothing@0 71
nothing@0 72 for name in ['Dynamics', 'Rhythm', 'Timbre', 'Pitch', 'Tonality']:
nothing@0 73 idref = URIRef(local+name+'FeatureExtractor')
nothing@0 74 graph.add((
nothing@0 75 idref,
nothing@0 76 RDF.type,
nothing@0 77 OWL.Class
nothing@0 78 ))
nothing@0 79 graph.add((
nothing@0 80 idref,
nothing@0 81 RDFS.subClassOf,
nothing@0 82 URIRef(local+'FeatureExtractor')
nothing@0 83 ))
nothing@0 84
nothing@0 85 for su in source.subjects(RDF.type, RDFS.Resource):
nothing@0 86 idref = URIRef(local + su.split('/')[-1])
nothing@15 87 # graph.add((
nothing@15 88 # idref,
nothing@15 89 # RDF.type,
nothing@15 90 # OWL.Class
nothing@15 91 # ))
nothing@15 92
nothing@0 93 graph.add((
nothing@0 94 idref,
nothing@0 95 RDF.type,
nothing@15 96 URIRef(local+"AudioFeature")
nothing@0 97 ))
nothing@0 98
nothing@0 99 count=sum(1 for _ in source.objects(su, URIRef(local+'tag')))
nothing@0 100
nothing@0 101 if count == 1:
nothing@0 102 for it in source.objects(su, URIRef(local+'tag')):
nothing@0 103 graph.add((
nothing@0 104 idref,
nothing@0 105 RDFS.subClassOf,
nothing@0 106 URIRef(local+it+'FeatureExtractor')
nothing@0 107 ))
nothing@0 108
nothing@0 109 count = sum(1 for _ in source.objects(su, URIRef(local+'group')))
nothing@0 110
nothing@0 111 if count == 1:
nothing@0 112 for it in source.objects(su, URIRef(local+'group')):
nothing@0 113 graph.add((
nothing@0 114 idref,
nothing@0 115 RDFS.subClassOf,
nothing@0 116 URIRef(local+it)
nothing@0 117 ))
nothing@0 118
nothing@0 119 count=sum(1 for _ in source.objects(su, URIRef(local+'type')))
nothing@0 120
nothing@0 121 if count == 1:
nothing@0 122 for it in source.objects(su, URIRef(local+'type')):
nothing@0 123 if it == "Operator":
nothing@0 124 graph.add((
nothing@0 125 idref,
nothing@0 126 RDFS.subClassOf,
nothing@0 127 URIRef(local+it)
nothing@0 128 ))
nothing@0 129
nothing@0 130 graph.serialize(basedir + 'rdfonto/MIR-onto.rdf')
nothing@0 131 graph.serialize(basedir + 'rdfonto/MIR-onto.n3', format='n3')