nothing@0: import rdflib, os, fnmatch, urllib2 nothing@0: from rdflib import Graph, RDF, RDFS, plugin, URIRef, Literal, OWL, XSD, Namespace nothing@0: from xml.dom.minidom import parseString nothing@0: nothing@0: names = [line.strip() for line in open('pdfextract/names.txt')] nothing@0: cat = [line.strip() for line in open('pdfextract/categories.txt')] nothing@0: sig = [line.strip() for line in open('pdfextract/sig.txt')] nothing@0: nothing@0: basedir = '/Users/alo/MusicOntology/features/' nothing@0: nothing@0: local = 'http://sovarr.c4dm.eecs.qmul.ac.uk/features/' nothing@0: nothing@0: DC = Namespace(u"http://purl.org/dc/elements/1.1/") nothing@0: VS = Namespace(u"http://www.w3.org/2003/06/sw-vocab-status/ns#") nothing@0: nothing@0: graph = Graph() nothing@0: graph.bind('af', URIRef(local)) nothing@0: graph.bind('dc', URIRef('http://purl.org/dc/elements/1.1/')) nothing@0: graph.bind('owl', URIRef('http://www.w3.org/2002/07/owl#')) nothing@0: graph.bind('xsd', URIRef('http://www.w3.org/2001/XMLSchema#')) nothing@0: graph.bind('vs', URIRef('http://www.w3.org/2003/06/sw-vocab-status/ns#')) nothing@0: nothing@0: graph.add(( nothing@0: URIRef(''), nothing@0: RDF.type, nothing@0: OWL.Ontology nothing@0: )) nothing@0: nothing@0: graph.add(( nothing@0: URIRef(''), nothing@0: DC['title'], nothing@0: Literal("Audio Features Base Ontology") nothing@0: )) nothing@0: nothing@0: graph.add(( nothing@0: URIRef(''), nothing@0: OWL.versionInfo, nothing@0: Literal("Version 0.1") nothing@0: )) nothing@0: nothing@0: graph.add(( nothing@0: URIRef(''), nothing@0: DC['description'], nothing@0: Literal("This is a base ontology for the Audio Features engineering process collected from literature") nothing@0: )) nothing@0: nothing@0: graph.add(( nothing@0: VS['term_status'], nothing@0: RDF.type, nothing@0: OWL.AnnotationProperty nothing@0: )) nothing@0: nothing@0: i = 0 nothing@0: nothing@0: order = [ nothing@0: "Zero Crossing Rate", nothing@0: "Linear Predictive Coding", nothing@0: "Mel-scale Frequency Cepstral Coefficients", nothing@0: "Auditory Filter Bank Temporal Envelopes", nothing@0: "Rate-scale-frequency Features", nothing@0: "Phase Space Features" nothing@0: ] nothing@0: nothing@0: domains = { nothing@0: "Zero Crossing Rate": 'temporal', nothing@0: "Linear Predictive Coding": 'frequency', nothing@0: "Mel-scale Frequency Cepstral Coefficients": 'cepstral', nothing@0: "Auditory Filter Bank Temporal Envelopes": 'modulation frequency', nothing@0: "Rate-scale-frequency Features": 'eigendomain', nothing@0: "Phase Space Features": 'phase space' nothing@0: } nothing@0: nothing@0: abbr = { nothing@0: "Zero Crossing Rate": "ZCR", nothing@0: "Mel-scale Frequency Cepstral Coefficients": "MFCC", nothing@0: "Linear Predictive Coding": "LPC", nothing@0: "Linear Prediction Cepstral Coefficients": "LPCC", nothing@0: "Zero crossing peak amplitudes": "ZCPA", nothing@0: "Line spectral frequencies": "LSF", nothing@0: "Short-time energy": "STE", nothing@0: "Amplitude descriptor": "AD", nothing@0: "Adaptive time frequency transform": "ATFT", nothing@0: "Daubechies Wavelet coefficient histogram": "DWCH", nothing@0: "Spectral Flux": "SF", nothing@0: "Group delay function": "GDF", nothing@0: "Modified group delay function": "MGDF", nothing@0: "Spectral centroid": "SC", nothing@0: "Subband spectral flux": "SSF", nothing@0: "Perceptual linear prediction": "PLP" nothing@0: } nothing@0: nothing@0: appdom = { nothing@0: 'ASR': "Speech Recognition", nothing@0: 'ESR': "Environmental Sound Recognition", nothing@0: 'MIR': "Music Information Retrieval", nothing@0: 'AS': "Audio Segmentation", nothing@0: 'FP': "Fingerprinting", nothing@0: 'VAR': "Several", nothing@0: 'EXC': "" nothing@0: } nothing@0: nothing@0: domain = "" nothing@0: domainIndex = 0 nothing@0: compdict = {} nothing@0: nothing@0: graph.add(( nothing@0: URIRef(local + 'MathematicalOperation'), nothing@0: RDF.type, nothing@0: OWL.Class nothing@0: )) nothing@0: nothing@0: graph.add(( nothing@0: URIRef(local + 'Filter'), nothing@0: RDF.type, nothing@0: OWL.Class nothing@0: )) nothing@0: graph.add(( nothing@0: URIRef(local + 'Filter'), nothing@0: RDFS.subClassOf, nothing@0: URIRef(local + 'MathematicalOperation') nothing@0: )) nothing@0: nothing@0: graph.add(( nothing@0: URIRef(local + 'Transformation'), nothing@0: RDF.type, nothing@0: OWL.Class nothing@0: )) nothing@0: graph.add(( nothing@0: URIRef(local + 'Transformation'), nothing@0: RDFS.subClassOf, nothing@0: URIRef(local + 'MathematicalOperation') nothing@0: )) nothing@0: graph.add(( nothing@0: URIRef(local + 'Aggregation'), nothing@0: RDF.type, nothing@0: OWL.Class nothing@0: )) nothing@0: graph.add(( nothing@0: URIRef(local + 'Aggregation'), nothing@0: RDFS.subClassOf, nothing@0: URIRef(local + 'MathematicalOperation') nothing@0: )) nothing@0: nothing@0: for filename in ['filters', 'trans', 'aggr']: nothing@0: compsuper = filename.replace('filters', 'Filter').replace('trans', 'Transformation').replace('aggr', 'Aggregation') nothing@0: for line in [line.strip() for line in open(basedir + 'pdfextract/' + filename + '.txt')]: nothing@0: compname = line[2:] nothing@0: compidref = URIRef(local + compname.replace(' ', '').replace('(', '').replace(')', '').replace('-', '').replace(',', '')) nothing@0: graph.add(( nothing@0: compidref, nothing@0: RDF.type, nothing@0: OWL.Class nothing@0: )) nothing@0: graph.add(( nothing@0: compidref, nothing@0: RDFS.subClassOf, nothing@0: URIRef(local + compsuper) nothing@0: )) nothing@0: graph.add(( nothing@0: compidref, nothing@0: RDFS.label, nothing@0: Literal(compname) nothing@0: )) nothing@0: compdict[line[0]] = compidref nothing@0: nothing@0: graph.add(( nothing@0: URIRef(local + 'Signal'), nothing@0: RDF.type, nothing@0: OWL.Class nothing@0: )) nothing@0: nothing@0: graph.add(( nothing@0: URIRef(local + 'Feature'), nothing@0: RDF.type, nothing@0: OWL.Class nothing@0: )) nothing@0: nothing@0: graph.add(( nothing@0: URIRef(local + 'Feature'), nothing@0: OWL.subClassOf, nothing@0: URIRef(local + 'Signal'), nothing@0: )) nothing@0: nothing@0: for dom in domains.values(): nothing@0: idref = URIRef(local + dom.capitalize().replace(' ', '') + 'Feature') nothing@0: graph.add(( nothing@0: idref, nothing@0: RDF.type, nothing@0: OWL.Class nothing@0: )) nothing@0: graph.add(( nothing@0: idref, nothing@0: RDFS.subClassOf, nothing@0: URIRef(local + 'Feature') nothing@0: )) nothing@0: nothing@0: graph.add(( nothing@0: URIRef(local + 'PerceptualFeature'), nothing@0: RDF.type, nothing@0: OWL.Class nothing@0: )) nothing@0: graph.add(( nothing@0: URIRef(local + 'PerceptualFeature'), nothing@0: RDFS.subClassOf, nothing@0: URIRef(local + 'Feature') nothing@0: )) nothing@0: nothing@0: graph.add(( nothing@0: URIRef(local + 'FrequencyDomainPerceptualFeature'), nothing@0: RDF.type, nothing@0: OWL.Class nothing@0: )) nothing@0: graph.add(( nothing@0: URIRef(local + 'FrequencyDomainPerceptualFeature'), nothing@0: RDFS.subClassOf, nothing@0: URIRef(local + 'FrequencyFeature') nothing@0: )) nothing@0: graph.add(( nothing@0: URIRef(local + 'FrequencyDomainPerceptualFeature'), nothing@0: OWL.equivalentClass, nothing@0: URIRef(local + 'PerceptualFeature') nothing@0: )) nothing@0: nothing@0: graph.add(( nothing@0: URIRef(local + 'FrequencyDomainPhysicalFeature'), nothing@0: RDF.type, nothing@0: OWL.Class nothing@0: )) nothing@0: graph.add(( nothing@0: URIRef(local + 'FrequencyDomainPhysicalFeature'), nothing@0: RDFS.subClassOf, nothing@0: URIRef(local + 'FrequencyFeature') nothing@0: )) nothing@0: graph.add(( nothing@0: URIRef(local + 'FrequencyDomainPhysicalFeature'), nothing@0: OWL.equivalentClass, nothing@0: URIRef(local + 'PhysicalFeature') nothing@0: )) nothing@0: nothing@0: nothing@0: nothing@0: graph.add(( nothing@0: URIRef(local + 'PhysicalFeature'), nothing@0: RDF.type, nothing@0: OWL.Class nothing@0: )) nothing@0: graph.add(( nothing@0: URIRef(local + 'PhysicalFeature'), nothing@0: RDFS.subClassOf, nothing@0: URIRef(local + 'Feature') nothing@0: )) nothing@0: nothing@0: graph.add(( nothing@0: URIRef(local + 'ParametrizedDimensions'), nothing@0: RDF.type, nothing@0: OWL.Class nothing@0: )) nothing@0: nothing@0: graph.add(( nothing@0: URIRef(local + 'ComputationalComplexity'), nothing@0: RDF.type, nothing@0: OWL.Class nothing@0: )) nothing@0: graph.add(( nothing@0: URIRef(local + 'LowComplexity'), nothing@0: RDF.type, nothing@0: OWL.Class nothing@0: )) nothing@0: graph.add(( nothing@0: URIRef(local + 'LowComplexity'), nothing@0: RDFS.subClassOf, nothing@0: URIRef(local + 'ComputationalComplexity') nothing@0: )) nothing@0: graph.add(( nothing@0: URIRef(local + 'MediumComplexity'), nothing@0: RDF.type, nothing@0: OWL.Class nothing@0: )) nothing@0: graph.add(( nothing@0: URIRef(local + 'MediumComplexity'), nothing@0: RDFS.subClassOf, nothing@0: URIRef(local + 'ComputationalComplexity') nothing@0: )) nothing@0: graph.add(( nothing@0: URIRef(local + 'HighComplexity'), nothing@0: RDF.type, nothing@0: OWL.Class nothing@0: )) nothing@0: graph.add(( nothing@0: URIRef(local + 'HighComplexity'), nothing@0: RDFS.subClassOf, nothing@0: URIRef(local + 'ComputationalComplexity') nothing@0: )) nothing@0: nothing@0: graph.add(( nothing@0: URIRef(local + 'TemporalScale'), nothing@0: RDF.type, nothing@0: OWL.Class nothing@0: )) nothing@0: graph.add(( nothing@0: URIRef(local + 'IntraFrame'), nothing@0: RDF.type, nothing@0: OWL.Class nothing@0: )) nothing@0: graph.add(( nothing@0: URIRef(local + 'IntraFrame'), nothing@0: RDFS.subClassOf, nothing@0: URIRef(local + 'TemporalScale') nothing@0: )) nothing@0: graph.add(( nothing@0: URIRef(local + 'InterFrame'), nothing@0: RDF.type, nothing@0: OWL.Class nothing@0: )) nothing@0: graph.add(( nothing@0: URIRef(local + 'InterFrame'), nothing@0: RDFS.subClassOf, nothing@0: URIRef(local + 'TemporalScale') nothing@0: )) nothing@0: graph.add(( nothing@0: URIRef(local + 'Global'), nothing@0: RDF.type, nothing@0: OWL.Class nothing@0: )) nothing@0: graph.add(( nothing@0: URIRef(local + 'Global'), nothing@0: RDFS.subClassOf, nothing@0: URIRef(local + 'TemporalScale') nothing@0: )) nothing@0: nothing@0: nothing@0: graph.add(( nothing@0: URIRef(local + 'ApplicationDomain'), nothing@0: RDF.type, nothing@0: OWL.Class nothing@0: )) nothing@0: nothing@0: for key in appdom.keys(): nothing@0: if appdom[key] != "": nothing@0: idref = URIRef(local + appdom[key].replace(" ", "")) nothing@0: graph.add(( nothing@0: idref, nothing@0: URIRef(RDF.type), nothing@0: OWL.Class nothing@0: )) nothing@0: graph.add(( nothing@0: idref, nothing@0: RDFS.subClassOf, nothing@0: URIRef(local + 'ApplicationDomain') nothing@0: )) nothing@0: nothing@0: #properties nothing@0: graph.add(( nothing@0: URIRef(local + "application_domain"), nothing@0: RDF.type, nothing@0: RDF.Property nothing@0: )) nothing@0: graph.add(( nothing@0: URIRef(local + "application_domain"), nothing@0: RDFS.range, nothing@0: URIRef(local + 'ApplicationDomain') nothing@0: )) nothing@0: graph.add(( nothing@0: URIRef(local + "application_domain"), nothing@0: VS['term_status'], nothing@0: Literal("testing") nothing@0: )) nothing@0: graph.add(( nothing@0: URIRef(local + "application_domain"), nothing@0: RDFS.comment, nothing@0: Literal("application domain property") nothing@0: )) nothing@0: nothing@0: nothing@0: nothing@0: graph.add(( nothing@0: URIRef(local + "semantic_interpretation"), nothing@0: RDF.type, nothing@0: RDF.Property nothing@0: )) nothing@0: graph.add(( nothing@0: URIRef(local + "semantic_interpretation"), nothing@0: VS['term_status'], nothing@0: Literal("testing") nothing@0: )) nothing@0: nothing@0: graph.add(( nothing@0: URIRef(local + "computational_complexity"), nothing@0: RDF.type, nothing@0: RDF.Property nothing@0: )) nothing@0: graph.add(( nothing@0: URIRef(local + "computational_complexity"), nothing@0: VS['term_status'], nothing@0: Literal("testing") nothing@0: )) nothing@0: nothing@0: graph.add(( nothing@0: URIRef(local + "computational_complexity"), nothing@0: RDFS.range, nothing@0: URIRef(local + 'ComputationalComplexity') nothing@0: )) nothing@0: nothing@0: graph.add(( nothing@0: URIRef(local + "psychoacoustic_model"), nothing@0: RDF.type, nothing@0: RDF.Property nothing@0: )) nothing@0: graph.add(( nothing@0: URIRef(local + "psychoacoustic_model"), nothing@0: RDFS.range, nothing@0: XSD.Boolean nothing@0: )) nothing@0: graph.add(( nothing@0: URIRef(local + "psychoacoustic_model"), nothing@0: VS['term_status'], nothing@0: Literal("testing") nothing@0: )) nothing@0: nothing@0: nothing@0: graph.add(( nothing@0: URIRef(local + "dimensions"), nothing@0: RDF.type, nothing@0: RDF.Property nothing@0: )) nothing@0: graph.add(( nothing@0: URIRef(local + "dimensions"), nothing@0: RDFS.range, nothing@0: XSD.Integer nothing@0: )) nothing@0: graph.add(( nothing@0: URIRef(local + "dimensions"), nothing@0: RDFS.range, nothing@0: URIRef(local + 'ParametrizedDimensions') nothing@0: )) nothing@0: nothing@0: graph.add(( nothing@0: URIRef(local + "temporal_scale"), nothing@0: RDF.type, nothing@0: RDF.Property nothing@0: )) nothing@0: graph.add(( nothing@0: URIRef(local + "temporal_scale"), nothing@0: RDFS.range, nothing@0: URIRef(local + 'TemporalScale') nothing@0: )) nothing@0: nothing@0: for name in names: nothing@0: id = local + (name.replace(' ','').replace('-','')) nothing@0: nothing@0: if name == order[domainIndex]: nothing@0: domain = domains[order[domainIndex]] nothing@0: domainIndex += 1 nothing@0: nothing@0: graph.add(( URIRef(id), nothing@0: URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), nothing@0: OWL.Class nothing@0: )) nothing@0: nothing@0: graph.add(( nothing@0: URIRef(id), nothing@0: VS['term_status'], nothing@0: Literal("testing") nothing@0: )) nothing@0: nothing@0: if domain == "frequency": nothing@0: if word[1] == 'Y': nothing@0: temp = URIRef(local + 'FrequencyDomainPerceptualFeature') nothing@0: else: nothing@0: temp = URIRef(local + 'FrequencyDomainPhysicalFeature') nothing@0: nothing@0: graph.add(( nothing@0: URIRef(id), nothing@0: RDFS.subClassOf, nothing@0: URIRef(temp) nothing@0: )) nothing@0: nothing@0: else: nothing@0: graph.add(( nothing@0: URIRef(id), nothing@0: RDFS.subClassOf, nothing@0: URIRef(local + domain.capitalize().replace(' ', '') + 'Feature') nothing@0: )) nothing@0: nothing@0: graph.add(( nothing@0: URIRef(id), nothing@0: #URIRef(local + 'feature'), nothing@0: RDFS.label, nothing@0: Literal(name.replace(' ','').replace('-','')) nothing@0: )) nothing@0: nothing@0: graph.add(( nothing@0: URIRef(id), nothing@0: RDFS.comment, nothing@0: Literal(name + " feature") nothing@0: )) nothing@0: nothing@0: graph.add(( nothing@0: URIRef(id), nothing@0: RDFS.label, nothing@0: Literal(name) nothing@0: )) nothing@0: nothing@0: word = cat[i].split(' ') nothing@0: nothing@0: temp = { nothing@0: 'I': URIRef(local+'IntraFrame'), nothing@0: 'X': URIRef(local+'InterFrame'), nothing@0: 'G': URIRef(local+'Global') nothing@0: }[word[0]] nothing@0: nothing@0: graph.add(( nothing@0: URIRef(id), nothing@0: URIRef(local + 'temporal_scale'), nothing@0: temp nothing@0: )) nothing@0: nothing@0: nothing@0: if word[1] == 'Y': nothing@0: temp = URIRef(local + 'PerceptualFeature') nothing@0: else: nothing@0: temp = URIRef(local + 'PhysicalFeature') nothing@0: nothing@0: graph.add(( nothing@0: URIRef(id), nothing@0: URIRef(local + "semantic_interpretation"), nothing@0: temp nothing@0: )) nothing@0: nothing@0: if word[2] == 'Y': nothing@0: graph.add(( nothing@0: URIRef(id), nothing@0: URIRef(local + "psychoacoustic_model"), nothing@0: Literal(True) nothing@0: )) nothing@0: else: nothing@0: graph.add(( nothing@0: URIRef(id), nothing@0: URIRef(local + "psychoacoustic_model"), nothing@0: Literal(False) nothing@0: )) nothing@0: nothing@0: temp = { nothing@0: 'L': URIRef(local + 'LowComplexity'), nothing@0: 'M': URIRef(local + 'MediumComplexity'), nothing@0: 'H': URIRef(local + 'HighComplexity') nothing@0: }[word[3]] nothing@0: nothing@0: graph.add(( nothing@0: URIRef(id), nothing@0: URIRef(local + "computational_complexity"), nothing@0: temp nothing@0: )) nothing@0: nothing@0: if word[4] == 'V': nothing@0: temp = URIRef(local + 'ParametrizedDimensions') nothing@0: else: nothing@0: temp = Literal(int(word[4])) nothing@0: nothing@0: graph.add(( nothing@0: URIRef(id), nothing@0: URIRef(local + 'dimensions'), nothing@0: temp nothing@0: )) nothing@0: nothing@0: temp = appdom[word[5]] nothing@0: nothing@0: if temp != '': nothing@0: graph.add(( nothing@0: URIRef(id), nothing@0: URIRef(local + "application_domain"), nothing@0: URIRef(local + temp.replace(" ", "")) nothing@0: )) nothing@0: nothing@0: steps = sig[i].split(' ') nothing@0: nothing@0: for key in steps: nothing@0: graph.add(( nothing@0: URIRef(id), nothing@0: URIRef(local + 'computation'), nothing@0: compdict[key] nothing@0: )) nothing@0: nothing@0: if name.find('MPEG-7') >= 0: nothing@0: graph.add(( nothing@0: URIRef(id), nothing@0: URIRef(local + 'computedIn'), nothing@0: Literal('MPEG-7') nothing@0: )) nothing@0: #graph.add(( nothing@0: # URIRef(local+name.replace('MPEG-7', '').lower().lstrip().replace(' ', '_')+'_feature'), nothing@0: # RDF.type, nothing@0: # URIRef(id) nothing@0: #)) nothing@0: nothing@0: if name in abbr.keys(): nothing@0: graph.add(( nothing@0: URIRef(id), nothing@0: URIRef(local + 'abbreviation'), nothing@0: Literal(abbr[name]) nothing@0: )) nothing@0: nothing@0: nothing@0: i += 1 nothing@0: nothing@0: nothing@0: nothing@0: graph.serialize('/Users/alo/MusicOntology/features/baseOnto.n3', format='n3') nothing@0: graph.serialize('/Users/alo/MusicOntology/features/baseOnto.rdf')