def merge_conll_mmax(conll_wlist, mmax_wlist): """Align elements from conll_wlist and mmax_wlist. conll_wlist's are lists of 3-tuples in which first element is conll word, second and third elements are sentence and word id of this word, respectively. mmax_list is a list of two tuples, in which first element is MMAX word and second element is MMAX id of this word. This method determines to which CONLL word given MMAX corresponds and returns an augmented CONLL list whose tuples contain one additional 4-th element which is itself a list of MMAX word id's corresponding to given CONLL word. @param conll_wlist - list of 3-tuples with CONLL word, its sentence and word id @param mmax_wlist - list of 2-tuples with MMAX word and its MMAX id @return list of three elements tuples, in which first element will be sentence id of CONLL word, second element will be word's id in that sentence, and the third element will be a list of MMAX word ids corresponding to given CONLL word """ conll_words = [e[0] for e in conll_wlist] mmax_words = [e[0] for e in mmax_wlist] s_id = w_id = 0 mw_id_list = [] # aligned_words will be a list of length len(conll_words) in which every # element will also be a list of the indices of mmax_words which correspond # to CONLL word at given position aligned_words = nw_align(conll_words, mmax_words, substitute=_cmp_words) # join CONLL and MMAX indices ret = [] for i, mwlist in enumerate(aligned_words): # append to `ret` a three-tuple with sentence and word indices of # CONLL word s_id, w_id = conll_wlist[i][1:] mw_id_list = [mmax_wlist[mw_id][1] for mw_id in mwlist] ret.append((s_id, w_id, mw_id_list)) return ret
return performance # similarity measure used by Needleman-Wunsch algorithm def note_similarity(score_note, performance_note): # at the moment we just give a 1 if the pitch matches, 0.5 if it's # within a tone and 0 if more # over time this can be tweaked to include velocity, duration, etc if score_note[0] == performance_note[1]: return 1 elif abs(score_note[0] - performance_note[1]) < 3: return 0.5 else: return 0 if __name__ == "__main__": score = load_score("../examples/scores/hanon_21_rh.txt") performance = load_performance("../examples/recordings/hanon_21_rh.txt") # align score and performance using above similarity function and a penalty # of -1 for insertions and deletions @@@ might need a lot of tweaking for i in nw_align(score, performance, note_similarity, -1, -1): print i