nothing@1: import rdflib nothing@1: from rdflib import Graph, RDF, RDFS, plugin, URIRef, Literal, OWL nothing@1: nothing@1: abbr = { nothing@1: "Zero Crossing Rate": "ZCR", nothing@1: "Mel-scale Frequency Cepstral Coefficients": "MFCC", nothing@1: "Linear Predictive Coding": "LPC", nothing@1: "Linear Prediction Cepstral Coefficients": "LPCC", nothing@1: "Zero Crossing Peak Amplitudes": "ZCPA", nothing@1: "Line Spectral Frequencies": "LSF", nothing@1: "Short-Time Energy": "STE", nothing@1: "Amplitude Descriptor": "AD", nothing@1: "Adaptive Time Frequency Transform": "ATFT", nothing@1: "Daubechies Wavelet Coefficient Histogram": "DWCH", nothing@1: "Spectral Flux": "SF", nothing@1: "Group Delay Function": "GDF", nothing@1: "Modified Group Delay Function": "MGDF", nothing@1: "Spectral Centroid": "SC", nothing@1: "Subband Spectral Flux": "SSF", nothing@1: "Perceptual Linear Prediction": "PLP", nothing@1: "Linear Spectral Pairs": "LSP", nothing@1: "Average Magnitude Difference Function": "AMDF", nothing@1: "Octave Band Signal Intensity": "OBSI", nothing@1: "Root Mean Square": "RMS", nothing@1: "Harmonic Pitch Class Profile": "HPCP" nothing@1: } nothing@1: nothing@1: synonyms = { nothing@1: "Mel-scale Frequency Cepstral Coefficients": ["Mel Frequency Cepstral Coefficients", "Mel-Frequency Cepstral Coefficients", "Coefficients", "Mfcc"], nothing@1: "Spectral Kurtosis": ["Kurtosis", "Spectral kurtosis"], nothing@1: "Spectral Rolloff": ["Rolloff", "Spectral Rolloff Point"], nothing@1: "Zero Crossing Rate": ["Zero Crossing", "Zcr", "Zero Crossings"], nothing@1: "Spectral Skewness": ["Skewness", "Spectral skewness"], nothing@1: "Spectral Flux": ["Flux"], nothing@1: "Spectral Centroid": ["Centroid", "Spectral centroid"], nothing@1: "Spectral Slope": ["Spectral slope"], nothing@1: "Spectral Flatness": ["Spectral Flatness Measure", "Flatness"], nothing@1: "Harmonic Spectrum": ["Harmonic spectrum"], nothing@1: "Average Magnitude Difference Function": ["Amdf"], nothing@1: "AutoCorrelation": ["Autocorrelation"], nothing@1: "PeakSpectrum": ["Peak spectrum"], nothing@1: "Spectral Spread": ["Spread"], nothing@1: "Spectral Crest": ["Spectral Crest Measure"], nothing@1: "Onset Detection Function": ["Onset", "Onsets"], nothing@1: "Root Mean Square": ["Rms"] nothing@1: } nothing@1: nothing@1: execfile('/Users/alo/Development/python-Levenshtein-0.10.2/StringMatcher.py') nothing@1: nothing@1: def checkSynonyms( name ): nothing@1: rtn = "" nothing@1: for key, syns in synonyms.items(): nothing@1: for item in syns: nothing@1: if name.replace(' ', '').replace('-', '') == item.replace(' ', '').replace('-', ''): nothing@1: rtn = key.replace(' ', '').replace('-', '') nothing@1: break nothing@1: return rtn nothing@1: nothing@1: def checkAbbreviations( name ): nothing@1: rtn = "" nothing@1: for key, ab in abbr.items(): nothing@1: if name.replace(' ', '').replace('-', '').lower() == ab.replace(' ', '').replace('-', '').lower(): nothing@1: rtn = key.replace(' ', '').replace('-', '') nothing@1: break nothing@1: return rtn nothing@1: nothing@1: nothing@1: def loadBase( graph, path ): nothing@1: graph.parse(path) nothing@1: for su, pr in graph.subject_predicates(OWL.Class): nothing@1: graph.add((su, RDFS.subClassOf, URIRef(ns+'AudioFeature'))) nothing@1: nothing@1: def addBaseTriples( graph, ns ): nothing@1: graph.add(( nothing@1: URIRef(ns+'Signal'), nothing@1: RDF.type, nothing@1: OWL.Class nothing@1: )) nothing@1: nothing@1: graph.add(( nothing@1: URIRef(ns+'Feature'), nothing@1: RDF.type, nothing@1: OWL.Class nothing@1: )) nothing@1: nothing@1: graph.add(( nothing@1: URIRef(ns+'AudioFeature'), nothing@1: RDFS.subClassOf, nothing@1: URIRef(ns+'Signal') nothing@1: )) nothing@1: nothing@1: nothing@1: def addTriplesFromFile( graph, path, ns ): nothing@1: loc = Graph() nothing@1: loc.parse(path) nothing@1: nothing@1: for su in loc.subjects(RDF.type, RDFS.Resource): nothing@1: name = su.split('/')[-1] nothing@1: nothing@1: ids = "" nothing@1: nothing@1: ids = checkSynonyms(name) nothing@1: nothing@1: if ids == "": nothing@1: ids = checkAbbreviations(name) nothing@1: nothing@1: if ids == "": nothing@1: ids = name.replace(' ','').replace('-','') nothing@1: nothing@1: graph.add(( nothing@1: URIRef(ns + ids), nothing@1: RDF.type, nothing@1: OWL.Class nothing@1: )) nothing@1: graph.add(( nothing@1: URIRef(ns + ids), nothing@1: RDFS.subClassOf, nothing@1: URIRef(ns+'AudioFeature') nothing@1: )) nothing@1: for pr, ob in loc.predicate_objects(su): nothing@1: if ob != RDFS.Resource: nothing@1: graph.add(( URIRef(ns + ids), pr, ob )) nothing@1: nothing@1: graph.add(( URIRef(ns + ids), URIRef(ns+'computedIn'), Literal(path.split('/')[-1][3:-4]) )) nothing@1: nothing@1: nothing@1: def compareForSimilarities( graph, ns, threshold=0.75 ): nothing@1: for s, p in graph.subject_predicates(OWL.Class): nothing@1: for ss, pp in graph.subject_predicates(OWL.Class): nothing@1: it = s.split('/')[-1] nothing@1: other = ss.split('/')[-1] nothing@1: if s != ss: nothing@1: m = StringMatcher() nothing@1: m.set_seqs(it, other) nothing@1: score = float(m.distance()) / ((len(it) + len(other)) / 2.0) nothing@1: if score < (1 - threshold): nothing@1: graph.add((s, URIRef(ns + 'similarTo'), ss)) nothing@1: #graph.add((s, URIRef(ns + 'similarity'), Literal(1.0-score))) nothing@1: