comparison pdfextract/writeRDFs.py @ 1:365a37a2fb6c

added files from pdfextract directory
author nothing@tehis.net
date Mon, 25 Feb 2013 14:47:41 +0000
parents
children
comparison
equal deleted inserted replaced
0:62d2c72e4223 1:365a37a2fb6c
1 import rdflib, os, fnmatch, urllib2
2 from rdflib import Graph, RDF, RDFS, plugin, URIRef, Literal, OWL
3 from xml.dom.minidom import parseString
4
5 ############# Vamp ###############
6
7 vampdir = '/Users/alo/Library/Audio/Plug-Ins/Vamp/'
8
9 source = Graph()
10
11 graph = Graph()
12 local = 'http://sovarr.c4dm.eecs.qmul.ac.uk/features/'
13 graph.bind('local', URIRef(local))
14 graph.bind('dc', URIRef('http://purl.org/dc/elements/1.1/'))
15
16 for name in os.listdir(vampdir):
17 if fnmatch.fnmatch(name, '*.n3'):
18
19 print (vampdir + name)
20
21 source.parse(vampdir + name, format='n3')
22
23 for su, pr in source.subject_predicates(URIRef('http://purl.org/ontology/vamp/Plugin')):
24
25 for name in source.objects(su, URIRef('http://purl.org/ontology/vamp/name')):
26 id = name.replace(' ', '').replace('Marsyas-BatchFeatureExtract-', '')
27 feature = name
28 graph.add((
29 URIRef(id),
30 URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
31 URIRef('http://www.w3.org/2000/01/rdf-schema#Resource')
32 ))
33 graph.add((
34 URIRef(id),
35 URIRef(local+'feature'),
36 Literal(feature)
37 ))
38 for domain in source.objects(su, URIRef('http://purl.org/ontology/vamp/input_domain')):
39 dom = domain.split('/')[-1].replace('Domain', '')
40 dom = dom.lower().replace('Time', 'temporal')
41 graph.add((
42 URIRef(id),
43 URIRef(local+'domain'),
44 Literal(dom)
45 ))
46 for desc in source.objects(su, URIRef('http://purl.org/dc/elements/1.1/description')):
47 description = " ".join(desc.split())
48 graph.add((
49 URIRef(id),
50 URIRef('http://purl.org/dc/elements/1.1/description'),
51 Literal(description)
52 ))
53 for maker in source.objects(su, URIRef('http://xmlns.com/foaf/0.1/maker')):
54 for mname in source.objects(maker, URIRef('http://xmlns.com/foaf/0.1/name')):
55 makername = mname
56 graph.add((
57 URIRef(id),
58 URIRef(local+'source'),
59 Literal(makername)
60 ))
61
62 count=sum(1 for _ in source.objects(su, URIRef('http://purl.org/ontology/vamp/output')))
63
64 if count == 1:
65 for it in source.objects(su, URIRef('http://purl.org/ontology/vamp/output')):
66 for output in source.objects(it, URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type')):
67 out = output.split('/')[-1].replace('Output', '')
68 if out.find('Sparse') >= 0 or out.find('Dense') >= 0:
69 graph.add((
70 URIRef(id),
71 URIRef(local+'output'),
72 Literal(out)
73 ))
74
75 else:
76 for it in source.objects(su, URIRef('http://purl.org/ontology/vamp/output')):
77 for name in source.objects(it, URIRef('http://purl.org/dc/elements/1.1/title')):
78 if name != feature:
79 subid = name.replace(' ', '')
80 graph.add((
81 URIRef(subid),
82 URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
83 URIRef('http://www.w3.org/2000/01/rdf-schema#Resource')
84 ))
85 graph.add((
86 URIRef(subid),
87 URIRef(local+'feature'),
88 Literal(name + " (" + feature + ")")
89 ))
90 graph.add((
91 URIRef(subid),
92 URIRef(local+'domain'),
93 Literal(dom)
94 ))
95 graph.add((
96 URIRef(subid),
97 URIRef(local+'source'),
98 Literal(makername)
99 ))
100 for output in source.objects(it, URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type')):
101 out = output.split('/')[-1].replace('Output', '')
102 if out.find('Sparse') >= 0 or out.find('Dense') >= 0:
103 graph.add((
104 URIRef(subid),
105 URIRef(local+'output'),
106 Literal(out)
107 ))
108
109 graph.serialize('/Users/alo/MusicOntology/features/rdf/af-Vamp.rdf')
110
111 ############# Marsyas ###############
112 mdir = '/Users/alo/Development/MIR/marsyas-0.4.7/src/marsyas/'
113
114 madict = {}
115
116 graph = Graph()
117 local = 'http://sovarr.c4dm.eecs.qmul.ac.uk/features/'
118 graph.bind('local', URIRef(local))
119 graph.bind('dc', URIRef('http://purl.org/dc/elements/1.1/'))
120
121 for name in os.listdir(mdir):
122 if fnmatch.fnmatch(name, '*.h'):
123 code = [line.strip() for line in open(mdir + name)]
124 found = False
125 for line in code:
126 if line.find('\ingroup Analysis') >= 0:
127 found = True
128 break
129
130 if found:
131 i = 0
132 cl = ''
133 for line in code:
134 if line.find('\class') >= 0:
135 cl = line.split(' ')[-1]
136 madict[cl] = {'brief': code[i+2][7:]}
137 if code[i+3] != '':
138 madict[cl]['brief'] += code[i+3]
139
140 break
141
142 i += 1
143
144 graph.add((
145 URIRef(cl),
146 URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
147 URIRef('http://www.w3.org/2000/01/rdf-schema#Resource')
148 ))
149
150 graph.add((
151 URIRef(cl),
152 URIRef('http://purl.org/dc/elements/1.1/description'),
153 Literal(madict[cl]['brief'])
154 ))
155
156 graph.serialize('/Users/alo/MusicOntology/features/rdf/af-Marsyas.rdf')
157
158 ############# jMIR ###############
159 jdir = '/Users/alo/Development/MIR/jAudio/jAudio/'
160 jsrc = '/Users/alo/Development/MIR/jAudio/jAudio/src/jAudioFeatureExtractor/AudioFeatures/'
161
162 file = urllib2.urlopen('file://' + jdir + 'features.xml')
163 data = file.read()
164 file.close()
165
166 dom = parseString(data)
167 jmir = dom.getElementsByTagName('feature')
168
169 graph = Graph()
170 local = 'http://sovarr.c4dm.eecs.qmul.ac.uk/features/'
171 graph.bind('local', URIRef(local))
172 graph.bind('dc', URIRef('http://purl.org/dc/elements/1.1/'))
173
174 for nodes in jmir:
175 jname = nodes.childNodes[1].firstChild.nodeValue.split('.')[-1]
176 graph.add((
177 URIRef(jname),
178 URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
179 URIRef('http://www.w3.org/2000/01/rdf-schema#Resource')
180 ))
181 file = open(jsrc + jname + '.java')
182 code = file.read()
183 searchstr = 'String name ='
184 start = code.find(searchstr) + len(searchstr)
185 start = code.find('"', start) + 1
186 end = code.find('"', start)
187 name = code[start:end]
188
189 if name > "":
190 graph.add((
191 URIRef(jname),
192 URIRef(local+'name'),
193 Literal(name)
194 ))
195
196 searchstr = 'String description'
197 start = code.find(searchstr) + len(searchstr)
198 start = code.find('"', start) + 1
199 end = code.find('";', start)
200 desc = code[start:end]
201 desc = desc.replace("
", "").replace("\t", "").replace('\n', '').replace('+', '').replace('"', '').replace(';', '').replace('//\n', '')
202 desc = " ".join(desc.split())
203
204 if desc > "":
205 graph.add((
206 URIRef(jname),
207 URIRef('http://purl.org/dc/elements/1.1/description'),
208 Literal(desc)
209 ))
210
211
212 graph.serialize('/Users/alo/MusicOntology/features/rdf/af-jMIR.rdf')
213
214
215 ############# yaafe ###############
216
217 graph = Graph()
218 local = 'http://sovarr.c4dm.eecs.qmul.ac.uk/features/'
219 graph.bind('local', URIRef(local))
220 graph.bind('dc', URIRef('http://purl.org/dc/elements/1.1/'))
221
222 path = "/Users/alo/Development/MIR/yaafe-v0.64/src_python/yaafefeatures.py"
223
224 lines = [line.strip() for line in open(path)]
225
226 count = 0
227
228 for ln in lines:
229 if ln.find('class ') >= 0:
230 yname = ln[6:ln.find('(AudioFeature)')]
231 desc = lines[count+2]
232 desc = desc.replace("`", "").replace("<", "").replace(">", "")
233 desc = " ".join(desc.split())
234
235 graph.add((
236 URIRef(yname),
237 URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
238 URIRef('http://www.w3.org/2000/01/rdf-schema#Resource')
239 ))
240
241 graph.add((
242 URIRef(yname),
243 URIRef('http://purl.org/dc/elements/1.1/description'),
244 Literal(desc)
245 ))
246
247 count += 1
248
249 graph.serialize('/Users/alo/MusicOntology/features/rdf/af-Yaafe.rdf')
250
251 ############# libXtract ###############
252 graph = Graph()
253 local = 'http://sovarr.c4dm.eecs.qmul.ac.uk/features/'
254 graph.bind('local', URIRef(local))
255 graph.bind('dc', URIRef('http://purl.org/dc/elements/1.1/'))
256
257 path = '/Users/alo/Development/MIR/LibXtract/xtract/libxtract.h'
258
259 lines = [line.strip() for line in open(path)]
260
261 xtract = lines[(lines.index('enum xtract_features_ {')+1):(lines.index('XTRACT_WINDOWED')-1)]
262
263 for ln in xtract:
264 xname = ln[(ln.index('_')+1):-1].replace("_", " ").lower().capitalize()
265
266 graph.add((
267 URIRef(xname),
268 URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
269 URIRef('http://www.w3.org/2000/01/rdf-schema#Resource')
270 ))
271
272 graph.serialize('/Users/alo/MusicOntology/features/rdf/af-libXtract.rdf')