nothing@18: import rdflib nothing@18: from rdflib import Graph, RDF, RDFS, plugin, URIRef, Literal, OWL, Namespace, XSD nothing@18: nothing@18: plugin.register( nothing@18: 'sparql', rdflib.query.Processor, nothing@18: 'rdfextras.sparql.processor', 'Processor') nothing@18: plugin.register( nothing@18: 'sparql', rdflib.query.Result, nothing@18: 'rdfextras.sparql.query', 'SPARQLQueryResult') nothing@18: nothing@18: def getTools(graph, nsuri): nothing@18: return graph.query( nothing@18: """SELECT DISTINCT ?tool nothing@18: WHERE { nothing@18: ?x af:computedIn ?tool . nothing@18: ?x af:feature ?feature nothing@18: } nothing@18: ORDER BY ?tool""", nothing@18: initNs=dict( nothing@18: af=Namespace(nsuri)) nothing@18: ) nothing@18: nothing@18: nothing@18: url = 'http://sovarr.c4dm.eecs.qmul.ac.uk/af/' nothing@18: afv = Namespace(url+'vocabulary/1.0#') nothing@18: basedir = '/Users/alo/MusicOntology/features/' nothing@18: nothing@18: afc = Namespace(url + 'features/') nothing@18: nothing@18: af = Namespace(url+'ontology/1.0#') nothing@18: nothing@18: cat = Namespace('http://sovarr.c4dm.eecs.qmul.ac.uk/features/') nothing@18: nothing@18: vs = Namespace('http://www.w3.org/2003/06/sw-vocab-status/ns#') nothing@18: nothing@18: graph = Graph() nothing@18: graph.bind('afv', afv) nothing@18: graph.bind('owl', OWL) nothing@18: graph.bind('af', af) nothing@18: graph.bind('vs', vs) nothing@18: graph.bind('xsd', XSD) nothing@18: nothing@18: graph.add(( vs['term_status'], RDF.type, OWL.AnnotationProperty )) nothing@18: graph.add(( afv['computedIn'], RDF.type, OWL.Property )) nothing@18: graph.add(( afv['computedIn'], RDFS.range, XSD.String )) nothing@18: graph.add(( afv['computedIn'], RDFS.domain, af['AudioFeature'] )) nothing@18: nothing@18: source = Graph() nothing@18: source.parse(basedir + 'af-catalogue-01.rdf') nothing@18: nothing@18: tooldict = {} nothing@18: nothing@18: for tool in getTools(source, cat): nothing@18: idt = afv[tool[0]] nothing@18: tooldict[tool[0]] = idt nothing@18: graph.add((idt, RDF.type, OWL.Class)) nothing@18: graph.add((idt, RDFS.label, tool[0])) nothing@18: graph.add((idt, RDFS.comment, tool[0] )) nothing@18: graph.add((idt, vs['term_status'], Literal("testing", lang="en") )) nothing@18: nothing@18: for sub in source.subjects(RDF.type, OWL.Class): nothing@18: name = sub.split('/')[-1] nothing@18: id = name.replace("(", "").replace(")", "").replace("4Hz", "") nothing@18: #id = ''.join(i for i in id if not i.isdigit()) nothing@18: id = URIRef(afv+id) nothing@18: graph.add((id, RDF.type, OWL.Class)) nothing@18: graph.add((id, RDFS.subClassOf, af['AudioFeature'])) nothing@18: graph.add((id, vs['term_status'], Literal("testing", lang="en") )) nothing@18: count = sum(1 for _ in source.objects(sub, afc['feature'])) nothing@18: if count > 0: nothing@18: labelAdded = False nothing@18: for label in source.objects(sub, afc['feature']): nothing@18: if not labelAdded: nothing@18: graph.add(( id, RDFS.label, Literal(label, lang="en") )) nothing@18: graph.add(( id, RDFS.comment, Literal(label) )) nothing@18: labelAdded = True nothing@18: else: nothing@18: if name.find("ADRess") > -1: nothing@18: graph.add(( id, RDFS.label, Literal(name + " (Azimuth Discrimination and Resynthesis)", lang="en") )) nothing@18: graph.add(( id, RDFS.comment, Literal(name + " (Azimuth Discrimination and Resynthesis)") )) nothing@18: else: nothing@18: if name.find("Aim") == 0: nothing@18: graph.add(( id, RDFS.label, Literal(name + " (Auditory Image Model) ", lang="en") )) nothing@18: graph.add(( id, RDFS.comment, Literal(name + " (Auditory Image Model) ") )) nothing@18: else: nothing@18: graph.add(( id, RDFS.label, Literal(name, lang="en"))) nothing@18: graph.add(( id, RDFS.comment, Literal(name) )) nothing@18: nothing@18: for tool in source.objects(sub, cat['computedIn']): nothing@18: graph.add((id, afv['computedIn'], tooldict[tool])) nothing@18: nothing@18: graph.serialize(basedir+'rdfonto/af-vocabulary.rdf') nothing@18: graph.serialize(basedir+'rdfonto/af-vocabulary.n3', format="n3") nothing@18: