def tree(words, rhythms, syllables, poses, phs_type): assert len(words) == len(rhythms) assert len(words) == len(poses) assert len(''.join(words)) / 3 == len(syllables) tree_init = {'assist': {}} for key, value in rhythm_map.items(): tree_init[value] = [] tree_init['assist'][value] = None # print tree_init syllables_copy = copy.deepcopy(syllables) poses_copy = copy.deepcopy(poses) for word, rhythm in zip(words, rhythms): tree_per_word(word, rhythm, tree_init, syllables_copy, poses_copy) newLab = LabNode(sons=tree_init[rhythm_map['#4']], index=1, rhythm='#5') newLab.adjust() # print tree_init['rhythm4'] # show([newLab],0) # show(tree_init['rhythm4'],0) def get_first(): return tree_init['rhythm4'][0].sons[0].sons[0].sons[0].sons[0].sons[0] def adjust(): fphone = get_first() if phs_type[0] == 's': newLab = LabNode(txt='sil', rhythm='ph') newLab.rbrother = fphone fphone.lbrother = newLab fphone = newLab phone = fphone for ptype in phs_type[1:-1]: assert phone is not None # print (phone.txt,ptype), if ptype in ['s', 'd']: if ptype == 's': newLab = LabNode(txt='pau', rhythm='ph') else: newLab = LabNode(txt='sp', rhythm='ph') newLab.rbrother = phone.rbrother newLab.lbrother = phone phone.rbrother = newLab newLab.rbrother.lbrother = newLab phone = newLab else: phone = phone.rbrother assert phone is not None assert phone.rbrother is None if phs_type[-1] == 's': phone.rbrother = LabNode(txt='sil', rhythm='ph') phone.rbrother.lbrother = phone return fphone return adjust()
def tree_per_word(word, rhythm, tree_init, syllables, poses): def get_list(rhythm): return tree_init[rhythm_map[rhythm]] # print get_list('#4') assert rhythm in rhythm_map rhythm_list = get_list(rhythm) if rhythm == 'ph': pass elif rhythm == 'syl': pre_rhythm = 'ph' for phones in syllables[0]: tree_per_word(phones, pre_rhythm, tree_init, syllables, poses) del syllables[0] elif rhythm == '#0': pre_rhythm = 'syl' for syllable in syllables[0:len(word) / 3]: tree_per_word(''.join(syllable), pre_rhythm, tree_init, syllables, poses) elif rhythm in ['#1', '#2']: pre_rhythm = '#0' tree_per_word(word, pre_rhythm, tree_init, syllables, poses) elif rhythm == '#3': pre_rhythm = '#1' tree_per_word(word, pre_rhythm, tree_init, syllables, poses) elif rhythm == '#4': pre_rhythm = '#3' tree_per_word(word, pre_rhythm, tree_init, syllables, poses) else: print 'error rhythm input' exit(-1) if rhythm == 'ph': newLab = LabNode(txt=word, index=len(rhythm_list) + 1, rhythm=rhythm) else: newLab = LabNode(sons=get_list(pre_rhythm), txt=word, index=len(rhythm_list) + 1, rhythm=rhythm) tree_init['assist'][rhythm_map[pre_rhythm]] = get_list(pre_rhythm)[-1] tree_init[rhythm_map[pre_rhythm]] = [] newLab.adjust() # newLab.txt=word if rhythm == '#0': newLab.pos = poses[0][0] del poses[0] if len(rhythm_list) != 0: newLab.lbrother = rhythm_list[-1] rhythm_list[-1].rbrother = newLab elif tree_init['assist'][rhythm_map[rhythm]]: newLab.lbrother = tree_init['assist'][rhythm_map[rhythm]] tree_init['assist'][rhythm_map[rhythm]].rbrother = newLab rhythm_list.append(newLab)