annotate rdfpy/writeRDFs.py @ 18:d5012016bf64 tip

added rdfpy and rdfonto directories
author nothing@tehis.net
date Tue, 23 Apr 2013 11:49:20 +0100
parents 62d2c72e4223
children
rev   line source
nothing@0 1 import rdflib, os, fnmatch, urllib2
nothing@0 2 from rdflib import Graph, RDF, RDFS, plugin, URIRef, Literal, OWL
nothing@0 3 from xml.dom.minidom import parseString
nothing@0 4
nothing@0 5 ############# Vamp ###############
nothing@0 6
nothing@0 7 vampdir = '/Users/alo/Library/Audio/Plug-Ins/Vamp/'
nothing@0 8
nothing@0 9 source = Graph()
nothing@0 10
nothing@0 11 graph = Graph()
nothing@0 12 local = 'http://sovarr.c4dm.eecs.qmul.ac.uk/features/'
nothing@0 13 graph.bind('local', URIRef(local))
nothing@0 14 graph.bind('dc', URIRef('http://purl.org/dc/elements/1.1/'))
nothing@0 15
nothing@0 16 for name in os.listdir(vampdir):
nothing@0 17 if fnmatch.fnmatch(name, '*.n3'):
nothing@0 18
nothing@0 19 print (vampdir + name)
nothing@0 20
nothing@0 21 source.parse(vampdir + name, format='n3')
nothing@0 22
nothing@0 23 for su, pr in source.subject_predicates(URIRef('http://purl.org/ontology/vamp/Plugin')):
nothing@0 24
nothing@0 25 for name in source.objects(su, URIRef('http://purl.org/ontology/vamp/name')):
nothing@0 26 id = name.replace(' ', '').replace('Marsyas-BatchFeatureExtract-', '')
nothing@0 27 feature = name
nothing@0 28 graph.add((
nothing@0 29 URIRef(id),
nothing@0 30 URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
nothing@0 31 URIRef('http://www.w3.org/2000/01/rdf-schema#Resource')
nothing@0 32 ))
nothing@0 33 graph.add((
nothing@0 34 URIRef(id),
nothing@0 35 URIRef(local+'feature'),
nothing@0 36 Literal(feature)
nothing@0 37 ))
nothing@0 38 for domain in source.objects(su, URIRef('http://purl.org/ontology/vamp/input_domain')):
nothing@0 39 dom = domain.split('/')[-1].replace('Domain', '')
nothing@0 40 dom = dom.lower().replace('Time', 'temporal')
nothing@0 41 graph.add((
nothing@0 42 URIRef(id),
nothing@0 43 URIRef(local+'domain'),
nothing@0 44 Literal(dom)
nothing@0 45 ))
nothing@0 46 for desc in source.objects(su, URIRef('http://purl.org/dc/elements/1.1/description')):
nothing@0 47 description = " ".join(desc.split())
nothing@0 48 graph.add((
nothing@0 49 URIRef(id),
nothing@0 50 URIRef('http://purl.org/dc/elements/1.1/description'),
nothing@0 51 Literal(description)
nothing@0 52 ))
nothing@0 53 for maker in source.objects(su, URIRef('http://xmlns.com/foaf/0.1/maker')):
nothing@0 54 for mname in source.objects(maker, URIRef('http://xmlns.com/foaf/0.1/name')):
nothing@0 55 makername = mname
nothing@0 56 graph.add((
nothing@0 57 URIRef(id),
nothing@0 58 URIRef(local+'source'),
nothing@0 59 Literal(makername)
nothing@0 60 ))
nothing@0 61
nothing@0 62 count=sum(1 for _ in source.objects(su, URIRef('http://purl.org/ontology/vamp/output')))
nothing@0 63
nothing@0 64 if count == 1:
nothing@0 65 for it in source.objects(su, URIRef('http://purl.org/ontology/vamp/output')):
nothing@0 66 for output in source.objects(it, URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type')):
nothing@0 67 out = output.split('/')[-1].replace('Output', '')
nothing@0 68 if out.find('Sparse') >= 0 or out.find('Dense') >= 0:
nothing@0 69 graph.add((
nothing@0 70 URIRef(id),
nothing@0 71 URIRef(local+'output'),
nothing@0 72 Literal(out)
nothing@0 73 ))
nothing@0 74
nothing@0 75 else:
nothing@0 76 for it in source.objects(su, URIRef('http://purl.org/ontology/vamp/output')):
nothing@0 77 for name in source.objects(it, URIRef('http://purl.org/dc/elements/1.1/title')):
nothing@0 78 if name != feature:
nothing@0 79 subid = name.replace(' ', '')
nothing@0 80 graph.add((
nothing@0 81 URIRef(subid),
nothing@0 82 URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
nothing@0 83 URIRef('http://www.w3.org/2000/01/rdf-schema#Resource')
nothing@0 84 ))
nothing@0 85 graph.add((
nothing@0 86 URIRef(subid),
nothing@0 87 URIRef(local+'feature'),
nothing@0 88 Literal(name + " (" + feature + ")")
nothing@0 89 ))
nothing@0 90 graph.add((
nothing@0 91 URIRef(subid),
nothing@0 92 URIRef(local+'domain'),
nothing@0 93 Literal(dom)
nothing@0 94 ))
nothing@0 95 graph.add((
nothing@0 96 URIRef(subid),
nothing@0 97 URIRef(local+'source'),
nothing@0 98 Literal(makername)
nothing@0 99 ))
nothing@0 100 for output in source.objects(it, URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type')):
nothing@0 101 out = output.split('/')[-1].replace('Output', '')
nothing@0 102 if out.find('Sparse') >= 0 or out.find('Dense') >= 0:
nothing@0 103 graph.add((
nothing@0 104 URIRef(subid),
nothing@0 105 URIRef(local+'output'),
nothing@0 106 Literal(out)
nothing@0 107 ))
nothing@0 108
nothing@0 109 graph.serialize('/Users/alo/MusicOntology/features/rdf/af-Vamp.rdf')
nothing@0 110
nothing@0 111 ############# Marsyas ###############
nothing@0 112 mdir = '/Users/alo/Development/MIR/marsyas-0.4.7/src/marsyas/'
nothing@0 113
nothing@0 114 madict = {}
nothing@0 115
nothing@0 116 graph = Graph()
nothing@0 117 local = 'http://sovarr.c4dm.eecs.qmul.ac.uk/features/'
nothing@0 118 graph.bind('local', URIRef(local))
nothing@0 119 graph.bind('dc', URIRef('http://purl.org/dc/elements/1.1/'))
nothing@0 120
nothing@0 121 for name in os.listdir(mdir):
nothing@0 122 if fnmatch.fnmatch(name, '*.h'):
nothing@0 123 code = [line.strip() for line in open(mdir + name)]
nothing@0 124 found = False
nothing@0 125 for line in code:
nothing@0 126 if line.find('\ingroup Analysis') >= 0:
nothing@0 127 found = True
nothing@0 128 break
nothing@0 129
nothing@0 130 if found:
nothing@0 131 i = 0
nothing@0 132 cl = ''
nothing@0 133 for line in code:
nothing@0 134 if line.find('\class') >= 0:
nothing@0 135 cl = line.split(' ')[-1]
nothing@0 136 madict[cl] = {'brief': code[i+2][7:]}
nothing@0 137 if code[i+3] != '':
nothing@0 138 madict[cl]['brief'] += code[i+3]
nothing@0 139
nothing@0 140 break
nothing@0 141
nothing@0 142 i += 1
nothing@0 143
nothing@0 144 graph.add((
nothing@0 145 URIRef(cl),
nothing@0 146 URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
nothing@0 147 URIRef('http://www.w3.org/2000/01/rdf-schema#Resource')
nothing@0 148 ))
nothing@0 149
nothing@0 150 graph.add((
nothing@0 151 URIRef(cl),
nothing@0 152 URIRef('http://purl.org/dc/elements/1.1/description'),
nothing@0 153 Literal(madict[cl]['brief'])
nothing@0 154 ))
nothing@0 155
nothing@0 156 graph.serialize('/Users/alo/MusicOntology/features/rdf/af-Marsyas.rdf')
nothing@0 157
nothing@0 158 ############# jMIR ###############
nothing@0 159 jdir = '/Users/alo/Development/MIR/jAudio/jAudio/'
nothing@0 160 jsrc = '/Users/alo/Development/MIR/jAudio/jAudio/src/jAudioFeatureExtractor/AudioFeatures/'
nothing@0 161
nothing@0 162 file = urllib2.urlopen('file://' + jdir + 'features.xml')
nothing@0 163 data = file.read()
nothing@0 164 file.close()
nothing@0 165
nothing@0 166 dom = parseString(data)
nothing@0 167 jmir = dom.getElementsByTagName('feature')
nothing@0 168
nothing@0 169 graph = Graph()
nothing@0 170 local = 'http://sovarr.c4dm.eecs.qmul.ac.uk/features/'
nothing@0 171 graph.bind('local', URIRef(local))
nothing@0 172 graph.bind('dc', URIRef('http://purl.org/dc/elements/1.1/'))
nothing@0 173
nothing@0 174 for nodes in jmir:
nothing@0 175 jname = nodes.childNodes[1].firstChild.nodeValue.split('.')[-1]
nothing@0 176 graph.add((
nothing@0 177 URIRef(jname),
nothing@0 178 URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
nothing@0 179 URIRef('http://www.w3.org/2000/01/rdf-schema#Resource')
nothing@0 180 ))
nothing@0 181 file = open(jsrc + jname + '.java')
nothing@0 182 code = file.read()
nothing@0 183 searchstr = 'String name ='
nothing@0 184 start = code.find(searchstr) + len(searchstr)
nothing@0 185 start = code.find('"', start) + 1
nothing@0 186 end = code.find('"', start)
nothing@0 187 name = code[start:end]
nothing@0 188
nothing@0 189 if name > "":
nothing@0 190 graph.add((
nothing@0 191 URIRef(jname),
nothing@0 192 URIRef(local+'name'),
nothing@0 193 Literal(name)
nothing@0 194 ))
nothing@0 195
nothing@0 196 searchstr = 'String description'
nothing@0 197 start = code.find(searchstr) + len(searchstr)
nothing@0 198 start = code.find('"', start) + 1
nothing@0 199 end = code.find('";', start)
nothing@0 200 desc = code[start:end]
nothing@0 201 desc = desc.replace("
", "").replace("\t", "").replace('\n', '').replace('+', '').replace('"', '').replace(';', '').replace('//\n', '')
nothing@0 202 desc = " ".join(desc.split())
nothing@0 203
nothing@0 204 if desc > "":
nothing@0 205 graph.add((
nothing@0 206 URIRef(jname),
nothing@0 207 URIRef('http://purl.org/dc/elements/1.1/description'),
nothing@0 208 Literal(desc)
nothing@0 209 ))
nothing@0 210
nothing@0 211
nothing@0 212 graph.serialize('/Users/alo/MusicOntology/features/rdf/af-jMIR.rdf')
nothing@0 213
nothing@0 214
nothing@0 215 ############# yaafe ###############
nothing@0 216
nothing@0 217 graph = Graph()
nothing@0 218 local = 'http://sovarr.c4dm.eecs.qmul.ac.uk/features/'
nothing@0 219 graph.bind('local', URIRef(local))
nothing@0 220 graph.bind('dc', URIRef('http://purl.org/dc/elements/1.1/'))
nothing@0 221
nothing@0 222 path = "/Users/alo/Development/MIR/yaafe-v0.64/src_python/yaafefeatures.py"
nothing@0 223
nothing@0 224 lines = [line.strip() for line in open(path)]
nothing@0 225
nothing@0 226 count = 0
nothing@0 227
nothing@0 228 for ln in lines:
nothing@0 229 if ln.find('class ') >= 0:
nothing@0 230 yname = ln[6:ln.find('(AudioFeature)')]
nothing@0 231 desc = lines[count+2]
nothing@0 232 desc = desc.replace("`", "").replace("<", "").replace(">", "")
nothing@0 233 desc = " ".join(desc.split())
nothing@0 234
nothing@0 235 graph.add((
nothing@0 236 URIRef(yname),
nothing@0 237 URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
nothing@0 238 URIRef('http://www.w3.org/2000/01/rdf-schema#Resource')
nothing@0 239 ))
nothing@0 240
nothing@0 241 graph.add((
nothing@0 242 URIRef(yname),
nothing@0 243 URIRef('http://purl.org/dc/elements/1.1/description'),
nothing@0 244 Literal(desc)
nothing@0 245 ))
nothing@0 246
nothing@0 247 count += 1
nothing@0 248
nothing@0 249 graph.serialize('/Users/alo/MusicOntology/features/rdf/af-Yaafe.rdf')
nothing@0 250
nothing@0 251 ############# libXtract ###############
nothing@0 252 graph = Graph()
nothing@0 253 local = 'http://sovarr.c4dm.eecs.qmul.ac.uk/features/'
nothing@0 254 graph.bind('local', URIRef(local))
nothing@0 255 graph.bind('dc', URIRef('http://purl.org/dc/elements/1.1/'))
nothing@0 256
nothing@0 257 path = '/Users/alo/Development/MIR/LibXtract/xtract/libxtract.h'
nothing@0 258
nothing@0 259 lines = [line.strip() for line in open(path)]
nothing@0 260
nothing@0 261 xtract = lines[(lines.index('enum xtract_features_ {')+1):(lines.index('XTRACT_WINDOWED')-1)]
nothing@0 262
nothing@0 263 for ln in xtract:
nothing@0 264 xname = ln[(ln.index('_')+1):-1].replace("_", " ").lower().capitalize()
nothing@0 265
nothing@0 266 graph.add((
nothing@0 267 URIRef(xname),
nothing@0 268 URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
nothing@0 269 URIRef('http://www.w3.org/2000/01/rdf-schema#Resource')
nothing@0 270 ))
nothing@0 271
nothing@0 272 graph.serialize('/Users/alo/MusicOntology/features/rdf/af-libXtract.rdf')