view rdfpy/writeMarsyas.py @ 0:62d2c72e4223

initial commit
author nothing@tehis.net
date Mon, 25 Feb 2013 14:40:54 +0000
parents
children
line wrap: on
line source
import rdflib, os, fnmatch, urllib2
from rdflib import Graph, RDF, RDFS, plugin, URIRef, Literal, OWL

############# Marsyas ###############
def removeNonAscii(s): return "".join(i for i in s if ord(i)<128)

mdir = '/Users/alo/Development/MIR/marsyas-0.4.7/src/marsyas/'

graph = Graph()
local = 'http://sovarr.c4dm.eecs.qmul.ac.uk/features/'
graph.bind('local', URIRef(local))
graph.bind('dc', URIRef('http://purl.org/dc/elements/1.1/'))

for name in os.listdir(mdir):
    if fnmatch.fnmatch(name, '*.h'):
        file = open(mdir + name)
        code = file.read()
        file.close()
        if ('\ingroup' in code) and ('Analysis' in code):
            if code.find('\class') == -1:
                cl = name[:-2]
            else:    
                cl = code[code.find('\class')+7:code.find('\n', code.find('\class')+7)]
            
            br = code[code.find('brief')+5:code.find('*/', code.find('brief')+5)]
            br = br.replace('\n', ' ')
            br = " ".join(br.split())
            
            br = removeNonAscii(br)
            
            graph.add((
                URIRef(cl), 
                URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), 
                URIRef('http://www.w3.org/2000/01/rdf-schema#Resource')
            ))
            graph.add((
                URIRef(cl), 
                URIRef('http://purl.org/dc/elements/1.1/description'), 
                Literal(br)
            ))
            
graph.serialize('/Users/alo/MusicOntology/features/rdfn3/af-Marsyas.n3', format='n3')