comparison rdfpy/af-vocabulary.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
2 from rdflib import Graph, RDF, RDFS, plugin, URIRef, Literal, OWL, Namespace, XSD
3
4 plugin.register(
5 'sparql', rdflib.query.Processor,
6 'rdfextras.sparql.processor', 'Processor')
7 plugin.register(
8 'sparql', rdflib.query.Result,
9 'rdfextras.sparql.query', 'SPARQLQueryResult')
10
11 def getTools(graph, nsuri):
12 return graph.query(
13 """SELECT DISTINCT ?tool
14 WHERE {
15 ?x af:computedIn ?tool .
16 ?x af:feature ?feature
17 }
18 ORDER BY ?tool""",
19 initNs=dict(
20 af=Namespace(nsuri))
21 )
22
23
24 url = 'http://sovarr.c4dm.eecs.qmul.ac.uk/af/'
25 afv = Namespace(url+'vocabulary/1.0#')
26 basedir = '/Users/alo/MusicOntology/features/'
27
28 afc = Namespace(url + 'features/')
29
30 af = Namespace(url+'ontology/1.0#')
31
32 cat = Namespace('http://sovarr.c4dm.eecs.qmul.ac.uk/features/')
33
34 vs = Namespace('http://www.w3.org/2003/06/sw-vocab-status/ns#')
35
36 graph = Graph()
37 graph.bind('afv', afv)
38 graph.bind('owl', OWL)
39 graph.bind('af', af)
40 graph.bind('vs', vs)
41 graph.bind('xsd', XSD)
42
43 graph.add(( vs['term_status'], RDF.type, OWL.AnnotationProperty ))
44 graph.add(( afv['computedIn'], RDF.type, OWL.Property ))
45 graph.add(( afv['computedIn'], RDFS.range, XSD.String ))
46 graph.add(( afv['computedIn'], RDFS.domain, af['AudioFeature'] ))
47
48 source = Graph()
49 source.parse(basedir + 'af-catalogue-01.rdf')
50
51 tooldict = {}
52
53 for tool in getTools(source, cat):
54 idt = afv[tool[0]]
55 tooldict[tool[0]] = idt
56 graph.add((idt, RDF.type, OWL.Class))
57 graph.add((idt, RDFS.label, tool[0]))
58 graph.add((idt, RDFS.comment, tool[0] ))
59 graph.add((idt, vs['term_status'], Literal("testing", lang="en") ))
60
61 for sub in source.subjects(RDF.type, OWL.Class):
62 name = sub.split('/')[-1]
63 id = name.replace("(", "").replace(")", "").replace("4Hz", "")
64 #id = ''.join(i for i in id if not i.isdigit())
65 id = URIRef(afv+id)
66 graph.add((id, RDF.type, OWL.Class))
67 graph.add((id, RDFS.subClassOf, af['AudioFeature']))
68 graph.add((id, vs['term_status'], Literal("testing", lang="en") ))
69 count = sum(1 for _ in source.objects(sub, afc['feature']))
70 if count > 0:
71 labelAdded = False
72 for label in source.objects(sub, afc['feature']):
73 if not labelAdded:
74 graph.add(( id, RDFS.label, Literal(label, lang="en") ))
75 graph.add(( id, RDFS.comment, Literal(label) ))
76 labelAdded = True
77 else:
78 if name.find("ADRess") > -1:
79 graph.add(( id, RDFS.label, Literal(name + " (Azimuth Discrimination and Resynthesis)", lang="en") ))
80 graph.add(( id, RDFS.comment, Literal(name + " (Azimuth Discrimination and Resynthesis)") ))
81 else:
82 if name.find("Aim") == 0:
83 graph.add(( id, RDFS.label, Literal(name + " (Auditory Image Model) ", lang="en") ))
84 graph.add(( id, RDFS.comment, Literal(name + " (Auditory Image Model) ") ))
85 else:
86 graph.add(( id, RDFS.label, Literal(name, lang="en")))
87 graph.add(( id, RDFS.comment, Literal(name) ))
88
89 for tool in source.objects(sub, cat['computedIn']):
90 graph.add((id, afv['computedIn'], tooldict[tool]))
91
92 graph.serialize(basedir+'rdfonto/af-vocabulary.rdf')
93 graph.serialize(basedir+'rdfonto/af-vocabulary.n3', format="n3")
94