annotate rdfpy/af-ontology-defs.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
nothing@18 3 from InfixOwl.InfixOwl import *
nothing@18 4 from datetime import datetime
nothing@18 5
nothing@18 6 def writeHeader(graph, title, version, description):
nothing@18 7 ontoid = URIRef('')
nothing@18 8 graph.add((
nothing@18 9 ontoid,
nothing@18 10 RDF.type,
nothing@18 11 OWL.Ontology
nothing@18 12 ))
nothing@18 13 graph.add((
nothing@18 14 ontoid,
nothing@18 15 dc['title'],
nothing@18 16 Literal(title, lang="en")
nothing@18 17 ))
nothing@18 18 graph.add((
nothing@18 19 URIRef(''),
nothing@18 20 OWL.versionInfo,
nothing@18 21 Literal("Version "+version)
nothing@18 22 ))
nothing@18 23 graph.add((
nothing@18 24 ontoid,
nothing@18 25 RDFS.label,
nothing@18 26 Literal(title, lang="en")
nothing@18 27 ))
nothing@18 28 graph.add((
nothing@18 29 URIRef(''),
nothing@18 30 dc['description'],
nothing@18 31 Literal(description, lang="en")
nothing@18 32 ))
nothing@18 33 graph.add((
nothing@18 34 URIRef(''),
nothing@18 35 dc['date'],
nothing@18 36 Literal(datetime.isoformat(datetime.now()))
nothing@18 37 ))
nothing@18 38
nothing@18 39 def addTerm(graph, label):
nothing@18 40 termid = URIRef(af+label.replace(" ", ""))
nothing@18 41 graph.add(( termid, RDF.type, OWL.Class ))
nothing@18 42 graph.add(( termid, RDFS.label, Literal(label, lang="en") ))
nothing@18 43 return termid
nothing@18 44
nothing@18 45 def addOriginalOntology(graph):
nothing@18 46 orig = Namespace('http://purl.org/ontology/af/')
nothing@18 47 origOnto = Graph()
nothing@18 48 #origOnto.parse(orig)
nothing@18 49 origOnto.parse('/Users/alo/MusicOntology/audiofeaturesonto/rdf/audio_features.rdf')
nothing@18 50
nothing@18 51 importTerms = ["Signal", "signal_feature", "value", "dimensions", "Segment", "Point", "feature"]
nothing@18 52
nothing@18 53 for item in importTerms:
nothing@18 54 sub = URIRef(orig+item)
nothing@18 55 oid = URIRef(af+item)
nothing@18 56 for pre, obj in origOnto.predicate_objects(sub):
nothing@18 57 if pre == RDFS.subClassOf and type(obj) == BNode:
nothing@18 58 onProp = origOnto.value(obj,OWL.onProperty,None)
nothing@18 59 values = origOnto.value(obj,OWL.someValuesFrom,None)
nothing@18 60 Restriction(onProp,ontograph,someValuesFrom=values,identifier=obj)
nothing@18 61 ontograph.add(( oid, pre, obj ))
nothing@18 62 else:
nothing@18 63 if obj.find(orig) > -1:
nothing@18 64 val = URIRef(obj.replace(orig,af))
nothing@18 65 else:
nothing@18 66 val = obj
nothing@18 67 graph.add(( oid, pre, val ))
nothing@18 68
nothing@18 69 def addBaseTriples(graph, pypath, rdfpath):
nothing@18 70 execfile(pypath)
nothing@18 71
nothing@18 72 base = Graph()
nothing@18 73 base.parse(rdfpath)
nothing@18 74
nothing@18 75 for sub, obj in base.subject_objects(RDF.type):
nothing@18 76 if obj == OWL.Class or obj == RDF.Property:
nothing@18 77 bid = URIRef(af+sub.split("/")[-1])
nothing@18 78 for pre, item in base.predicate_objects(sub):
nothing@18 79 if item.find(afcns) > -1:
nothing@18 80 addobj = URIRef(item.replace(afcns,af))
nothing@18 81 else:
nothing@18 82 addobj = item
nothing@18 83
nothing@18 84 ontograph.add((bid, pre, addobj))
nothing@18 85
nothing@18 86 graph.add((bid, vs['term_status'], Literal("testing", lang="en")))
nothing@18 87 graph.add((bid, RDFS.comment, Literal("Audio feature taxonomy term (Mitrovic et al)") ))
nothing@18 88
nothing@18 89
nothing@18 90 def addTriples(graph, pypath, rdfpath, tool):
nothing@18 91 execfile(pypath)
nothing@18 92
nothing@18 93 source = Graph()
nothing@18 94 source.parse(rdfpath)
nothing@18 95
nothing@18 96 for sub, obj in source.subject_objects(RDF.type):
nothing@18 97 if obj != URIRef(afcns+"AudioFeature") and obj != OWL.Ontology:
nothing@18 98 bid = URIRef(af+sub.split("/")[-1])
nothing@18 99 for pre, item in source.predicate_objects(sub):
nothing@18 100 if item.find(afcns) > -1:
nothing@18 101 addobj = URIRef(item.replace(afcns,af))
nothing@18 102 else:
nothing@18 103 addobj = item
nothing@18 104
nothing@18 105 graph.add((bid, pre, addobj))
nothing@18 106
nothing@18 107 graph.add((bid, vs['term_status'], Literal("testing", lang="en")))
nothing@18 108 graph.add((bid, RDFS.comment, Literal(tool + " ontology term") ))
nothing@18 109
nothing@18 110 def serialize(graph, path, name):
nothing@18 111 graph.serialize(path + name + '.rdf')
nothing@18 112 graph.serialize(path + name + '.n3', format="n3")
nothing@18 113
nothing@18 114