view rdfpy/writeCuidadoOnto.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, os, fnmatch, urllib2
from rdflib import Graph, RDF, RDFS, plugin, URIRef, Literal, OWL, XSD, Namespace

graph = Graph()

af = Namespace('http://sovarr.c4dm.eecs.qmul.ac.uk/features/')
graph.bind('af', af)

dc = Namespace('http://purl.org/dc/elements/1.1/')
graph.bind('dc', dc)

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

xsd = Namespace('http://www.w3.org/2001/XMLSchema#')
graph.bind('xsd', xsd)

categories = [
    "Temporal",
    "Energy",
    "Spectral",
    "Harmonic",
    "Perceptual",
    "Various"
]

graph.add((
    URIRef(af+"Feature"),
    RDF.type,
    OWL.Class
))
graph.add((
    URIRef(af+"Feature"),
    RDFS.label,
    Literal("CUIDADO Audio Feature", lang="en")
))

#properties
graph.add((
    URIRef(af+"Dimensions"),
    RDF.type,
    OWL.ObjectProperty
))
graph.add((
    URIRef(af+"Dimensions"),
    RDFS.range,
    XSD.Integer
))
graph.add((
    URIRef(af+"Dimensions"),
    RDFS.domain,
    URIRef(af+"Feature")
))

graph.add((
    URIRef(af+"FrameBased"),
    RDF.type,
    OWL.ObjectProperty
))
graph.add((
    URIRef(af+"FrameBased"),
    RDFS.range,
    XSD.Boolean
))
graph.add((
    URIRef(af+"FrameBased"),
    RDFS.domain,
    URIRef(af+"Feature")
))


for category in categories:
    graph.add((
        URIRef(af + category + "Feature"),
        RDF.type,
        OWL.Class
    ))
    graph.add((
        URIRef(af + category + "Feature"),
        RDFS.label,
        Literal(category + " Feature", lang="en")
    ))
    graph.add((
        URIRef(af + category + "Feature"),
        RDFS.subClassOf,
        URIRef(af+"Feature")
    ))

subcategories = {
    "Temporal": ["Global Temporal Feature", "Instantaneous Temporal Feature"],
    "Spectral": ["Spectral Shape", "Global Spectral Shape Description"],
    "Harmonic": ["Harmonic Spectral Shape"],
    "Perceptual": ["Perceptual Spectral Envelope Shape"],
    
}

flat = []

for category in subcategories.keys():
    for subcategory in subcategories[category]:
        flat.append(subcategory)
        id = af + subcategory.replace(" ", "")
        graph.add((
            URIRef(id),
            RDF.type,
            OWL.Class
        ))
        graph.add((
            URIRef(id),
            RDFS.label,
            Literal(subcategory, lang="en")
        ))
        graph.add((
            URIRef(id),
            RDFS.subClassOf,
            URIRef(af + category + "Feature")
        ))

lines = [line.strip() for line in open('pdfextract/cuidado.txt')]

category = "Temporal"
subcategory = "Global Temporal Feature"

lineIndex = 5

for line in lines[5:]:
    addFeature = True
    if line.find("Features") > -1:
        addFeature = False
        if categories.count(line[0:line.find("Features")-1]) > 0:
            if line[0:line.find("Features")-1] != category:
                category = line[0:line.find("Features")-1]
                subcategory = ""
    for sub in flat:
        if line == sub:
            addFeature = False
            if subcategory != line:
                subcategory = line
            
    if line.count(":") == 0 and line.count("_") == 0 and len(line) > 2 and addFeature:
        id = af + line.replace(" ", "")
        graph.add((
            URIRef(id),
            RDF.type,
            OWL.Class
        ))
        graph.add((
            URIRef(id),
            RDFS.label,
            Literal(line, lang="en")
        ))
        graph.add((
            URIRef(id),
            URIRef(af+"Dimensions"),
            Literal(int(lines[lineIndex+2]))
        ))
        
        if lines[lineIndex+1] == "y":
            frameBased = True
        else:
            frameBased = False
            
        graph.add((
            URIRef(id),
            URIRef(af+"FrameBased"),
            Literal(frameBased)
        ))
        
        if subcategory == "":
            graph.add((
                URIRef(id),
                RDFS.subClassOf,
                URIRef(af + category + "Feature")
            ))
        else:
            graph.add((
                URIRef(id),
                RDFS.subClassOf,
                URIRef(af + subcategory.replace(" ", ""))
            ))

    lineIndex += 1
            

graph.serialize('/Users/alo/MusicOntology/features/rdfonto/cuidado-onto.n3', format="n3")