有什么办法可以将“pos_tag”的值放入 python nltk 中字典内的列表中吗?

Is there any way to ' pos_tag ' values into a list inside dictionary in python nltk?

我有一个 python 包含值列表的字典。当我尝试 pos_tag 列表中的值时,它显示错误。有什么办法可以解决吗?

RuleSet = {1: ['drafts', 'duly', 'signed', 'beneficiary', 'drawn', 'issuing', 'bank', 'quoting', 'lc', ''], 2: ['date', ''], 3: ['signed', 'commerical', 'invoices', 'quadruplicate', 'gross', 'cifvalue', 'goods', '']}
for key in RuleSet:
    value = RuleSet[key]
    Tagged = nltk.pos_tag(value)
    print(Tagged)

IndexError: string index out of range

您可以使用列表,但其中不能有空项目。查看错误日志:

File "C:\Users\wstribizew\AppData\Local\Programs\Python\Python36-32\lib\site-packages\nltk\tag\perceptron.py", line 240, in normalize
    elif word[0].isdigit():

perceptron.pyelif word[0].isdigit()中没有检查字符串长度,因为通常nltk.pos_tagnltk.word_tokenize之后完成标记字符串时不输出空项。

这是工作片段:

import nltk
RuleSet = {1: ['drafts', 'duly', 'signed', 'beneficiary', 'drawn', 'issuing', 'bank', 'quoting', 'lc', ''], 2: ['date', ''], 3: ['signed', 'commerical', 'invoices', 'quadruplicate', 'gross', 'cifvalue', 'goods', '']}
for key in RuleSet:
    value = list(filter(None, RuleSet[key])) # Get rid of empty items
    Tagged = nltk.pos_tag(value)
    print(Tagged)

输出:

[('drafts', 'NNS'), ('duly', 'RB'), ('signed', 'VBD'), ('beneficiary', 'JJ'), ('drawn', 'NN'), ('issuing', 'VBG'), ('bank', 'NN'), ('quoting', 'VBG'), ('lc', 'NN')]
[('date', 'NN')]
[('signed', 'VBN'), ('commerical', 'JJ'), ('invoices', 'NNS'), ('quadruplicate', 'VBP'), ('gross', 'JJ'), ('cifvalue', 'NN'), ('goods', 'NNS')]