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