annotate rdfpy/writeMarsyasOnto.py @ 1:365a37a2fb6c

added files from pdfextract directory
author nothing@tehis.net
date Mon, 25 Feb 2013 14:47:41 +0000
parents 62d2c72e4223
children 53069717108c
rev   line source
nothing@0 1 import rdflib
nothing@0 2 from rdflib import Graph, RDF, RDFS, plugin, URIRef, Literal, OWL, Namespace
nothing@0 3
nothing@0 4 basedir = '/Users/alo/MusicOntology/features/'
nothing@0 5
nothing@0 6 local = 'http://sovarr.c4dm.eecs.qmul.ac.uk/features/'
nothing@0 7
nothing@0 8 DC = Namespace(u"http://purl.org/dc/elements/1.1/")
nothing@0 9
nothing@0 10 graph = Graph()
nothing@0 11 graph.bind('af', URIRef(local))
nothing@0 12 graph.bind('dc', URIRef('http://purl.org/dc/elements/1.1/'))
nothing@0 13 graph.bind('owl', URIRef('http://www.w3.org/2002/07/owl#'))
nothing@0 14
nothing@0 15 graph.add((
nothing@0 16 URIRef(''),
nothing@0 17 RDF.type,
nothing@0 18 OWL.Ontology
nothing@0 19 ))
nothing@0 20
nothing@0 21 graph.add((
nothing@0 22 URIRef(''),
nothing@0 23 DC['title'],
nothing@0 24 Literal("Marsyas Ontology")
nothing@0 25 ))
nothing@0 26
nothing@0 27 graph.add((
nothing@0 28 URIRef(''),
nothing@0 29 OWL.versionInfo,
nothing@0 30 Literal("Version 0.1")
nothing@0 31 ))
nothing@0 32
nothing@0 33 graph.add((
nothing@0 34 URIRef(''),
nothing@0 35 DC['description'],
nothing@0 36 Literal("This is an ontology derived from Marsyas feature extraction tools for the Audio Features engineering process")
nothing@0 37 ))
nothing@0 38
nothing@0 39
nothing@0 40 source = Graph()
nothing@0 41 source.parse(basedir+'rdfonto/af-Marsyas.n3', format='n3')
nothing@0 42
nothing@0 43 categories = []
nothing@0 44
nothing@0 45 for su, ob in source.subject_objects(URIRef('file:///Users/alo/MusicOntology/features/rdf/type')):
nothing@0 46 if not ob in categories:
nothing@0 47 categories.append(ob)
nothing@0 48
nothing@0 49 graph.add((
nothing@0 50 URIRef(local+'Analysis'),
nothing@0 51 RDF.type,
nothing@0 52 OWL.Class
nothing@0 53 ))
nothing@0 54
nothing@0 55 for category in categories:
nothing@0 56 graph.add((
nothing@0 57 URIRef(local+category),
nothing@0 58 RDF.type,
nothing@0 59 OWL.Class
nothing@0 60 ))
nothing@0 61 graph.add((
nothing@0 62 URIRef(local+category),
nothing@0 63 RDFS.subClassOf,
nothing@0 64 URIRef(local+'Analysis'),
nothing@0 65 ))
nothing@0 66
nothing@0 67 for su in source.subjects(RDF.type, RDFS.Resource):
nothing@0 68 idref = URIRef(local+su.split('/')[-1])
nothing@0 69 graph.add((
nothing@0 70 idref,
nothing@0 71 RDF.type,
nothing@0 72 OWL.Class
nothing@0 73 ))
nothing@0 74
nothing@0 75 count = sum(1 for _ in source.objects(su,URIRef('file:///Users/alo/MusicOntology/features/rdf/type')))
nothing@0 76
nothing@0 77 if count > 0:
nothing@0 78 for ob in source.objects(su,URIRef('file:///Users/alo/MusicOntology/features/rdf/type')):
nothing@0 79 graph.add((
nothing@0 80 idref,
nothing@0 81 RDFS.subClassOf,
nothing@0 82 URIRef(local+ob)
nothing@0 83 ))
nothing@0 84 else:
nothing@0 85 graph.add((
nothing@0 86 idref,
nothing@0 87 RDFS.subClassOf,
nothing@0 88 URIRef(local+'Analysis'),
nothing@0 89 ))
nothing@0 90
nothing@0 91 for ob in source.objects(su,URIRef('http://purl.org/dc/elements/1.1/description')):
nothing@0 92 graph.add((
nothing@0 93 idref,
nothing@0 94 URIRef('http://purl.org/dc/elements/1.1/description'),
nothing@0 95 ob
nothing@0 96 ))
nothing@0 97
nothing@0 98 graph.serialize(basedir + 'rdfonto/Marsyas-onto.rdf')
nothing@0 99 graph.serialize(basedir + 'rdfonto/Marsyas-onto.n3', format='n3')