nothing@0: import rdflib, os nothing@0: from rdflib import Graph, RDF, RDFS, plugin, URIRef, Literal, OWL, Namespace nothing@0: nothing@0: basedir = '/Users/alo/MusicOntology/features/' nothing@0: nothing@0: local = 'http://sovarr.c4dm.eecs.qmul.ac.uk/features/' nothing@0: nothing@0: DC = Namespace(u"http://purl.org/dc/elements/1.1/") nothing@0: nothing@0: graph = Graph() nothing@0: graph.bind('af', URIRef(local)) nothing@0: graph.bind('dc', URIRef('http://purl.org/dc/elements/1.1/')) nothing@0: graph.bind('owl', URIRef('http://www.w3.org/2002/07/owl#')) nothing@0: nothing@0: graph.add(( nothing@0: URIRef(''), nothing@0: RDF.type, nothing@0: OWL.Ontology nothing@0: )) nothing@0: nothing@0: graph.add(( nothing@0: URIRef(''), nothing@0: DC['title'], nothing@0: Literal("MIR Toolbox Ontology") nothing@0: )) nothing@0: nothing@0: graph.add(( nothing@0: URIRef(''), nothing@0: OWL.versionInfo, nothing@0: Literal("Version 0.1") nothing@0: )) nothing@0: nothing@0: graph.add(( nothing@0: URIRef(''), nothing@0: DC['description'], nothing@0: Literal("This is an ontology derived from MIR Toolbox for the Audio Features engineering process") nothing@0: )) nothing@0: nothing@0: source = Graph() nothing@0: source.parse(basedir+'rdfonto/af-MIRToolbox.rdf') nothing@0: nothing@0: graph.add(( nothing@0: URIRef(local+'Operator'), nothing@0: RDF.type, nothing@0: OWL.Class nothing@0: )) nothing@0: graph.add(( nothing@0: URIRef(local+'FeatureExtractor'), nothing@0: RDF.type, nothing@0: OWL.Class nothing@0: )) nothing@0: graph.add(( nothing@0: URIRef(local+'HighLevelFeature'), nothing@0: RDF.type, nothing@0: OWL.Class nothing@0: )) nothing@0: nothing@0: for name in ['Structure', 'Statistics', 'Predictions', 'Similarity']: nothing@0: idref = URIRef(local+name) nothing@0: graph.add(( nothing@0: idref, nothing@0: RDF.type, nothing@0: OWL.Class nothing@0: )) nothing@0: graph.add(( nothing@0: idref, nothing@0: RDFS.subClassOf, nothing@0: URIRef(local+'HighLevelFeature') nothing@0: )) nothing@0: nothing@0: nothing@0: for name in ['Dynamics', 'Rhythm', 'Timbre', 'Pitch', 'Tonality']: nothing@0: idref = URIRef(local+name+'FeatureExtractor') nothing@0: graph.add(( nothing@0: idref, nothing@0: RDF.type, nothing@0: OWL.Class nothing@0: )) nothing@0: graph.add(( nothing@0: idref, nothing@0: RDFS.subClassOf, nothing@0: URIRef(local+'FeatureExtractor') nothing@0: )) nothing@0: nothing@0: for su in source.subjects(RDF.type, RDFS.Resource): nothing@0: idref = URIRef(local + su.split('/')[-1]) nothing@15: # graph.add(( nothing@15: # idref, nothing@15: # RDF.type, nothing@15: # OWL.Class nothing@15: # )) nothing@15: nothing@0: graph.add(( nothing@0: idref, nothing@0: RDF.type, nothing@15: URIRef(local+"AudioFeature") nothing@0: )) nothing@0: nothing@0: count=sum(1 for _ in source.objects(su, URIRef(local+'tag'))) nothing@0: nothing@0: if count == 1: nothing@0: for it in source.objects(su, URIRef(local+'tag')): nothing@0: graph.add(( nothing@0: idref, nothing@0: RDFS.subClassOf, nothing@0: URIRef(local+it+'FeatureExtractor') nothing@0: )) nothing@0: nothing@0: count = sum(1 for _ in source.objects(su, URIRef(local+'group'))) nothing@0: nothing@0: if count == 1: nothing@0: for it in source.objects(su, URIRef(local+'group')): nothing@0: graph.add(( nothing@0: idref, nothing@0: RDFS.subClassOf, nothing@0: URIRef(local+it) nothing@0: )) nothing@0: nothing@0: count=sum(1 for _ in source.objects(su, URIRef(local+'type'))) nothing@0: nothing@0: if count == 1: nothing@0: for it in source.objects(su, URIRef(local+'type')): nothing@0: if it == "Operator": nothing@0: graph.add(( nothing@0: idref, nothing@0: RDFS.subClassOf, nothing@0: URIRef(local+it) nothing@0: )) nothing@0: nothing@0: graph.serialize(basedir + 'rdfonto/MIR-onto.rdf') nothing@0: graph.serialize(basedir + 'rdfonto/MIR-onto.n3', format='n3')