Python 词性标记中的字符串索引超出范围
我正在用python中的nltk包做词性标记。现在它显示错误字符串索引超出范围,即使我的字符串不大Python 词性标记中的字符串索引超出范围,python,string,nltk,pos-tagger,Python,String,Nltk,Pos Tagger,我正在用python中的nltk包做词性标记。现在它显示错误字符串索引超出范围,即使我的字符串不大 import nltk sample_list = ['', 'emma', 'jane', 'austen', '1816', '', 'volume', 'chapter', 'emma', 'woodhouse', ' ','handsome', ' ', 'clever', ' ', 'rich', ' ', 'comfortable', 'home', 'happy', 'dispos
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)