view rdfpy/writeCuidado.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('local', af)

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

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

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 = []

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.title().replace(" ", "")
        graph.add((
            URIRef(id),
            RDF.type,
            RDFS.Resource
        ))
        graph.add((
            URIRef(id),
            af['feature'],
            Literal(line)
        ))
        graph.add((
            URIRef(id),
            URIRef(af+"Dimensions"),
            Literal(lines[lineIndex+2])
        ))
                    
        graph.add((
            URIRef(id),
            URIRef(af+"FrameBased"),
            Literal(lines[lineIndex+1])
        ))
        
        if subcategory == "":
            graph.add((
                URIRef(id),
                af['typeOfFeature'],
                Literal(category + " Feature")
            ))
        else:
            graph.add((
                URIRef(id),
                af['typeOfFeature'],
                Literal(subcategory.replace(" ", ""))
            ))

    lineIndex += 1
            

graph.serialize('/Users/alo/MusicOntology/features/rdf/af-cuidado.rdf')
graph.serialize('/Users/alo/MusicOntology/features/rdfn3/af-cuidado.n3', format="n3")