nothing@18: import rdflib, os, fnmatch, urllib2 nothing@18: from rdflib import Graph, RDF, RDFS, plugin, URIRef, Literal, OWL, XSD, Namespace nothing@18: nothing@18: graph = Graph() nothing@18: nothing@18: af = Namespace('http://sovarr.c4dm.eecs.qmul.ac.uk/features/') nothing@18: graph.bind('local', af) nothing@18: nothing@18: dc = Namespace('http://purl.org/dc/elements/1.1/') nothing@18: graph.bind('dc', dc) nothing@18: nothing@18: categories = [ nothing@18: "Temporal", nothing@18: "Energy", nothing@18: "Spectral", nothing@18: "Harmonic", nothing@18: "Perceptual", nothing@18: "Various" nothing@18: ] nothing@18: nothing@18: subcategories = { nothing@18: "Temporal": ["Global Temporal Feature", "Instantaneous Temporal Feature"], nothing@18: "Spectral": ["Spectral Shape", "Global Spectral Shape Description"], nothing@18: "Harmonic": ["Harmonic Spectral Shape"], nothing@18: "Perceptual": ["Perceptual Spectral Envelope Shape"], nothing@18: nothing@18: } nothing@18: nothing@18: flat = [] nothing@18: nothing@18: lines = [line.strip() for line in open('pdfextract/cuidado.txt')] nothing@18: nothing@18: category = "Temporal" nothing@18: subcategory = "Global Temporal Feature" nothing@18: nothing@18: lineIndex = 5 nothing@18: nothing@18: for line in lines[5:]: nothing@18: addFeature = True nothing@18: if line.find("Features") > -1: nothing@18: addFeature = False nothing@18: if categories.count(line[0:line.find("Features")-1]) > 0: nothing@18: if line[0:line.find("Features")-1] != category: nothing@18: category = line[0:line.find("Features")-1] nothing@18: subcategory = "" nothing@18: for sub in flat: nothing@18: if line == sub: nothing@18: addFeature = False nothing@18: if subcategory != line: nothing@18: subcategory = line nothing@18: nothing@18: if line.count(":") == 0 and line.count("_") == 0 and len(line) > 2 and addFeature: nothing@18: id = af + line.title().replace(" ", "") nothing@18: graph.add(( nothing@18: URIRef(id), nothing@18: RDF.type, nothing@18: RDFS.Resource nothing@18: )) nothing@18: graph.add(( nothing@18: URIRef(id), nothing@18: af['feature'], nothing@18: Literal(line) nothing@18: )) nothing@18: graph.add(( nothing@18: URIRef(id), nothing@18: URIRef(af+"Dimensions"), nothing@18: Literal(lines[lineIndex+2]) nothing@18: )) nothing@18: nothing@18: graph.add(( nothing@18: URIRef(id), nothing@18: URIRef(af+"FrameBased"), nothing@18: Literal(lines[lineIndex+1]) nothing@18: )) nothing@18: nothing@18: if subcategory == "": nothing@18: graph.add(( nothing@18: URIRef(id), nothing@18: af['typeOfFeature'], nothing@18: Literal(category + " Feature") nothing@18: )) nothing@18: else: nothing@18: graph.add(( nothing@18: URIRef(id), nothing@18: af['typeOfFeature'], nothing@18: Literal(subcategory.replace(" ", "")) nothing@18: )) nothing@18: nothing@18: lineIndex += 1 nothing@18: nothing@18: nothing@18: graph.serialize('/Users/alo/MusicOntology/features/rdf/af-cuidado.rdf') nothing@18: graph.serialize('/Users/alo/MusicOntology/features/rdfn3/af-cuidado.n3', format="n3")