annotate 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
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('af', 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 owl = Namespace('http://www.w3.org/2002/07/owl#')
nothing@18 13 graph.bind('owl', owl)
nothing@18 14
nothing@18 15 xsd = Namespace('http://www.w3.org/2001/XMLSchema#')
nothing@18 16 graph.bind('xsd', xsd)
nothing@18 17
nothing@18 18 categories = [
nothing@18 19 "Temporal",
nothing@18 20 "Energy",
nothing@18 21 "Spectral",
nothing@18 22 "Harmonic",
nothing@18 23 "Perceptual",
nothing@18 24 "Various"
nothing@18 25 ]
nothing@18 26
nothing@18 27 graph.add((
nothing@18 28 URIRef(af+"Feature"),
nothing@18 29 RDF.type,
nothing@18 30 OWL.Class
nothing@18 31 ))
nothing@18 32 graph.add((
nothing@18 33 URIRef(af+"Feature"),
nothing@18 34 RDFS.label,
nothing@18 35 Literal("CUIDADO Audio Feature", lang="en")
nothing@18 36 ))
nothing@18 37
nothing@18 38 #properties
nothing@18 39 graph.add((
nothing@18 40 URIRef(af+"Dimensions"),
nothing@18 41 RDF.type,
nothing@18 42 OWL.ObjectProperty
nothing@18 43 ))
nothing@18 44 graph.add((
nothing@18 45 URIRef(af+"Dimensions"),
nothing@18 46 RDFS.range,
nothing@18 47 XSD.Integer
nothing@18 48 ))
nothing@18 49 graph.add((
nothing@18 50 URIRef(af+"Dimensions"),
nothing@18 51 RDFS.domain,
nothing@18 52 URIRef(af+"Feature")
nothing@18 53 ))
nothing@18 54
nothing@18 55 graph.add((
nothing@18 56 URIRef(af+"FrameBased"),
nothing@18 57 RDF.type,
nothing@18 58 OWL.ObjectProperty
nothing@18 59 ))
nothing@18 60 graph.add((
nothing@18 61 URIRef(af+"FrameBased"),
nothing@18 62 RDFS.range,
nothing@18 63 XSD.Boolean
nothing@18 64 ))
nothing@18 65 graph.add((
nothing@18 66 URIRef(af+"FrameBased"),
nothing@18 67 RDFS.domain,
nothing@18 68 URIRef(af+"Feature")
nothing@18 69 ))
nothing@18 70
nothing@18 71
nothing@18 72 for category in categories:
nothing@18 73 graph.add((
nothing@18 74 URIRef(af + category + "Feature"),
nothing@18 75 RDF.type,
nothing@18 76 OWL.Class
nothing@18 77 ))
nothing@18 78 graph.add((
nothing@18 79 URIRef(af + category + "Feature"),
nothing@18 80 RDFS.label,
nothing@18 81 Literal(category + " Feature", lang="en")
nothing@18 82 ))
nothing@18 83 graph.add((
nothing@18 84 URIRef(af + category + "Feature"),
nothing@18 85 RDFS.subClassOf,
nothing@18 86 URIRef(af+"Feature")
nothing@18 87 ))
nothing@18 88
nothing@18 89 subcategories = {
nothing@18 90 "Temporal": ["Global Temporal Feature", "Instantaneous Temporal Feature"],
nothing@18 91 "Spectral": ["Spectral Shape", "Global Spectral Shape Description"],
nothing@18 92 "Harmonic": ["Harmonic Spectral Shape"],
nothing@18 93 "Perceptual": ["Perceptual Spectral Envelope Shape"],
nothing@18 94
nothing@18 95 }
nothing@18 96
nothing@18 97 flat = []
nothing@18 98
nothing@18 99 for category in subcategories.keys():
nothing@18 100 for subcategory in subcategories[category]:
nothing@18 101 flat.append(subcategory)
nothing@18 102 id = af + subcategory.replace(" ", "")
nothing@18 103 graph.add((
nothing@18 104 URIRef(id),
nothing@18 105 RDF.type,
nothing@18 106 OWL.Class
nothing@18 107 ))
nothing@18 108 graph.add((
nothing@18 109 URIRef(id),
nothing@18 110 RDFS.label,
nothing@18 111 Literal(subcategory, lang="en")
nothing@18 112 ))
nothing@18 113 graph.add((
nothing@18 114 URIRef(id),
nothing@18 115 RDFS.subClassOf,
nothing@18 116 URIRef(af + category + "Feature")
nothing@18 117 ))
nothing@18 118
nothing@18 119 lines = [line.strip() for line in open('pdfextract/cuidado.txt')]
nothing@18 120
nothing@18 121 category = "Temporal"
nothing@18 122 subcategory = "Global Temporal Feature"
nothing@18 123
nothing@18 124 lineIndex = 5
nothing@18 125
nothing@18 126 for line in lines[5:]:
nothing@18 127 addFeature = True
nothing@18 128 if line.find("Features") > -1:
nothing@18 129 addFeature = False
nothing@18 130 if categories.count(line[0:line.find("Features")-1]) > 0:
nothing@18 131 if line[0:line.find("Features")-1] != category:
nothing@18 132 category = line[0:line.find("Features")-1]
nothing@18 133 subcategory = ""
nothing@18 134 for sub in flat:
nothing@18 135 if line == sub:
nothing@18 136 addFeature = False
nothing@18 137 if subcategory != line:
nothing@18 138 subcategory = line
nothing@18 139
nothing@18 140 if line.count(":") == 0 and line.count("_") == 0 and len(line) > 2 and addFeature:
nothing@18 141 id = af + line.replace(" ", "")
nothing@18 142 graph.add((
nothing@18 143 URIRef(id),
nothing@18 144 RDF.type,
nothing@18 145 OWL.Class
nothing@18 146 ))
nothing@18 147 graph.add((
nothing@18 148 URIRef(id),
nothing@18 149 RDFS.label,
nothing@18 150 Literal(line, lang="en")
nothing@18 151 ))
nothing@18 152 graph.add((
nothing@18 153 URIRef(id),
nothing@18 154 URIRef(af+"Dimensions"),
nothing@18 155 Literal(int(lines[lineIndex+2]))
nothing@18 156 ))
nothing@18 157
nothing@18 158 if lines[lineIndex+1] == "y":
nothing@18 159 frameBased = True
nothing@18 160 else:
nothing@18 161 frameBased = False
nothing@18 162
nothing@18 163 graph.add((
nothing@18 164 URIRef(id),
nothing@18 165 URIRef(af+"FrameBased"),
nothing@18 166 Literal(frameBased)
nothing@18 167 ))
nothing@18 168
nothing@18 169 if subcategory == "":
nothing@18 170 graph.add((
nothing@18 171 URIRef(id),
nothing@18 172 RDFS.subClassOf,
nothing@18 173 URIRef(af + category + "Feature")
nothing@18 174 ))
nothing@18 175 else:
nothing@18 176 graph.add((
nothing@18 177 URIRef(id),
nothing@18 178 RDFS.subClassOf,
nothing@18 179 URIRef(af + subcategory.replace(" ", ""))
nothing@18 180 ))
nothing@18 181
nothing@18 182 lineIndex += 1
nothing@18 183
nothing@18 184
nothing@18 185 graph.serialize('/Users/alo/MusicOntology/features/rdfonto/cuidado-onto.n3', format="n3")