Words may have multiple candidates in ISLE; returns the 'optimal' one.

def _findBestPronunciation(isleWordList, aPron):
    '''
    Words may have multiple candidates in ISLE; returns the 'optimal' one.
    '''
    
    aP = _prepPronunciation(aPron)  # Mapping to simplified phone inventory
    
    numDiffList = []
    withStress = []
    i = 0
    alignedSyllabificationList = []
    alignedActualPronunciationList = []
    for wordTuple in isleWordList:
        aPronMap = copy.deepcopy(aPron)
        syllableList = wordTuple[0]  # syllableList, stressList
        
        iP = [phone for phoneList in syllableList for phone in phoneList]
        iP = _prepPronunciation(iP)

        alignedIP, alignedAP = alignPronunciations(iP, aP)
        
        # Remapping to actual phones
#         alignedAP = [origPronDict.get(phon, u"''") for phon in alignedAP]
        alignedAP = [aPronMap.pop(0) if phon != u"''" else u"''"
                     for phon in alignedAP]
        alignedActualPronunciationList.append(alignedAP)
        
        # Adjusting the syllabification for differences between the dictionary
        # pronunciation and the actual pronunciation
        alignedSyllabification = _adjustSyllabification(alignedIP,
                                                        syllableList)
        alignedSyllabificationList.append(alignedSyllabification)
        
        # Count the number of misalignments between the two
        numDiff = alignedIP.count(u"''") + alignedAP.count(u"''")
        numDiffList.append(numDiff)
        
        # Is there stress in this word
        hasStress = False
        for syllable in syllableList:
            for phone in syllable:
                hasStress = u"ˈ" in phone or hasStress
        
        if hasStress:
            withStress.append(i)
        i += 1
    
    # Return the pronunciation that had the fewest differences
    #     to the actual pronunciation
    minDiff = min(numDiffList)
    
    # When there are multiple candidates that have the minimum number
    #     of differences, prefer one that has stress in it
    bestIndex = None
    bestIsStressed = None
    for i, numDiff in enumerate(numDiffList):
        if numDiff != minDiff:
            continue
        if bestIndex is None:
            bestIndex = i
            bestIsStressed = i in withStress
        else:
            if not bestIsStressed and i in withStress:
                bestIndex = i
                bestIsStressed = True
    
    return (isleWordList, alignedActualPronunciationList,
            alignedSyllabificationList, bestIndex)