view fuxi/convertImplicationstoN3.py @ 18:d5012016bf64 tip

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

cat = ["complexity", "domain", "level", "temporalscale", "dimensionality", "model"]
val = {
    "complexity": ["Low", "Medium", "High"],
    "temporalscale": ["Interframe", "Intraframe", "Global"],
    "domain": ["Modulation Frequency", "Frequency", "Cepstral", "Eigendomain", "Phase Space", "Temporal", "Correlation"],
    "level": ["Perceptual", "Physical"],
    "dimensionality": ["One-dimensional", "Multi-dimensional"],
    "model": ["Psychoacoustic"]
}

basedir = '/Users/alo/MusicOntology/features/'
readpath = basedir + 'fcadata/baseImplicationsv2.txt'
writepath = basedir + 'fuxi/af-base-rules.n3'
factpath = basedir + 'fuxi/af-base-facts.n3'
ontopath = basedir + 'fuxi/af-base-ontology.rdf'
#ontopath = basedir + 'fuxi/af-base-properties.rdf'
cataloguePath = '/Users/alo/MusicOntology/features/af-catalogue.rdf'
baseuri = 'http://sovarr.c4dm.eecs.qmul.ac.uk/features/af-ontology#'

def readFile(path):
    file = open(path)
    text = file.read()
    file.close()
    return text.split("\n")

def writeN3(string, path):
    file = open(path, 'w')
    file.write(string)
    file.close()
    
def addHeader():
    text = "@prefix af: <" + baseuri + "> .\n"
    text += "@prefix owl: <http://www.w3.org/2002/07/owl#> .\n\n"
    return text
    
def convertRules(rows):
    n3 = addHeader()
    #n3 += "?x a af:AudioFeature . \n"

    for i in range(len(rows)-1):
        
        row = rows[i]
        if row != '':
            count = int(row[row.index("<")+1:row.index(">")-1])
            premise = row[row.index(">")+2:row.index("==>")-1].split()
            conclusion = row[row.index("==>")+4:-1].split()
            if count > 0:
                var = str(i+1).zfill(2)
                n3 += "{ ?x" + var + " a af:AudioFeature . "

                for pre in premise:
                    prop = "af:"+pre.split("-")[-1]
                    value = "af:"+(" ".join(pre.split("-")[0:-1]).title().replace(" ", "").replace("-", ""))
                    n3 += ' ?x' + var + ' ' + prop + ' ' + value
                    n3 += " . "
                
                n3 = n3[:-2] + " } => { "

                for con in conclusion:
                    prop = "af:"+con.split("-")[-1]
                    value = "af:"+(" ".join(con.split("-")[0:-1]).title().replace(" ", "").replace("-", ""))
                    n3 += ' ?x' + var + ' ' + prop + ' ' + value
                    n3 += " . "

                n3 = n3[:-2] + " } . \n"
    
    return n3

def writeClasses(path):
    graph = Graph()

    af = Namespace(baseuri)
    graph.bind('af', af)

    owl = Namespace('http://www.w3.org/2002/07/owl#')
    graph.bind('owl', owl)

    afID = af + "AudioFeature"

    graph.add((
        URIRef(afID),
        RDF.type,
        OWL.Class
    ))

    for obj in cat:
        objID = af+obj.title()
        graph.add((
            URIRef(objID),
            RDF.type,
            OWL.Class
        ))
        oneof = []
        for subcls in val[obj]:
            subID = af+subcls.replace(" ", "").replace("-", "")+obj.title()
            graph.add((
                URIRef(subID),
                RDF.type,
                OWL.Class
            ))
            graph.add((
                URIRef(subID),
                RDFS.subClassOf,
                URIRef(objID)
            ))
            oneof.append(subID)
        # add object properties
        propID = af+obj
        graph.add((
            URIRef(propID),
            RDF.type,
            OWL.ObjectProperty
        ))
        graph.add((
            URIRef(propID),
            RDFS.domain,
            URIRef(afID)
        ))
        graph.add((
            URIRef(propID),
            RDFS.range,
            XSD.string
        ))
            
    graph.serialize(path)
    graph.serialize(path.replace('.rdf', '.n3'), format='n3')

def writeFeatures(path):
    source = Graph()
    source.parse(cataloguePath)
    sourcens = Namespace(baseuri.replace('af-ontology#', ''))
    
    graph = Graph()

    af = Namespace(baseuri)
    graph.bind('af', af)

    owl = Namespace('http://www.w3.org/2002/07/owl#')
    graph.bind('owl', owl)
    
    cns = sourcens['computedIn']
    tns = sourcens['temporalscale']
    for su in source.subjects(RDF.type, OWL.Class):
        ccount = sum(1 for _ in source.objects(su,cns))
        tcount = sum(1 for _ in source.objects(su,tns))
        if ccount > 0 and tcount > 0 and su.find('MPEG7') == -1:
            afid = URIRef(af + su.split('/')[-1])
            graph.add(( afid, RDF.type, af['AudioFeature'] ))
            for pr, ob in source.predicate_objects(su):
                if cat.count(pr.split('/')[-1]) == 1:
                    afpr = URIRef(af + pr.split('/')[-1])
                    graph.add((afid, afpr,
                        af[ob.replace("-", "").title().replace(" ", "") + pr.split('/')[-1].title()]
                    ))
                    graph.add((afid, af['computation'], af[su.split('/')[-1] + '_computation'] ))
    
    graph.serialize(path, format='n3')



writeClasses(ontopath)
writeFeatures(factpath)
writeN3(convertRules(readFile(readpath)),writepath)