def add_head_middle_tail_silence(root_node, phs_type):
    """Add a 'sil' tree node at the head or middle or tail of tree according to
    the phs_type"""
    fphone = get_first_node_of_tree(root_node)

    if phs_type[0] == 's':
        newNode = LabNode(txt='sil', rhythm='ph')
        newNode.rbrother = fphone
        fphone.lbrother = newNode
        fphone = newNode
    phone = fphone
    assert phone is not None
    for ptype in phs_type[1:-1]:
        if ptype in ['s', 'd']:
            if ptype == 's':
                newNode = LabNode(txt='pau', rhythm='ph')
            else:
                newNode = LabNode(txt='sp', rhythm='ph')
            newNode.rbrother = phone.rbrother
            newNode.lbrother = phone
            phone.rbrother = newNode
            newNode.rbrother.lbrother = newNode
            phone = newNode
        else:
            assert phone is not None
            phone = phone.rbrother
    assert phone is not None
    if phs_type[-1] in ['s', 'd']:
        phone.rbrother = LabNode(txt='sil', rhythm='ph')
        phone.rbrother.lbrother = phone
    return fphone
Ejemplo n.º 2
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
Ejemplo n.º 3
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)