nothing@18: import rdflib nothing@18: from rdflib import Graph, RDF, RDFS, plugin, URIRef, Literal, OWL, Namespace nothing@18: from InfixOwl.InfixOwl import * nothing@18: from datetime import datetime nothing@18: nothing@18: def writeHeader(graph, title, version, description): nothing@18: ontoid = URIRef('') nothing@18: graph.add(( nothing@18: ontoid, nothing@18: RDF.type, nothing@18: OWL.Ontology nothing@18: )) nothing@18: graph.add(( nothing@18: ontoid, nothing@18: dc['title'], nothing@18: Literal(title, lang="en") nothing@18: )) nothing@18: graph.add(( nothing@18: URIRef(''), nothing@18: OWL.versionInfo, nothing@18: Literal("Version "+version) nothing@18: )) nothing@18: graph.add(( nothing@18: ontoid, nothing@18: RDFS.label, nothing@18: Literal(title, lang="en") nothing@18: )) nothing@18: graph.add(( nothing@18: URIRef(''), nothing@18: dc['description'], nothing@18: Literal(description, lang="en") nothing@18: )) nothing@18: graph.add(( nothing@18: URIRef(''), nothing@18: dc['date'], nothing@18: Literal(datetime.isoformat(datetime.now())) nothing@18: )) nothing@18: nothing@18: def addTerm(graph, label): nothing@18: termid = URIRef(af+label.replace(" ", "")) nothing@18: graph.add(( termid, RDF.type, OWL.Class )) nothing@18: graph.add(( termid, RDFS.label, Literal(label, lang="en") )) nothing@18: return termid nothing@18: nothing@18: def addOriginalOntology(graph): nothing@18: orig = Namespace('http://purl.org/ontology/af/') nothing@18: origOnto = Graph() nothing@18: #origOnto.parse(orig) nothing@18: origOnto.parse('/Users/alo/MusicOntology/audiofeaturesonto/rdf/audio_features.rdf') nothing@18: nothing@18: importTerms = ["Signal", "signal_feature", "value", "dimensions", "Segment", "Point", "feature"] nothing@18: nothing@18: for item in importTerms: nothing@18: sub = URIRef(orig+item) nothing@18: oid = URIRef(af+item) nothing@18: for pre, obj in origOnto.predicate_objects(sub): nothing@18: if pre == RDFS.subClassOf and type(obj) == BNode: nothing@18: onProp = origOnto.value(obj,OWL.onProperty,None) nothing@18: values = origOnto.value(obj,OWL.someValuesFrom,None) nothing@18: Restriction(onProp,ontograph,someValuesFrom=values,identifier=obj) nothing@18: ontograph.add(( oid, pre, obj )) nothing@18: else: nothing@18: if obj.find(orig) > -1: nothing@18: val = URIRef(obj.replace(orig,af)) nothing@18: else: nothing@18: val = obj nothing@18: graph.add(( oid, pre, val )) nothing@18: nothing@18: def addBaseTriples(graph, pypath, rdfpath): nothing@18: execfile(pypath) nothing@18: nothing@18: base = Graph() nothing@18: base.parse(rdfpath) nothing@18: nothing@18: for sub, obj in base.subject_objects(RDF.type): nothing@18: if obj == OWL.Class or obj == RDF.Property: nothing@18: bid = URIRef(af+sub.split("/")[-1]) nothing@18: for pre, item in base.predicate_objects(sub): nothing@18: if item.find(afcns) > -1: nothing@18: addobj = URIRef(item.replace(afcns,af)) nothing@18: else: nothing@18: addobj = item nothing@18: nothing@18: ontograph.add((bid, pre, addobj)) nothing@18: nothing@18: graph.add((bid, vs['term_status'], Literal("testing", lang="en"))) nothing@18: graph.add((bid, RDFS.comment, Literal("Audio feature taxonomy term (Mitrovic et al)") )) nothing@18: nothing@18: nothing@18: def addTriples(graph, pypath, rdfpath, tool): nothing@18: execfile(pypath) nothing@18: nothing@18: source = Graph() nothing@18: source.parse(rdfpath) nothing@18: nothing@18: for sub, obj in source.subject_objects(RDF.type): nothing@18: if obj != URIRef(afcns+"AudioFeature") and obj != OWL.Ontology: nothing@18: bid = URIRef(af+sub.split("/")[-1]) nothing@18: for pre, item in source.predicate_objects(sub): nothing@18: if item.find(afcns) > -1: nothing@18: addobj = URIRef(item.replace(afcns,af)) nothing@18: else: nothing@18: addobj = item nothing@18: nothing@18: graph.add((bid, pre, addobj)) nothing@18: nothing@18: graph.add((bid, vs['term_status'], Literal("testing", lang="en"))) nothing@18: graph.add((bid, RDFS.comment, Literal(tool + " ontology term") )) nothing@18: nothing@18: def serialize(graph, path, name): nothing@18: graph.serialize(path + name + '.rdf') nothing@18: graph.serialize(path + name + '.n3', format="n3") nothing@18: nothing@18: