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