nltk太多值无法解包(预期2)

时间:2018-11-02 08:51:16

标签: python nltk

import nltk
from nltk.corpus import brown

brown_tagged_sents = brown.tagged_sents(categories='news')
((x[1],y[1],z[0],z[1])
    for sent in brown_tagged_sents
    for x,y,z in nltk.trigrams(sent))
cfd = nltk.ConditionalFreqDist(
    ((x[1],y[1],z[0],z[1])
    for sent in brown_tagged_sents
    for x,y,z in nltk.trigrams(sent))
)

上面的代码将发生:

Traceback (most recent call last):  
  File "/Users/shike/Desktop/textprocess/nltktest.py", line 10, in <module>  
    for sent in brown_tagged_sents  
  File "/usr/local/lib/python3.6/site-packages/nltk/probability.py", line  
 1803, in __init__  
    for (cond, sample) in cond_samples:  
ValueError: too many values to unpack (expected 2)  

列表理解

((x[1],y[1],z[0],z[1])
    for sent in brown_tagged_sents
    for x,y,z in nltk.trigrams(sent))

可以单独工作,但是当它用作参数时会发生错误。我仍然不知道为什么。所有代码都在Python3中进行了测试。

2 个答案:

答案 0 :(得分:1)

初始化器nltk.ConditionalFreqDist()需要两个参数,即condition和sample。因此,(x[1],y[1],z[0],z[1])需要写成((x[1],y[1],z[0]),z[1])

答案 1 :(得分:0)

nltk.ConditionalFreqDist期望两个元素元组(例如(condition, sample))的可迭代。但是,您正在传递一个由4个元素组成的元组(x[1],y[1],z[0],z[1])的迭代。

您可能需要查看以下内容的documentationimplementation 该课程。