我需要从.txt文件中识别复杂的单词。我试图使用nltk但不存在这样的模块。复杂单词是文本中包含两个以上音节的单词。
我会用Pyphen。该模块有一个用于连字符的Pyphen
类。它的一个方法positions()
返回一个单词中可以拆分的位置数:
>>> from pyphen import Pyphen
>>> p = Pyphen(lang='en_US')
>>> p.positions('exclamation')
[2, 5, 7]
如果单词“exclamation”可以在三个地方分割,它有四个音节,所以你只需要过滤掉多个分割位置的所有单词。
. . .
但我注意到你把它标记为[t:nltk]问题。我自己对NLTK没有经验,但@Jules建议的the question在这方面提出了一个很好的建议:使用cmudict
模块。它为您提供了美式英语单词的发音列表:
>>> from nltk.corpus import cmudict
>>> d = cmudict.dict()
>>> pronounciations = d['exasperation']
>>> pronounciations
[['EH2', 'K', 'S', 'AE2', 'S', 'P', 'ER0', 'EY1', 'SH', 'AH0', 'N']]
幸运的是,我们的第一个单词只有一个发音。它表示为一个字符串列表,每个字符串代表一个音素:
>>> phonemes = pronounciations[0]
>>> phonemes
['EH2', 'K', 'S', 'AE2', 'S', 'P', 'ER0', 'EY1', 'SH', 'AH0', 'N']
请注意vowel phonemes have a number at the end, indicating stress:
元音标记为压力(1 =主要,2 =次要,0 =无压力)。例如:NATURAL 1 N AE1 CH ER0 AH0 L.
所以,我们只需要计算最后带数字的音素数:
>>> vowels = [ph for ph in phonemes if ph[-1].isdigit()]
>>> vowels
['EH2', 'AE2', 'ER0', 'EY1', 'AH0']
>>> len(vowels)
5
. . .
不确定哪个是最好的选择,但我想你可以从这里解决你的问题。