Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 词性标记中的字符串索引超出范围_Python_String_Nltk_Pos Tagger - Fatal编程技术网

Python 词性标记中的字符串索引超出范围

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

我正在用python中的nltk包做词性标记。现在它显示错误字符串索引超出范围,即使我的字符串不大

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)