comparison pdfextract/makeOwlGraph.py @ 1:365a37a2fb6c

added files from pdfextract directory
author nothing@tehis.net
date Mon, 25 Feb 2013 14:47:41 +0000
parents
children
comparison
equal deleted inserted replaced
0:62d2c72e4223 1:365a37a2fb6c
1 import rdflib, os, fnmatch, urllib2
2 from rdflib import Graph, RDF, RDFS, plugin, URIRef, Literal, OWL
3 from xml.dom.minidom import parseString
4
5 names = [line.strip() for line in open('pdfextract/names.txt')]
6 cat = [line.strip() for line in open('pdfextract/categories.txt')]
7 sig = [line.strip() for line in open('pdfextract/sig.txt')]
8
9 local = 'http://sovarr.c4dm.eecs.qmul.ac.uk/features/'
10
11 afgr = Graph()
12 afgr.bind('local', URIRef(local))
13 afgr.bind('dc', URIRef('http://purl.org/dc/elements/1.1/'))
14 afgr.bind('owl', URIRef('http://www.w3.org/2002/07/owl#'))
15
16 # def split_uppercase(string):
17 # x=''
18 # for i in string:
19 # if i.isupper():
20 # x+=' %s' %i
21 # else:
22 # x+=i
23 # return x.strip()
24
25 i = 0
26
27 order = [
28 "Zero Crossing Rate",
29 "Linear Predictive Coding",
30 "Mel-scale Frequency Cepstral Coefficients",
31 "Auditory Filter Bank Temporal Envelopes",
32 "Rate-scale-frequency Features",
33 "Phase Space Features"
34 ]
35
36 domains = {
37 "Zero Crossing Rate": 'temporal',
38 "Linear Predictive Coding": 'frequency',
39 "Mel-scale Frequency Cepstral Coefficients": 'cepstral',
40 "Auditory Filter Bank Temporal Envelopes": 'modulation frequency',
41 "Rate-scale-frequency Features": 'eigendomain',
42 "Phase Space Features": 'phase space'
43 }
44
45 abbr = {
46 "Zero Crossing Rate": "ZCR",
47 "Mel-scale Frequency Cepstral Coefficients": "MFCC",
48 "Linear Predictive Coding": "LPC",
49 "Linear Prediction Cepstral Coefficients": "LPCC",
50 "Zero crossing peak amplitudes": "ZCPA",
51 "Line spectral frequencies": "LSF",
52 "Short-time energy": "STE",
53 "Amplitude descriptor": "AD",
54 "Adaptive time frequency transform": "ATFT",
55 "Daubechies Wavelet coefficient histogram": "DWCH",
56 "Spectral Flux": "SF",
57 "Group delay function": "GDF",
58 "Modified group delay function": "MGDF",
59 "Spectral centroid": "SC",
60 "Subband spectral flux": "SSF",
61 "Perceptual linear prediction": "PLP"
62 }
63
64
65 domain = ""
66 domainIndex = 0
67 compdict = {}
68
69 for filename in ['filters', 'trans', 'aggr']:
70 for line in [line.strip() for line in open('pdfextract/' + filename + '.txt')]:
71 compdict[line[0]] = line[2:]
72
73
74
75 for name in names:
76 id = local + (name.replace(' ','').replace('-',''))
77
78 if name == order[domainIndex]:
79 domain = domains[order[domainIndex]]
80 domainIndex += 1
81
82 afgr.add(( URIRef(id),
83 URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
84 URIRef(OWL.Class)
85 ))
86
87 afgr.add((
88 URIRef(id),
89 URIRef(local + 'feature'),
90 Literal(name.replace(' ','').replace('-',''))
91 ))
92
93 afgr.add((
94 URIRef(id),
95 URIRef(local + 'domain'),
96 Literal(domain)
97 ))
98
99 word = cat[i].split(' ')
100
101 temp = {
102 'I': 'intraframe',
103 'X': 'interframe',
104 'G': 'global'
105 }[word[0]]
106
107 afgr.add((
108 URIRef(id),
109 URIRef(local + 'temporalscale'),
110 Literal(temp)
111 ))
112
113
114 if word[1] == 'Y':
115 temp = 'perceptual'
116 else:
117 temp = 'physical'
118
119 afgr.add((
120 URIRef(id),
121 URIRef(local + 'level'),
122 Literal(temp)
123 ))
124
125 if word[2] == 'Y':
126 afgr.add((
127 URIRef(id),
128 URIRef(local + 'model'),
129 Literal('psychoacoustic')
130 ))
131
132 temp = {
133 'L': 'low',
134 'M': 'medium',
135 'H': 'high'
136 }[word[3]]
137
138 afgr.add((
139 URIRef(id),
140 URIRef(local + 'complexity'),
141 Literal(temp)
142 ))
143
144 if word[4] == 'V':
145 temp = 'parameterized'
146 else:
147 temp = word[4]
148
149 afgr.add((
150 URIRef(id),
151 URIRef(local + 'dimensions'),
152 Literal(temp)
153 ))
154
155 temp = {
156 'ASR': "speech recognition",
157 'ESR': "environmental sound recognition",
158 'MIR': "music information retrieval",
159 'AS': "audio segmentation",
160 'FP': "fingerprinting",
161 'VAR': "several",
162 'EXC': ''
163 }[word[5]]
164
165 if temp != '':
166 afgr.add((
167 URIRef(id),
168 URIRef(local + 'appdomain'),
169 Literal(temp)
170 ))
171
172 steps = sig[i].split(' ')
173
174 for key in steps:
175 afgr.add((
176 URIRef(id),
177 URIRef(local + 'computation'),
178 Literal(compdict[key])
179 ))
180
181 if name.find('MPEG-7') >= 0:
182 afgr.add((
183 URIRef(id),
184 URIRef(local + 'computedIn'),
185 Literal('MPEG-7')
186 ))
187
188 if name in abbr.keys():
189 afgr.add((
190 URIRef(id),
191 URIRef(local + 'abbreviation'),
192 Literal(abbr[name])
193 ))
194
195
196 i += 1
197
198
199 execfile('/Users/alo/Downloads/python-Levenshtein-0.10.2/StringMatcher.py')
200
201 ############# Vamp ###############
202
203
204 vamp = Graph()
205 vamp.parse('/Users/alo/Development/qm/qm-vamp-plugins/qm-vamp-plugins.n3', format='n3')
206
207 vampdict = {}
208
209 current = afgr
210
211 for s, p, o in vamp.triples((None, None, URIRef('http://purl.org/ontology/vamp/Plugin'))):
212 for vs, vp, vo in vamp.triples((s, URIRef('http://purl.org/ontology/vamp/name'), None )):
213 score = 100
214 vampdict[vo] = {'score': 0, 'name': ""}
215 for s, p, o in current.triples((None, None, RDFS.Resource)):
216 for cs, cp, name in current.triples((s, URIRef('http://sovarr.c4dm.eecs.qmul.ac.uk/features/feature'), None)):
217 m = StringMatcher()
218 m.set_seqs(vo, name)
219 sc = float(m.distance()) / ((len(vo) + len(name)) / 2.0)
220 if sc < score:
221 vampdict[vo]['score'] = 1.0 - sc
222 vampdict[vo]['name'] = name
223 score = sc
224
225 for k in vampdict.keys():
226 if vampdict[k]['score'] > 0.75:
227 name = vampdict[k]['name']
228 id = local + (name.replace(' ','').replace('-',''))
229 afgr.add((
230 URIRef(id),
231 URIRef(local + 'computedIn'),
232 Literal('Vamp Plugins')
233 ))
234 else:
235 id = local + (k.replace(' ','').replace('-',''))
236 afgr.add(( URIRef(id),
237 URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
238 URIRef(OWL.Class)
239 ))
240
241 afgr.add((
242 URIRef(id),
243 URIRef(local + 'feature'),
244 Literal(k.replace(" ", ""))
245 ))
246
247 afgr.add((
248 URIRef(id),
249 URIRef(local + 'computedIn'),
250 Literal('Vamp Plugins')
251 ))
252
253
254 ############# Marsyas ###############
255
256
257 mdir = '/Users/alo/Development/MIR/marsyas-0.4.7/src/marsyas/'
258
259 madict = {}
260
261 for name in os.listdir(mdir):
262 if fnmatch.fnmatch(name, '*.h'):
263 code = [line.strip() for line in open(mdir + name)]
264 found = False
265 for line in code:
266 if line.find('\ingroup Analysis') >= 0:
267 found = True
268 break
269
270 if found:
271 i = 0
272 cl = ''
273 for line in code:
274 if line.find('\class') >= 0:
275 cl = line.split(' ')[-1]
276 madict[cl] = {'brief': code[i+2][7:]}
277 if code[i+3] != '':
278 madict[cl]['brief'] += code[i+3]
279
280 break
281
282 i += 1
283
284
285 score = 100
286 madict[cl]['score'] = 0
287 madict[cl]['name'] = ""
288 for s, p, o in current.triples((None, None, RDFS.Resource)):
289 for cs, cp, name in current.triples((s, URIRef('http://sovarr.c4dm.eecs.qmul.ac.uk/features/feature'), None)):
290 m = StringMatcher()
291 m.set_seqs(Literal(cl), name)
292 sc = float(m.distance()) / ((len(cl) + len(name)) / 2.0)
293 if sc < score:
294 madict[cl]['score'] = 1.0 - sc
295 madict[cl]['name'] = name
296 score = sc
297
298 if madict[cl]['score'] < 0.75:
299 for k in abbr.keys():
300 if abbr[k] == cl:
301 madict[cl]['score'] = 1.0
302 madict[cl]['name'] = k
303
304
305 for k in madict.keys():
306 if madict[k]['score'] > 0.75:
307 name = madict[k]['name']
308 id = local + (name.replace(' ','').replace('-',''))
309 afgr.add((
310 URIRef(id),
311 URIRef(local + 'computedIn'),
312 Literal('Marsyas')
313 ))
314 else:
315 id = local + (k.replace(' ','').replace('-',''))
316 afgr.add(( URIRef(id),
317 URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
318 URIRef(OWL.Class)
319 ))
320
321 afgr.add((
322 URIRef(id),
323 URIRef(local + 'feature'),
324 Literal(k)
325 ))
326
327 afgr.add((
328 URIRef(id),
329 URIRef(local + 'description'),
330 Literal(madict[k]['brief'])
331 ))
332
333 afgr.add((
334 URIRef(id),
335 URIRef(local + 'computedIn'),
336 Literal('Marsyas')
337 ))
338
339
340
341 ############# jMIR ###############
342
343 jdir = '/Users/alo/Development/MIR/jAudio/jAudio/'
344
345 jdict = {}
346
347 file = urllib2.urlopen('file://' + jdir + 'features.xml')
348 data = file.read()
349 file.close()
350
351 dom = parseString(data)
352 jmir = dom.getElementsByTagName('feature')
353
354 for nodes in jmir:
355 jname = nodes.childNodes[1].firstChild.nodeValue.split('.')[-1]
356 jdict[jname] = {'score': 0, 'name': ""}
357 # if len(nodes.childNodes) == 5:
358 # print nodes.childNodes[3]
359 score = 100
360
361 for s, p, o in current.triples((None, None, RDFS.Resource)):
362 for cs, cp, name in current.triples((s, URIRef('http://sovarr.c4dm.eecs.qmul.ac.uk/features/feature'), None)):
363 m = StringMatcher()
364 m.set_seqs(Literal(jname), name)
365 sc = float(m.distance()) / ((len(jname) + len(name)) / 2.0)
366 if sc < score:
367 jdict[jname]['score'] = 1.0 - sc
368 jdict[jname]['name'] = name
369 score = sc
370
371 if jdict[jname]['score'] < 0.75:
372 for k in abbr.keys():
373 if abbr[k] == jname:
374 jdict[jname]['score'] = 1.0
375 jdict[jname]['name'] = k
376
377 for k in jdict.keys():
378 if jdict[k]['score'] > 0.75:
379 name = jdict[k]['name']
380 id = local + (name.replace(' ','').replace('-',''))
381 afgr.add((
382 URIRef(id),
383 URIRef(local + 'computedIn'),
384 Literal('jMIR')
385 ))
386 else:
387 id = local + (k.replace(' ','').replace('-',''))
388 afgr.add(( URIRef(id),
389 URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
390 URIRef(OWL.Class)
391 ))
392
393 afgr.add((
394 URIRef(id),
395 URIRef(local + 'feature'),
396 Literal(k)
397 ))
398
399 afgr.add((
400 URIRef(id),
401 URIRef(local + 'computedIn'),
402 Literal('jMIR')
403 ))
404
405
406
407 ############# libXtract ###############
408
409
410 path = '/Users/alo/Development/MIR/LibXtract/xtract/libxtract.h'
411
412 lines = [line.strip() for line in open(path)]
413
414 xtract = lines[(lines.index('enum xtract_features_ {')+1):(lines.index('XTRACT_WINDOWED')-1)]
415
416 xdict = {}
417
418 for ln in xtract:
419 xname = ln[(ln.index('_')+1):-1].replace("_", " ").lower().capitalize()
420 xdict[xname] = {'score': 0, 'name': ""}
421
422 score = 100
423
424 for s, p, o in current.triples((None, None, RDFS.Resource)):
425 for cs, cp, name in current.triples((s, URIRef('http://sovarr.c4dm.eecs.qmul.ac.uk/features/feature'), None)):
426 m = StringMatcher()
427 m.set_seqs(Literal(xname), name)
428 sc = float(m.distance()) / ((len(xname) + len(name)) / 2.0)
429 if sc < score:
430 xdict[xname]['score'] = 1.0 - sc
431 xdict[xname]['name'] = name
432 score = sc
433
434 if xdict[xname]['score'] < 0.75:
435 for k in abbr.keys():
436 if abbr[k] == xname.upper():
437 xdict[xname]['score'] = 1.0
438 xdict[xname]['name'] = k
439
440 for k in xdict.keys():
441 if xdict[k]['score'] > 0.75:
442 name = xdict[k]['name']
443 id = local + (name.replace(' ','').replace('-',''))
444 afgr.add((
445 URIRef(id),
446 URIRef(local + 'computedIn'),
447 Literal('libXtract')
448 ))
449 else:
450 id = local + (k.replace(' ','').replace('-',''))
451 afgr.add(( URIRef(id),
452 URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
453 URIRef(OWL.Class)
454 ))
455
456 afgr.add((
457 URIRef(id),
458 URIRef(local + 'feature'),
459 Literal(k.replace(" ", "").replace("-", ""))
460 ))
461
462 afgr.add((
463 URIRef(id),
464 URIRef(local + 'computedIn'),
465 Literal('libXtract')
466 ))
467
468
469
470 ############# yaafe ###############
471
472 path = "/Users/alo/Development/MIR/yaafe-v0.64/src_python/yaafefeatures.py"
473
474 lines = [line.strip() for line in open(path)]
475
476 ydict = {}
477
478 for ln in lines:
479 if ln.find('class ') >= 0:
480 yname = ln[6:ln.find('(AudioFeature)')]
481
482 ydict[yname] = {'score': 0, 'name': ""}
483
484 score = 100
485
486 for s, p, o in current.triples((None, None, RDFS.Resource)):
487 for cs, cp, name in current.triples((s, URIRef('http://sovarr.c4dm.eecs.qmul.ac.uk/features/feature'), None)):
488 m = StringMatcher()
489 m.set_seqs(Literal(yname), name)
490 sc = float(m.distance()) / ((len(yname) + len(name)) / 2.0)
491 if sc < score:
492 ydict[yname]['score'] = 1.0 - sc
493 ydict[yname]['name'] = name
494 score = sc
495
496 if ydict[yname]['score'] < 0.75:
497 for k in abbr.keys():
498 if abbr[k] == yname:
499 ydict[yname]['score'] = 1.0
500 ydict[yname]['name'] = k
501
502 for k in ydict.keys():
503 if ydict[k]['score'] > 0.75:
504 name = ydict[k]['name']
505 id = local + (name.replace(' ','').replace('-',''))
506 afgr.add((
507 URIRef(id),
508 URIRef(local + 'computedIn'),
509 Literal('yaafe')
510 ))
511 else:
512 id = local + (k.replace(' ','').replace('-',''))
513 afgr.add(( URIRef(id),
514 URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
515 URIRef(OWL.Class)
516 ))
517
518 afgr.add((
519 URIRef(id),
520 URIRef(local + 'feature'),
521 Literal(k)
522 ))
523
524 afgr.add((
525 URIRef(id),
526 URIRef(local + 'computedIn'),
527 Literal('yaafe')
528 ))
529
530 ############# MIRToolbox ###############
531
532 path = "/Users/alo/MusicOntology/features/rdf/af-MIRToolbox.rdf"
533 mirt = Graph()
534 mirt.parse(path)
535
536 mdict = {}
537
538 for s, p, o in mirt.triples((None, None, RDFS.Resource)):
539 mname = s.split('/')[-1]
540 mdict[mname] = {'score': 0, 'name':"", 'sub': s}
541
542 score = 100
543
544 for s, p, o in current.triples((None, None, RDFS.Resource)):
545 for cs, cp, name in current.triples((s, URIRef('http://sovarr.c4dm.eecs.qmul.ac.uk/features/feature'), None)):
546 m = StringMatcher()
547 m.set_seqs(Literal(mname), name)
548 sc = float(m.distance()) / ((len(mname) + len(name)) / 2.0)
549 if sc < score:
550 mdict[mname]['score'] = 1.0 - sc
551 mdict[mname]['name'] = name
552 score = sc
553
554 if mdict[mname]['score'] < 0.75:
555 for k in abbr.keys():
556 if abbr[k] == mname:
557 mdict[mname]['score'] = 1.0
558 mdict[mname]['name'] = k
559
560
561 for k in mdict.keys():
562 if mdict[k]['score'] > 0.77:
563 name = mdict[k]['name']
564 id = local + (name.replace(' ','').replace('-',''))
565 afgr.add((
566 URIRef(id),
567 URIRef(local + 'computedIn'),
568 Literal('MIRToolbox')
569 ))
570 else:
571 id = local + (k.replace(' ','').replace('-',''))
572 afgr.add(( URIRef(id),
573 URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
574 URIRef(OWL.Class)
575 ))
576
577 afgr.add((
578 URIRef(id),
579 URIRef(local + 'feature'),
580 Literal(k)
581 ))
582
583 afgr.add((
584 URIRef(id),
585 URIRef(local + 'computedIn'),
586 Literal('MIRToolbox')
587 ))
588
589 for s, p, o in mirt.triples((mdict[k]['sub'], None, None)):
590 if o != RDFS.Resource:
591 afgr.add((URIRef(id), p, o))
592
593
594 ############# CLAM ###############
595
596 path = "/Users/alo/MusicOntology/features/rdf/af-CLAM.rdf"
597 clam = Graph()
598 clam.parse(path)
599
600 cdict = {}
601
602 for s, p, o in clam.triples((None, None, RDFS.Resource)):
603 cname = s.split('/')[-1]
604 cdict[cname] = {'score': 0, 'name':"", 'sub': s}
605
606 score = 100
607
608 for s, p, o in current.triples((None, None, RDFS.Resource)):
609 for cs, cp, name in current.triples((s, URIRef('http://sovarr.c4dm.eecs.qmul.ac.uk/features/feature'), None)):
610 m = StringMatcher()
611 m.set_seqs(Literal(cname), name)
612 sc = float(m.distance()) / ((len(cname) + len(name)) / 2.0)
613 if sc < score:
614 cdict[cname]['score'] = 1.0 - sc
615 cdict[cname]['name'] = name
616 score = sc
617
618 if cdict[cname]['score'] < 0.75:
619 for k in abbr.keys():
620 if abbr[k] == cname:
621 cdict[cname]['score'] = 1.0
622 cdict[cname]['name'] = k
623
624 for k in cdict.keys():
625 if cdict[k]['score'] > 0.77:
626 name = cdict[k]['name']
627 id = local + (name.replace(' ','').replace('-',''))
628 afgr.add((
629 URIRef(id),
630 URIRef(local + 'computedIn'),
631 Literal('CLAM')
632 ))
633 else:
634 id = local + (k.replace(' ','').replace('-',''))
635 afgr.add(( URIRef(id),
636 URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
637 URIRef(OWL.Class)
638 ))
639
640 afgr.add((
641 URIRef(id),
642 URIRef(local + 'feature'),
643 Literal(k)
644 ))
645
646 afgr.add((
647 URIRef(id),
648 URIRef(local + 'computedIn'),
649 Literal('CLAM')
650 ))
651
652 for s, p, o in clam.triples((cdict[k]['sub'], None, None)):
653 if o != RDFS.Resource:
654 afgr.add((URIRef(id), p, o))
655
656
657 ############# SCMIR ###############
658
659 path = "/Users/alo/MusicOntology/features/rdf/af-SuperCollider.rdf"
660 scg = Graph()
661 scg.parse(path)
662
663 scdict = {}
664
665 for s, p, o in scg.triples((None, None, RDFS.Resource)):
666 scname = s.split('/')[-1]
667 scdict[scname] = {'score': 0, 'name':"", 'sub': s}
668
669 score = 100
670
671 for s, p, o in current.triples((None, None, RDFS.Resource)):
672 for cs, cp, name in current.triples((s, URIRef('http://sovarr.c4dm.eecs.qmul.ac.uk/features/feature'), None)):
673 m = StringMatcher()
674 m.set_seqs(Literal(scname), name)
675 sc = float(m.distance()) / ((len(scname) + len(name)) / 2.0)
676 if sc < score:
677 scdict[scname]['score'] = 1.0 - sc
678 scdict[scname]['name'] = name
679 score = sc
680
681 if scdict[scname]['score'] < 0.75:
682 for k in abbr.keys():
683 if abbr[k] == scname:
684 scdict[scname]['score'] = 1.0
685 scdict[scname]['name'] = k
686
687 for k in scdict.keys():
688 if scdict[k]['score'] > 0.77:
689 name = scdict[k]['name']
690 id = local + (name.replace(' ','').replace('-',''))
691 afgr.add((
692 URIRef(id),
693 URIRef(local + 'computedIn'),
694 Literal('SuperCollider')
695 ))
696 else:
697 id = local + (k.replace(' ','').replace('-',''))
698 afgr.add(( URIRef(id),
699 URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
700 URIRef(OWL.Class)
701 ))
702
703 afgr.add((
704 URIRef(id),
705 URIRef(local + 'feature'),
706 Literal(k)
707 ))
708
709 afgr.add((
710 URIRef(id),
711 URIRef(local + 'computedIn'),
712 Literal('SuperCollider')
713 ))
714
715 for s, p, o in scg.triples((scdict[k]['sub'], None, None)):
716 if o != RDFS.Resource:
717 afgr.add((URIRef(id), p, o))
718
719
720 ############# aubio ###############
721 path = "/Users/alo/MusicOntology/features/rdf/af-aubio.rdf"
722 aug = Graph()
723 aug.parse(path)
724
725 audict = {}
726
727 for s, p, o in aug.triples((None, None, RDFS.Resource)):
728 auname = s.split('/')[-1]
729 audict[auname] = {'score': 0, 'name':"", 'sub': s}
730
731 score = 100
732
733 for s, p, o in current.triples((None, None, RDFS.Resource)):
734 for cs, cp, name in current.triples((s, URIRef('http://sovarr.c4dm.eecs.qmul.ac.uk/features/feature'), None)):
735 m = StringMatcher()
736 m.set_seqs(Literal(auname), name)
737 au = float(m.distance()) / ((len(auname) + len(name)) / 2.0)
738 if au < score:
739 audict[auname]['score'] = 1.0 - au
740 audict[auname]['name'] = name
741 score = au
742
743 if audict[auname]['score'] < 0.75:
744 for k in abbr.keys():
745 if abbr[k] == auname:
746 audict[auname]['score'] = 1.0
747 audict[auname]['name'] = k
748
749 for k in audict.keys():
750 if audict[k]['score'] > 0.77:
751 name = audict[k]['name']
752 id = local + (name.replace(' ','').replace('-',''))
753 afgr.add((
754 URIRef(id),
755 URIRef(local + 'computedIn'),
756 Literal('Aubio')
757 ))
758 else:
759 id = local + (k.replace(' ','').replace('-',''))
760 afgr.add(( URIRef(id),
761 URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
762 URIRef(OWL.Class)
763 ))
764
765 afgr.add((
766 URIRef(id),
767 URIRef(local + 'feature'),
768 Literal(k)
769 ))
770
771 afgr.add((
772 URIRef(id),
773 URIRef(local + 'computedIn'),
774 Literal('Aubio')
775 ))
776
777 for s, p, o in aug.triples((audict[k]['sub'], None, None)):
778 if o != RDFS.Resource:
779 afgr.add((URIRef(id), p, o))
780
781
782 ############# sMIRk ###############
783 path = "/Users/alo/MusicOntology/features/rdf/af-smirk.rdf"
784 smg = Graph()
785 smg.parse(path)
786
787 smdict = {}
788
789 for s, p, o in smg.triples((None, None, RDFS.Resource)):
790 smname = s.split('/')[-1]
791 smdict[smname] = {'score': 0, 'name':"", 'sub': s}
792
793 score = 100
794
795 for s, p, o in current.triples((None, None, RDFS.Resource)):
796 for cs, cp, name in current.triples((s, URIRef('http://sovarr.c4dm.eecs.qmul.ac.uk/features/feature'), None)):
797 m = StringMatcher()
798 m.set_seqs(Literal(smname), name)
799 sm = float(m.distance()) / ((len(smname) + len(name)) / 2.0)
800 if sm < score:
801 smdict[smname]['score'] = 1.0 - sm
802 smdict[smname]['name'] = name
803 score = sm
804
805 if smdict[smname]['score'] < 0.75:
806 for k in abbr.keys():
807 if abbr[k] == smname:
808 smdict[smname]['score'] = 1.0
809 smdict[smname]['name'] = k
810
811 for k in smdict.keys():
812 if smdict[k]['score'] > 0.77:
813 name = smdict[k]['name']
814 id = local + (name.replace(' ','').replace('-',''))
815 afgr.add((
816 URIRef(id),
817 URIRef(local + 'computedIn'),
818 Literal('sMIRk')
819 ))
820 else:
821 id = local + (k.replace(' ','').replace('-',''))
822 afgr.add(( URIRef(id),
823 URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
824 URIRef(OWL.Class)
825 ))
826
827 afgr.add((
828 URIRef(id),
829 URIRef(local + 'feature'),
830 Literal(k)
831 ))
832
833 afgr.add((
834 URIRef(id),
835 URIRef(local + 'computedIn'),
836 Literal('sMIRk')
837 ))
838
839 for s, p, o in smg.triples((smdict[k]['sub'], None, None)):
840 if o != RDFS.Resource:
841 afgr.add((URIRef(id), p, o))
842
843
844 ############ check similarities ###############
845 for s, p, o in afgr.triples((None, None, OWL.Class)):
846 for ss, pp, oo in afgr.triples((None, None, OWL.Class)):
847 it = s.split('/')[-1]
848 other = ss.split('/')[-1]
849 if s != ss:
850 m = StringMatcher()
851 m.set_seqs(it, other)
852 score = float(m.distance()) / ((len(it) + len(other)) / 2.0)
853 if score < 0.25:
854 print score
855 afgr.add((s, URIRef(local + 'similarTo'), ss))
856
857
858 afgr.serialize('/Users/alo/MusicOntology/features/featuresCatalogue.n3', format='n3')
859 afgr.serialize('/Users/alo/MusicOntology/features/featuresCatalogue.rdf')