我在python中使用nltk包进行POS标记。现在它显示错误字符串索引超出范围,尽管我的字符串不是很大。
import nltk
sample_list = ['', 'emma', 'jane', 'austen', '1816', '', 'volume', 'chapter', 'emma', 'woodhouse', ' ','handsome', ' ', 'clever', ' ', 'rich', ' ', 'comfortable', 'home', 'happy', 'disposition', ' ','seemed', 'unite', 'best','blessings', 'existence', '', 'lived','nearly', 'twenty-one', 'years','world', 'little', 'distress', 'vex', '', 'youngest','two']
tagged = nltk.pos_tag(sample_list)
错误的屏幕截图
您的问题是空字符串,即''
,因此您可以使用:
tagged = nltk.pos_tag([i for i in sample_list if i])
您的输入包含空的"单词",例如列表中的第一个项目。尝试过滤如下:
clean_sample_list = [word for word in sample_list if
word]
tagged = nltk.pos_tag(clean_sample_list)