annotate rdfpy/af-mirtoolbox.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, os
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 orig = 'http://sovarr.c4dm.eecs.qmul.ac.uk/features/'
nothing@18 7
nothing@18 8 local = Namespace('http://sovarr.c4dm.eecs.qmul.ac.uk/af/mir/MIRToolbox/1.0#')
nothing@18 9
nothing@18 10 af = Namespace('http://sovarr.c4dm.eecs.qmul.ac.uk/af/ontology/1.0#')
nothing@18 11
nothing@18 12 afv = Namespace('http://sovarr.c4dm.eecs.qmul.ac.uk/af/vocabulary/1.0#')
nothing@18 13
nothing@18 14 DC = Namespace("http://purl.org/dc/elements/1.1/")
nothing@18 15
nothing@18 16 vs = Namespace('http://www.w3.org/2003/06/sw-vocab-status/ns#')
nothing@18 17
nothing@18 18 afskos = Namespace(url + '/vocabulary/skos/1.0#')
nothing@18 19
nothing@18 20 skos = Namespace('http://www.w3.org/2004/02/skos/core#')
nothing@18 21
nothing@18 22 graph = Graph()
nothing@18 23 graph.bind('af', af)
nothing@18 24 graph.bind('afv', afv)
nothing@18 25 graph.bind('mirtoolbox', local)
nothing@18 26 graph.bind('dc', DC)
nothing@18 27 graph.bind('owl', OWL)
nothing@18 28 graph.bind('vs', vs)
nothing@18 29 graph.bind('skos', skos)
nothing@18 30 graph.bind('afsv', afskos)
nothing@18 31
nothing@18 32 graph.add((
nothing@18 33 URIRef(''),
nothing@18 34 RDF.type,
nothing@18 35 OWL.Ontology
nothing@18 36 ))
nothing@18 37
nothing@18 38 graph.add((
nothing@18 39 URIRef(''),
nothing@18 40 DC['title'],
nothing@18 41 Literal("MIR Toolbox Ontology", lang="en")
nothing@18 42 ))
nothing@18 43
nothing@18 44 graph.add((
nothing@18 45 URIRef(''),
nothing@18 46 OWL.versionInfo,
nothing@18 47 Literal("Version 1.0", lang="en")
nothing@18 48 ))
nothing@18 49
nothing@18 50 graph.add((
nothing@18 51 URIRef(''),
nothing@18 52 DC['description'],
nothing@18 53 Literal("This is an ontology for annotating MIR Toolbox audio features", lang="en")
nothing@18 54 ))
nothing@18 55
nothing@18 56 graph.add(( vs['term_status'], RDF.type, OWL.AnnotationProperty ))
nothing@18 57
nothing@18 58 source = Graph()
nothing@18 59 source.parse(basedir+'rdfonto/MIRToolbox.rdf')
nothing@18 60
nothing@18 61 '''
nothing@18 62 graph.add((
nothing@18 63 URIRef(local+'Operator'),
nothing@18 64 RDF.type,
nothing@18 65 OWL.Class
nothing@18 66 ))
nothing@18 67 graph.add((
nothing@18 68 URIRef(local+'FeatureExtractor'),
nothing@18 69 RDF.type,
nothing@18 70 OWL.Class
nothing@18 71 ))
nothing@18 72 graph.add((
nothing@18 73 URIRef(local+'HighLevelFeature'),
nothing@18 74 RDF.type,
nothing@18 75 OWL.Class
nothing@18 76 ))
nothing@18 77
nothing@18 78 for name in ['Structure', 'Statistics', 'Predictions', 'Similarity']:
nothing@18 79 idref = URIRef(local+name)
nothing@18 80 graph.add((
nothing@18 81 idref,
nothing@18 82 RDF.type,
nothing@18 83 OWL.Class
nothing@18 84 ))
nothing@18 85 graph.add((
nothing@18 86 idref,
nothing@18 87 RDFS.subClassOf,
nothing@18 88 URIRef(local+'HighLevelFeature')
nothing@18 89 ))
nothing@18 90
nothing@18 91
nothing@18 92 for name in ['Dynamics', 'Rhythm', 'Timbre', 'Pitch', 'Tonality']:
nothing@18 93 idref = URIRef(local+name+'FeatureExtractor')
nothing@18 94 graph.add((
nothing@18 95 idref,
nothing@18 96 RDF.type,
nothing@18 97 OWL.Class
nothing@18 98 ))
nothing@18 99 graph.add((
nothing@18 100 idref,
nothing@18 101 RDFS.subClassOf,
nothing@18 102 URIRef(local+'FeatureExtractor')
nothing@18 103 ))
nothing@18 104 '''
nothing@18 105 for su in source.subjects(RDF.type, RDFS.Resource):
nothing@18 106 name = su.split('/')[-1]
nothing@18 107 idref = URIRef(local + name)
nothing@18 108 graph.add((
nothing@18 109 idref,
nothing@18 110 RDF.type,
nothing@18 111 OWL.Class
nothing@18 112 ))
nothing@18 113
nothing@18 114 graph.add((
nothing@18 115 idref,
nothing@18 116 OWL.sameAs,
nothing@18 117 afv[su.split('/')[-1]]
nothing@18 118 ))
nothing@18 119
nothing@18 120 graph.add((idref, vs['term_status'], Literal("testing", lang="en") ))
nothing@18 121
nothing@18 122 graph.add(( idref, RDFS.label, Literal(name, lang="en") ))
nothing@18 123
nothing@18 124 graph.add(( idref, RDFS.comment, Literal( name + " MIR Toolbox audio feature") ))
nothing@18 125
nothing@18 126 count=sum(1 for _ in source.objects(su, URIRef(orig+'tag')))
nothing@18 127
nothing@18 128 if count == 1:
nothing@18 129 for it in source.objects(su, URIRef(orig+'tag')):
nothing@18 130 graph.add((
nothing@18 131 idref,
nothing@18 132 RDFS.subClassOf,
nothing@18 133 af[it+'FeatureExtractor']
nothing@18 134 ))
nothing@18 135
nothing@18 136 count = sum(1 for _ in source.objects(su, URIRef(orig+'group')))
nothing@18 137
nothing@18 138 if count == 1:
nothing@18 139 for it in source.objects(su, URIRef(orig+'group')):
nothing@18 140 graph.add((
nothing@18 141 idref,
nothing@18 142 RDFS.subClassOf,
nothing@18 143 af[it]
nothing@18 144 ))
nothing@18 145
nothing@18 146 count=sum(1 for _ in source.objects(su, URIRef(orig+'type')))
nothing@18 147
nothing@18 148 if count == 1:
nothing@18 149 for it in source.objects(su, URIRef(orig+'type')):
nothing@18 150 if it == "Operator":
nothing@18 151 graph.add((
nothing@18 152 idref,
nothing@18 153 RDFS.subClassOf,
nothing@18 154 af[it]
nothing@18 155 ))
nothing@18 156
nothing@18 157 graph.serialize(basedir + 'rdfonto/af-mirtoolbox.rdf')
nothing@18 158 graph.serialize(basedir + 'rdfonto/af-mirtoolbox.n3', format='n3')