nothing@18: import rdflib, os, fnmatch, urllib2 nothing@18: from rdflib import Graph, RDF, RDFS, plugin, URIRef, Literal, OWL, XSD, Namespace nothing@18: nothing@18: graph = Graph() nothing@18: nothing@18: af = Namespace('http://sovarr.c4dm.eecs.qmul.ac.uk/features/') nothing@18: graph.bind('af', af) nothing@18: nothing@18: dc = Namespace('http://purl.org/dc/elements/1.1/') nothing@18: graph.bind('dc', dc) nothing@18: nothing@18: owl = Namespace('http://www.w3.org/2002/07/owl#') nothing@18: graph.bind('owl', owl) nothing@18: nothing@18: xsd = Namespace('http://www.w3.org/2001/XMLSchema#') nothing@18: graph.bind('xsd', xsd) nothing@18: nothing@18: categories = [ nothing@18: "Temporal", nothing@18: "Energy", nothing@18: "Spectral", nothing@18: "Harmonic", nothing@18: "Perceptual", nothing@18: "Various" nothing@18: ] nothing@18: nothing@18: graph.add(( nothing@18: URIRef(af+"Feature"), nothing@18: RDF.type, nothing@18: OWL.Class nothing@18: )) nothing@18: graph.add(( nothing@18: URIRef(af+"Feature"), nothing@18: RDFS.label, nothing@18: Literal("CUIDADO Audio Feature", lang="en") nothing@18: )) nothing@18: nothing@18: #properties nothing@18: graph.add(( nothing@18: URIRef(af+"Dimensions"), nothing@18: RDF.type, nothing@18: OWL.ObjectProperty nothing@18: )) nothing@18: graph.add(( nothing@18: URIRef(af+"Dimensions"), nothing@18: RDFS.range, nothing@18: XSD.Integer nothing@18: )) nothing@18: graph.add(( nothing@18: URIRef(af+"Dimensions"), nothing@18: RDFS.domain, nothing@18: URIRef(af+"Feature") nothing@18: )) nothing@18: nothing@18: graph.add(( nothing@18: URIRef(af+"FrameBased"), nothing@18: RDF.type, nothing@18: OWL.ObjectProperty nothing@18: )) nothing@18: graph.add(( nothing@18: URIRef(af+"FrameBased"), nothing@18: RDFS.range, nothing@18: XSD.Boolean nothing@18: )) nothing@18: graph.add(( nothing@18: URIRef(af+"FrameBased"), nothing@18: RDFS.domain, nothing@18: URIRef(af+"Feature") nothing@18: )) nothing@18: nothing@18: nothing@18: for category in categories: nothing@18: graph.add(( nothing@18: URIRef(af + category + "Feature"), nothing@18: RDF.type, nothing@18: OWL.Class nothing@18: )) nothing@18: graph.add(( nothing@18: URIRef(af + category + "Feature"), nothing@18: RDFS.label, nothing@18: Literal(category + " Feature", lang="en") nothing@18: )) nothing@18: graph.add(( nothing@18: URIRef(af + category + "Feature"), nothing@18: RDFS.subClassOf, nothing@18: URIRef(af+"Feature") nothing@18: )) nothing@18: nothing@18: subcategories = { nothing@18: "Temporal": ["Global Temporal Feature", "Instantaneous Temporal Feature"], nothing@18: "Spectral": ["Spectral Shape", "Global Spectral Shape Description"], nothing@18: "Harmonic": ["Harmonic Spectral Shape"], nothing@18: "Perceptual": ["Perceptual Spectral Envelope Shape"], nothing@18: nothing@18: } nothing@18: nothing@18: flat = [] nothing@18: nothing@18: for category in subcategories.keys(): nothing@18: for subcategory in subcategories[category]: nothing@18: flat.append(subcategory) nothing@18: id = af + subcategory.replace(" ", "") nothing@18: graph.add(( nothing@18: URIRef(id), nothing@18: RDF.type, nothing@18: OWL.Class nothing@18: )) nothing@18: graph.add(( nothing@18: URIRef(id), nothing@18: RDFS.label, nothing@18: Literal(subcategory, lang="en") nothing@18: )) nothing@18: graph.add(( nothing@18: URIRef(id), nothing@18: RDFS.subClassOf, nothing@18: URIRef(af + category + "Feature") nothing@18: )) nothing@18: nothing@18: lines = [line.strip() for line in open('pdfextract/cuidado.txt')] nothing@18: nothing@18: category = "Temporal" nothing@18: subcategory = "Global Temporal Feature" nothing@18: nothing@18: lineIndex = 5 nothing@18: nothing@18: for line in lines[5:]: nothing@18: addFeature = True nothing@18: if line.find("Features") > -1: nothing@18: addFeature = False nothing@18: if categories.count(line[0:line.find("Features")-1]) > 0: nothing@18: if line[0:line.find("Features")-1] != category: nothing@18: category = line[0:line.find("Features")-1] nothing@18: subcategory = "" nothing@18: for sub in flat: nothing@18: if line == sub: nothing@18: addFeature = False nothing@18: if subcategory != line: nothing@18: subcategory = line nothing@18: nothing@18: if line.count(":") == 0 and line.count("_") == 0 and len(line) > 2 and addFeature: nothing@18: id = af + line.replace(" ", "") nothing@18: graph.add(( nothing@18: URIRef(id), nothing@18: RDF.type, nothing@18: OWL.Class nothing@18: )) nothing@18: graph.add(( nothing@18: URIRef(id), nothing@18: RDFS.label, nothing@18: Literal(line, lang="en") nothing@18: )) nothing@18: graph.add(( nothing@18: URIRef(id), nothing@18: URIRef(af+"Dimensions"), nothing@18: Literal(int(lines[lineIndex+2])) nothing@18: )) nothing@18: nothing@18: if lines[lineIndex+1] == "y": nothing@18: frameBased = True nothing@18: else: nothing@18: frameBased = False nothing@18: nothing@18: graph.add(( nothing@18: URIRef(id), nothing@18: URIRef(af+"FrameBased"), nothing@18: Literal(frameBased) nothing@18: )) nothing@18: nothing@18: if subcategory == "": nothing@18: graph.add(( nothing@18: URIRef(id), nothing@18: RDFS.subClassOf, nothing@18: URIRef(af + category + "Feature") nothing@18: )) nothing@18: else: nothing@18: graph.add(( nothing@18: URIRef(id), nothing@18: RDFS.subClassOf, nothing@18: URIRef(af + subcategory.replace(" ", "")) nothing@18: )) nothing@18: nothing@18: lineIndex += 1 nothing@18: nothing@18: nothing@18: graph.serialize('/Users/alo/MusicOntology/features/rdfonto/cuidado-onto.n3', format="n3")