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