annotate 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
rev   line source
nothing@18 1 import rdflib, os, fnmatch, urllib2
nothing@18 2 from rdflib import Graph, RDF, RDFS, plugin, URIRef, Literal, OWL, XSD, Namespace
nothing@18 3
nothing@18 4 graph = Graph()
nothing@18 5
nothing@18 6 af = Namespace('http://sovarr.c4dm.eecs.qmul.ac.uk/features/')
nothing@18 7 graph.bind('local', af)
nothing@18 8
nothing@18 9 dc = Namespace('http://purl.org/dc/elements/1.1/')
nothing@18 10 graph.bind('dc', dc)
nothing@18 11
nothing@18 12 categories = [
nothing@18 13 "Temporal",
nothing@18 14 "Energy",
nothing@18 15 "Spectral",
nothing@18 16 "Harmonic",
nothing@18 17 "Perceptual",
nothing@18 18 "Various"
nothing@18 19 ]
nothing@18 20
nothing@18 21 subcategories = {
nothing@18 22 "Temporal": ["Global Temporal Feature", "Instantaneous Temporal Feature"],
nothing@18 23 "Spectral": ["Spectral Shape", "Global Spectral Shape Description"],
nothing@18 24 "Harmonic": ["Harmonic Spectral Shape"],
nothing@18 25 "Perceptual": ["Perceptual Spectral Envelope Shape"],
nothing@18 26
nothing@18 27 }
nothing@18 28
nothing@18 29 flat = []
nothing@18 30
nothing@18 31 lines = [line.strip() for line in open('pdfextract/cuidado.txt')]
nothing@18 32
nothing@18 33 category = "Temporal"
nothing@18 34 subcategory = "Global Temporal Feature"
nothing@18 35
nothing@18 36 lineIndex = 5
nothing@18 37
nothing@18 38 for line in lines[5:]:
nothing@18 39 addFeature = True
nothing@18 40 if line.find("Features") > -1:
nothing@18 41 addFeature = False
nothing@18 42 if categories.count(line[0:line.find("Features")-1]) > 0:
nothing@18 43 if line[0:line.find("Features")-1] != category:
nothing@18 44 category = line[0:line.find("Features")-1]
nothing@18 45 subcategory = ""
nothing@18 46 for sub in flat:
nothing@18 47 if line == sub:
nothing@18 48 addFeature = False
nothing@18 49 if subcategory != line:
nothing@18 50 subcategory = line
nothing@18 51
nothing@18 52 if line.count(":") == 0 and line.count("_") == 0 and len(line) > 2 and addFeature:
nothing@18 53 id = af + line.title().replace(" ", "")
nothing@18 54 graph.add((
nothing@18 55 URIRef(id),
nothing@18 56 RDF.type,
nothing@18 57 RDFS.Resource
nothing@18 58 ))
nothing@18 59 graph.add((
nothing@18 60 URIRef(id),
nothing@18 61 af['feature'],
nothing@18 62 Literal(line)
nothing@18 63 ))
nothing@18 64 graph.add((
nothing@18 65 URIRef(id),
nothing@18 66 URIRef(af+"Dimensions"),
nothing@18 67 Literal(lines[lineIndex+2])
nothing@18 68 ))
nothing@18 69
nothing@18 70 graph.add((
nothing@18 71 URIRef(id),
nothing@18 72 URIRef(af+"FrameBased"),
nothing@18 73 Literal(lines[lineIndex+1])
nothing@18 74 ))
nothing@18 75
nothing@18 76 if subcategory == "":
nothing@18 77 graph.add((
nothing@18 78 URIRef(id),
nothing@18 79 af['typeOfFeature'],
nothing@18 80 Literal(category + " Feature")
nothing@18 81 ))
nothing@18 82 else:
nothing@18 83 graph.add((
nothing@18 84 URIRef(id),
nothing@18 85 af['typeOfFeature'],
nothing@18 86 Literal(subcategory.replace(" ", ""))
nothing@18 87 ))
nothing@18 88
nothing@18 89 lineIndex += 1
nothing@18 90
nothing@18 91
nothing@18 92 graph.serialize('/Users/alo/MusicOntology/features/rdf/af-cuidado.rdf')
nothing@18 93 graph.serialize('/Users/alo/MusicOntology/features/rdfn3/af-cuidado.n3', format="n3")