def rhyme(inp, level):
entries = nltk.corpus.cmudict.entries()
syllables = [(word, syl) for word, syl in entries if word == inp]
rhymes = []
for (word, syllable) in syllables:
rhymes += [word for word, pron in entries if pron[-level:] == syllable[-level:]]
return set(rhymes)
def doTheyRhyme(word1, word2):
# first, we don't want to report 'glue' and 'unglue' as rhyming words
# those kind of rhymes are LAME
if word1.find(word2) == len(word1) - len(word2):
return False
if word2.find(word1) == len(word2) - len(word1):
return False
return word1 in rhyme(word2, 1)
使用soundex或双变音找出它们是否押韵。NLTK似乎没有实现这些功能,但是Google的快速搜索显示了一些实现。
^{} library 对此做得很好。无黑客攻击,加载速度快,基于CMU发音字典,所以很可靠。
https://pypi.python.org/pypi/pronouncing
从他们的documentation:
Here我找到了一种使用NLTK查找给定单词押韵的方法:
其中
inp
是一个单词,level
意味着押韵应该有多好。因此,您可以使用此函数来检查两个单词是否押韵,您可以只检查其中一个单词是否在另一个单词的允许押韵集中:
相关问题 更多 >
编程相关推荐