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@18: "Harmonic Pitch Class Profile": "HPCP", nothing@18: "SignalToNoiseRatio": "SNR" 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@18: "Average Squared Difference Function": ["Asdf"], nothing@1: "AutoCorrelation": ["Autocorrelation"], nothing@1: "PeakSpectrum": ["Peak spectrum"], nothing@1: "Spectral Spread": ["Spread"], nothing@1: "Spectral Crest": ["Spectral Crest Measure"], nothing@18: "Onset Detection Function": ["Onset", "Onsets", "Onset Detector"], nothing@18: "Root Mean Square": ["Rms"], nothing@18: "Note Tracker": ["Aubio Note Tracker"], nothing@18: "Pitch": ["Aubio Pitch Detector"], nothing@18: "Silence Test": ["Aubio Silence Detector"], nothing@18: "AutoCorrelationFFT": ["Autocorrelationfft"], nothing@18: "Average Deviation": ["Average deviation"], nothing@18: "Bark Coefficients": ["Bark coefficients"], nothing@18: "Beat Spectrum": ["Beat Spectra"], nothing@18: "Beat Tracker": ["Beat Tracking", "BeatTracking" "BeatTrack", "Beat Track", "BeatTrack2", "Beats", "Aubio Beat Tracker"], nothing@18: "Complex Domain Onset Detection": ["Complex Domain Method Onset Detection Function"], nothing@18: "Discrete Cosine Transform": ["Dct"], nothing@18: "Funcdamental Frequency": ["F0", "Failsafef0"], nothing@18: "HighestValue": ["Highest value"], nothing@18: "Harmonic Product Spectrum": ["Hps"], nothing@18: "Key Detector": ["KeyTrack"], nothing@18: "KrumhanslKeyFinder": ["Krumhansl_key_finder"], nothing@18: "L-Norm": ["Lnorm"], nothing@18: "Lowest Value": ["Lowest value"], nothing@18: "MELODIAMelodyExtraction": ["MELODIAMelodyExtractionintermediatesteps"], nothing@18: "MIDI Note": ["MIDI"], nothing@18: "Note Onset Detector": ["Note Onsets", "Note Onset"], nothing@18: "Octave Band Signal Intensity Ratio": ["OBSIR"], nothing@18: "OddToEvenHarmonicRatio": ["Oddtoevenharmonicratio"], nothing@18: "OddEvenRatio": ["Oddevenratio"], nothing@18: "Pitch Countours": ["PitchContours:All", "PitchContours:Melody"], nothing@18: "PitchFFTYIN": ["PitchFftYin"], nothing@18: "PitchYIN": ["PitchYin", "Yin", "AubioYin"], nothing@18: "Rhythm Patterns": ["RhythmPattern"], nothing@18: "RMSAmplitude": ["Rmsamplitude"], nothing@18: "Spectral Shape": ["Spectral Shape Descriptors"], nothing@18: "Spectral Variance": ["Spectral Variation", "Spectralvariance"], nothing@18: "Spectral Average Deviation": ["Spectralaveragedeviation"], nothing@18: "Spectral Inharmonicity": ["Spectralinharmonicity"], nothing@18: "Spectral Mean": ["Spectralmean"], nothing@18: "Spectral Standard Deviation": ["Spectralstandarddeviation"], nothing@18: "Standard Deviation": ["Standarddeviation"], nothing@18: "Tristimulus1": ["TristimulusI"], nothing@18: "Tristimulus2": ["TristimulusII"], nothing@18: "Tristimulus3": ["TristimulusIII"], nothing@18: "Even Harmonic Ratio": ["evenHarmonicRatio"] 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@4: if name.replace(' ', '').replace('-', '').lower() == item.replace(' ', '').replace('-', '').lower(): 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: