Performs naive alignment for words in a textgrid
    
    Naive alignment gives each segment equal duration.
    Phone duration is determined by the duration of the word
    and the number of phones.
    
    removeOverlappingSegments - remove any labeled words or phones that
                                fall under labeled utterances

def naivePhoneAlignment(tg, wordTierName, phoneTierName, isleDict,
                        removeOverlappingSegments=False):
    '''
    Performs naive alignment for words in a textgrid
    
    Naive alignment gives each segment equal duration.
    Phone duration is determined by the duration of the word
    and the number of phones.
    
    removeOverlappingSegments - remove any labeled words or phones that
                                fall under labeled utterances
    '''
    wordTier = tg.tierDict[wordTierName]
    
    phoneTier = None
    if phoneTierName in tg.tierNameList:
        phoneTier = tg.tierDict[phoneTierName]
    
    # Load in the phone tier, if it exists:
    phoneEntryList = []
    if phoneTier is not None:
        if removeOverlappingSegments:
            for startT, stopT, _ in wordTier.entryList:
                phoneTier = phoneTier.eraseRegion(startT, stopT,
                                                  'truncate', False)
        phoneEntryList = phoneTier.entryList

    # Do the naive alignment
    for wordStartT, wordEndT, word in wordTier.entryList:
        
        # Get the list of phones in this word
        try:
            firstSyllableList = isleDict.lookup(word)[0][0][0]
        except isletool.WordNotInISLE:
            continue

        phoneList = [phone for syllable in firstSyllableList
                     for phone in syllable]
        for char in [u'ˈ', u'ˌ']:
            phoneList = [phone.replace(char, '') for phone in phoneList]
        
        # Get the naive alignment for phones, if alignment doesn't
        # already exist for phones
        subPhoneEntryList = []
        if phoneTier is not None:
            subPhoneEntryList = phoneTier.crop(wordStartT, wordEndT,
                                               "truncated", False).entryList
        
        if len(subPhoneEntryList) == 0:
            phoneDur = (wordEndT - wordStartT) / len(phoneList)
            
            phoneStartT = wordStartT
            for phone in phoneList:
                phoneEndT = phoneStartT + phoneDur
                subPhoneEntryList.append((phoneStartT, phoneEndT, phone))
                phoneStartT = phoneEndT

        phoneEntryList.extend(subPhoneEntryList)
    
    # Replace or add the phone tier
    newPhoneTier = tgio.IntervalTier(phoneTierName,
                                     phoneEntryList,
                                     tg.minTimestamp,
                                     tg.maxTimestamp)
    if phoneTier is not None:
        tg.replaceTier(phoneTierName, newPhoneTier)
    else:

        tg.addTier(newPhoneTier)
    
    return tg