utils.py 文件源码

python
阅读 31 收藏 0 点赞 0 评论 0

项目:LinguisticAnalysis 作者: DucAnhPhi 项目源码 文件源码
def get_word_syllables(word, pronouncingDict):
    # returns a list of transcriptions for a word
    # (a word may have alternative pronunciations)
    # eg. 'orange':
    # -> [['AO1', 'R', 'AH0', 'N', 'JH'], ['A01', 'R', 'IH0', 'N', 'JH']]
    # vowels are marked with numbers from 0-2
    # by counting the vowels we can get the number of syllables
    try:
        # last element is more common
        pronList = pronouncingDict[word.lower()][-1]
        sylCount = len([ syl for syl in pronList if syl[-1].isdecimal() ])
        return sylCount
    except KeyError:
        # scrape syllable count
        url = 'http://www.syllablecount.com/syllables/' + word
        request = urllib.request.urlopen(url)
        response = request.read().decode('utf-8')
        # use regex to match desired value
        sylCount = int(
            re.search("(?<=<b style='color: #008000'>)[0-9]+", response)[0]
        )
        return sylCount;
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号