在python-nltk中,有没有办法将“pos\u-tag”值放入字典内的列表中?

2024-10-03 21:24:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个python字典,它包含了一系列的值。当我试图标记列表中的值时,显示错误。有什么办法解决吗

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


Tags: key标记列表字典value错误signed办法
1条回答
网友
1楼 · 发布于 2024-10-03 21:24:58

你可以使用列表,你只是不能有一个空的项目在那里。请参阅错误日志:

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.py中的elif word[0].isdigit()中没有检查字符串长度,因为通常nltk.pos_tag是在nltk.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')]

相关问题 更多 >