nothing@18: import rdflib nothing@18: from rdflib import Graph, RDF, RDFS, plugin, URIRef, Literal, OWL, Namespace nothing@18: nothing@18: basedir = '/Users/alo/MusicOntology/features/' nothing@18: nothing@18: local = 'http://sovarr.c4dm.eecs.qmul.ac.uk/features/' nothing@18: nothing@18: DC = Namespace(u"http://purl.org/dc/elements/1.1/") nothing@18: nothing@18: graph = Graph() nothing@18: graph.bind('af', URIRef(local)) nothing@18: graph.bind('dc', URIRef('http://purl.org/dc/elements/1.1/')) nothing@18: graph.bind('owl', URIRef('http://www.w3.org/2002/07/owl#')) nothing@18: nothing@18: graph.add(( nothing@18: URIRef(''), nothing@18: RDF.type, nothing@18: OWL.Ontology nothing@18: )) nothing@18: nothing@18: graph.add(( nothing@18: URIRef(''), nothing@18: DC['title'], nothing@18: Literal("Marsyas Ontology") nothing@18: )) nothing@18: nothing@18: graph.add(( nothing@18: URIRef(''), nothing@18: OWL.versionInfo, nothing@18: Literal("Version 0.1") nothing@18: )) nothing@18: nothing@18: graph.add(( nothing@18: URIRef(''), nothing@18: DC['description'], nothing@18: Literal("This is an ontology derived from Marsyas feature extraction tools for the Audio Features engineering process") nothing@18: )) nothing@18: nothing@18: nothing@18: source = Graph() nothing@18: source.parse(basedir+'rdfonto/af-Marsyas.n3', format='n3') nothing@18: nothing@18: categories = [] nothing@18: nothing@18: for su, ob in source.subject_objects(URIRef('file:///Users/alo/MusicOntology/features/rdf/type')): nothing@18: if not ob in categories: nothing@18: categories.append(ob) nothing@18: nothing@18: graph.add(( nothing@18: URIRef(local+'Analysis'), nothing@18: RDF.type, nothing@18: OWL.Class nothing@18: )) nothing@18: nothing@18: for category in categories: nothing@18: graph.add(( nothing@18: URIRef(local+category), nothing@18: RDF.type, nothing@18: OWL.Class nothing@18: )) nothing@18: graph.add(( nothing@18: URIRef(local+category), nothing@18: RDFS.subClassOf, nothing@18: URIRef(local+'Analysis'), nothing@18: )) nothing@18: nothing@18: for su in source.subjects(RDF.type, RDFS.Resource): nothing@18: idref = URIRef(local+su.split('/')[-1]) nothing@18: # graph.add(( nothing@18: # idref, nothing@18: # RDF.type, nothing@18: # OWL.Class nothing@18: # )) nothing@18: nothing@18: graph.add(( nothing@18: idref, nothing@18: RDF.type, nothing@18: URIRef(local+"AudioFeature") nothing@18: )) nothing@18: nothing@18: count = sum(1 for _ in source.objects(su,URIRef('file:///Users/alo/MusicOntology/features/rdf/type'))) nothing@18: nothing@18: if count > 0: nothing@18: for ob in source.objects(su,URIRef('file:///Users/alo/MusicOntology/features/rdf/type')): nothing@18: graph.add(( nothing@18: idref, nothing@18: RDFS.subClassOf, nothing@18: URIRef(local+ob) nothing@18: )) nothing@18: else: nothing@18: graph.add(( nothing@18: idref, nothing@18: RDFS.subClassOf, nothing@18: URIRef(local+'Analysis'), nothing@18: )) nothing@18: nothing@18: for ob in source.objects(su,URIRef('http://purl.org/dc/elements/1.1/description')): nothing@18: graph.add(( nothing@18: idref, nothing@18: URIRef('http://purl.org/dc/elements/1.1/description'), nothing@18: ob nothing@18: )) nothing@18: nothing@18: graph.serialize(basedir + 'rdfonto/Marsyas-onto.rdf') nothing@18: graph.serialize(basedir + 'rdfonto/Marsyas-onto.n3', format='n3')