annotate rdfpy/af-vocabulary.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
nothing@18 2 from rdflib import Graph, RDF, RDFS, plugin, URIRef, Literal, OWL, Namespace, XSD
nothing@18 3
nothing@18 4 plugin.register(
nothing@18 5 'sparql', rdflib.query.Processor,
nothing@18 6 'rdfextras.sparql.processor', 'Processor')
nothing@18 7 plugin.register(
nothing@18 8 'sparql', rdflib.query.Result,
nothing@18 9 'rdfextras.sparql.query', 'SPARQLQueryResult')
nothing@18 10
nothing@18 11 def getTools(graph, nsuri):
nothing@18 12 return graph.query(
nothing@18 13 """SELECT DISTINCT ?tool
nothing@18 14 WHERE {
nothing@18 15 ?x af:computedIn ?tool .
nothing@18 16 ?x af:feature ?feature
nothing@18 17 }
nothing@18 18 ORDER BY ?tool""",
nothing@18 19 initNs=dict(
nothing@18 20 af=Namespace(nsuri))
nothing@18 21 )
nothing@18 22
nothing@18 23
nothing@18 24 url = 'http://sovarr.c4dm.eecs.qmul.ac.uk/af/'
nothing@18 25 afv = Namespace(url+'vocabulary/1.0#')
nothing@18 26 basedir = '/Users/alo/MusicOntology/features/'
nothing@18 27
nothing@18 28 afc = Namespace(url + 'features/')
nothing@18 29
nothing@18 30 af = Namespace(url+'ontology/1.0#')
nothing@18 31
nothing@18 32 cat = Namespace('http://sovarr.c4dm.eecs.qmul.ac.uk/features/')
nothing@18 33
nothing@18 34 vs = Namespace('http://www.w3.org/2003/06/sw-vocab-status/ns#')
nothing@18 35
nothing@18 36 graph = Graph()
nothing@18 37 graph.bind('afv', afv)
nothing@18 38 graph.bind('owl', OWL)
nothing@18 39 graph.bind('af', af)
nothing@18 40 graph.bind('vs', vs)
nothing@18 41 graph.bind('xsd', XSD)
nothing@18 42
nothing@18 43 graph.add(( vs['term_status'], RDF.type, OWL.AnnotationProperty ))
nothing@18 44 graph.add(( afv['computedIn'], RDF.type, OWL.Property ))
nothing@18 45 graph.add(( afv['computedIn'], RDFS.range, XSD.String ))
nothing@18 46 graph.add(( afv['computedIn'], RDFS.domain, af['AudioFeature'] ))
nothing@18 47
nothing@18 48 source = Graph()
nothing@18 49 source.parse(basedir + 'af-catalogue-01.rdf')
nothing@18 50
nothing@18 51 tooldict = {}
nothing@18 52
nothing@18 53 for tool in getTools(source, cat):
nothing@18 54 idt = afv[tool[0]]
nothing@18 55 tooldict[tool[0]] = idt
nothing@18 56 graph.add((idt, RDF.type, OWL.Class))
nothing@18 57 graph.add((idt, RDFS.label, tool[0]))
nothing@18 58 graph.add((idt, RDFS.comment, tool[0] ))
nothing@18 59 graph.add((idt, vs['term_status'], Literal("testing", lang="en") ))
nothing@18 60
nothing@18 61 for sub in source.subjects(RDF.type, OWL.Class):
nothing@18 62 name = sub.split('/')[-1]
nothing@18 63 id = name.replace("(", "").replace(")", "").replace("4Hz", "")
nothing@18 64 #id = ''.join(i for i in id if not i.isdigit())
nothing@18 65 id = URIRef(afv+id)
nothing@18 66 graph.add((id, RDF.type, OWL.Class))
nothing@18 67 graph.add((id, RDFS.subClassOf, af['AudioFeature']))
nothing@18 68 graph.add((id, vs['term_status'], Literal("testing", lang="en") ))
nothing@18 69 count = sum(1 for _ in source.objects(sub, afc['feature']))
nothing@18 70 if count > 0:
nothing@18 71 labelAdded = False
nothing@18 72 for label in source.objects(sub, afc['feature']):
nothing@18 73 if not labelAdded:
nothing@18 74 graph.add(( id, RDFS.label, Literal(label, lang="en") ))
nothing@18 75 graph.add(( id, RDFS.comment, Literal(label) ))
nothing@18 76 labelAdded = True
nothing@18 77 else:
nothing@18 78 if name.find("ADRess") > -1:
nothing@18 79 graph.add(( id, RDFS.label, Literal(name + " (Azimuth Discrimination and Resynthesis)", lang="en") ))
nothing@18 80 graph.add(( id, RDFS.comment, Literal(name + " (Azimuth Discrimination and Resynthesis)") ))
nothing@18 81 else:
nothing@18 82 if name.find("Aim") == 0:
nothing@18 83 graph.add(( id, RDFS.label, Literal(name + " (Auditory Image Model) ", lang="en") ))
nothing@18 84 graph.add(( id, RDFS.comment, Literal(name + " (Auditory Image Model) ") ))
nothing@18 85 else:
nothing@18 86 graph.add(( id, RDFS.label, Literal(name, lang="en")))
nothing@18 87 graph.add(( id, RDFS.comment, Literal(name) ))
nothing@18 88
nothing@18 89 for tool in source.objects(sub, cat['computedIn']):
nothing@18 90 graph.add((id, afv['computedIn'], tooldict[tool]))
nothing@18 91
nothing@18 92 graph.serialize(basedir+'rdfonto/af-vocabulary.rdf')
nothing@18 93 graph.serialize(basedir+'rdfonto/af-vocabulary.n3', format="n3")
nothing@18 94