POS 标记中的字符串索引超出范围

string index out of range in POS tagging

我正在 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)

screenshot of the error

你的问题是空字符串,即 '' 所以你可以使用:

tagged = nltk.pos_tag([i for i in sample_list if i])

您输入的内容为空 'words',例如列表中的第一项。尝试过滤是这样的:

clean_sample_list = [word for word in sample_list if 
word]
tagged = nltk.pos_tag(clean_sample_list)