Beispiel #1
0
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)
def tree_per_word(word, cur_rhythm, tree_init, syllables, poses):
    def get_list(rhythm):
        return tree_init[rhythm]

    assert cur_rhythm in rhythm_map
    current_rhythm_list = get_list(cur_rhythm)

    if cur_rhythm == 'ph':
        pass

    elif cur_rhythm == 'syl':
        smaller_rhythm = 'ph'
        syllable = syllables.pop(0)
        for phones in syllable:
            tree_per_word(phones, smaller_rhythm, tree_init, syllables, poses)

    elif cur_rhythm == '#0':
        smaller_rhythm = 'syl'
        for syllable in syllables[0:len(word)]:
            tree_per_word(''.join(syllable), smaller_rhythm, tree_init,
                          syllables, poses)

    elif cur_rhythm == '#1':
        smaller_rhythm = '#0'
        tree_per_word(word, smaller_rhythm, tree_init, syllables, poses)

    elif cur_rhythm == '#3':
        smaller_rhythm = '#1'
        tree_per_word(word, smaller_rhythm, tree_init, syllables, poses)

    elif cur_rhythm == '#4':
        smaller_rhythm = '#3'
        tree_per_word(word, smaller_rhythm, tree_init, syllables, poses)

    else:
        raise ValueError('rhythm {} is not correct'.format(cur_rhythm))

    # 创建节点
    if cur_rhythm == 'ph':
        newNode = LabNode(txt=word,
                          index=len(current_rhythm_list) + 1,
                          rhythm=cur_rhythm)

    else:
        smaller_rhythm_list = get_list(smaller_rhythm)
        newNode = LabNode(sons=smaller_rhythm_list,
                          txt=word,
                          index=len(current_rhythm_list) + 1,
                          rhythm=cur_rhythm)
        if len(smaller_rhythm_list) > 0:
            tree_init['assist'][smaller_rhythm] = smaller_rhythm_list[-1]
        else:
            tree_init['assist'][smaller_rhythm] = []
        # 清空此节点下的子节点list,以便新节点重建子节点list
        tree_init[smaller_rhythm] = []

    # 设置词性
    if cur_rhythm == '#0':
        newNode.pos = poses.pop(0)

    # 设置左右brother
    if len(current_rhythm_list) > 0:
        newNode.lbrother = current_rhythm_list[-1]
        current_rhythm_list[-1].rbrother = newNode
    elif tree_init['assist'][cur_rhythm]:
        # 设置非同树枝下的树叶连成一块
        newNode.lbrother = tree_init['assist'][cur_rhythm]
        tree_init['assist'][cur_rhythm].rbrother = newNode

    # 加入新节点到current_rhythm_list
    current_rhythm_list.append(newNode)