view rdfpy/af-vocabulary.py @ 18:d5012016bf64 tip

added rdfpy and rdfonto directories
author nothing@tehis.net
date Tue, 23 Apr 2013 11:49:20 +0100
parents
children
line wrap: on
line source
import rdflib
from rdflib import Graph, RDF, RDFS, plugin, URIRef, Literal, OWL, Namespace, XSD

plugin.register(
    'sparql', rdflib.query.Processor,
    'rdfextras.sparql.processor', 'Processor')
plugin.register(
    'sparql', rdflib.query.Result,
    'rdfextras.sparql.query', 'SPARQLQueryResult')

def getTools(graph, nsuri):
    return graph.query(
        """SELECT DISTINCT ?tool
           WHERE {
    	   	?x af:computedIn ?tool .
    		?x af:feature ?feature 
           }
    	   ORDER BY ?tool""",
        initNs=dict(
            af=Namespace(nsuri))
    )


url = 'http://sovarr.c4dm.eecs.qmul.ac.uk/af/'
afv = Namespace(url+'vocabulary/1.0#')
basedir = '/Users/alo/MusicOntology/features/'

afc = Namespace(url + 'features/')

af = Namespace(url+'ontology/1.0#')

cat = Namespace('http://sovarr.c4dm.eecs.qmul.ac.uk/features/')

vs = Namespace('http://www.w3.org/2003/06/sw-vocab-status/ns#')

graph = Graph()
graph.bind('afv', afv)
graph.bind('owl', OWL)
graph.bind('af', af)
graph.bind('vs', vs)
graph.bind('xsd', XSD)

graph.add(( vs['term_status'], RDF.type, OWL.AnnotationProperty ))
graph.add(( afv['computedIn'], RDF.type, OWL.Property ))
graph.add(( afv['computedIn'], RDFS.range, XSD.String ))
graph.add(( afv['computedIn'], RDFS.domain, af['AudioFeature'] ))

source = Graph()
source.parse(basedir + 'af-catalogue-01.rdf')

tooldict = {}

for tool in getTools(source, cat):
    idt = afv[tool[0]]
    tooldict[tool[0]] = idt
    graph.add((idt, RDF.type, OWL.Class))
    graph.add((idt, RDFS.label, tool[0]))
    graph.add((idt, RDFS.comment, tool[0] ))
    graph.add((idt, vs['term_status'], Literal("testing", lang="en") ))

for sub in source.subjects(RDF.type, OWL.Class):
    name = sub.split('/')[-1]
    id = name.replace("(", "").replace(")", "").replace("4Hz", "")
    #id = ''.join(i for i in id if not i.isdigit())
    id = URIRef(afv+id)
    graph.add((id, RDF.type, OWL.Class))
    graph.add((id, RDFS.subClassOf, af['AudioFeature']))
    graph.add((id, vs['term_status'], Literal("testing", lang="en") ))
    count = sum(1 for _ in source.objects(sub, afc['feature']))
    if count > 0:
        labelAdded = False
        for label in source.objects(sub, afc['feature']):
            if not labelAdded:
                graph.add(( id, RDFS.label, Literal(label, lang="en") ))
                graph.add(( id, RDFS.comment, Literal(label) ))
                labelAdded = True
    else:        
        if name.find("ADRess") > -1:
            graph.add(( id, RDFS.label, Literal(name + " (Azimuth Discrimination and Resynthesis)", lang="en") ))
            graph.add(( id, RDFS.comment, Literal(name + " (Azimuth Discrimination and Resynthesis)") ))
        else:
            if name.find("Aim") == 0:
                graph.add(( id, RDFS.label, Literal(name + " (Auditory Image Model) ", lang="en") ))
                graph.add(( id, RDFS.comment, Literal(name + " (Auditory Image Model) ") ))
            else:
                graph.add(( id, RDFS.label, Literal(name, lang="en")))
                graph.add(( id, RDFS.comment, Literal(name) ))
    
    for tool in source.objects(sub, cat['computedIn']):
        graph.add((id, afv['computedIn'], tooldict[tool]))
    
graph.serialize(basedir+'rdfonto/af-vocabulary.rdf')
graph.serialize(basedir+'rdfonto/af-vocabulary.n3', format="n3")