Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ValueError:要解压缩的值太多(应为2个)_Python - Fatal编程技术网

Python ValueError:要解压缩的值太多(应为2个)

Python ValueError:要解压缩的值太多(应为2个),python,Python,我正在使用nltk的朴素贝叶斯分类器进行情绪分析。我只是插入一个csv文件,其中包含单词及其标签作为训练集,但还没有测试它。我找到每个句子的情绪,然后找到所有句子的平均情绪。我的文件包含以下格式的文字: good,0.6 amazing,0.95 great,0.8 awesome,0.95 love,0.7 like,0.5 better,0.4 beautiful,0.6 bad,-0.6 worst,-0.9 hate,-0.8 sad,-0.4 disappointing,-0.6 an

我正在使用nltk的朴素贝叶斯分类器进行情绪分析。我只是插入一个csv文件,其中包含单词及其标签作为训练集,但还没有测试它。我找到每个句子的情绪,然后找到所有句子的平均情绪。我的文件包含以下格式的文字:

good,0.6
amazing,0.95
great,0.8
awesome,0.95
love,0.7
like,0.5
better,0.4
beautiful,0.6
bad,-0.6
worst,-0.9
hate,-0.8
sad,-0.4
disappointing,-0.6
angry,-0.7
happy,0.7
但是文件没有经过训练,出现了上面提到的错误。以下是我的python代码:

import nltk.classify.util
from nltk.classify import NaiveBayesClassifier
from nltk.corpus import stopwords
from nltk.tokenize import sent_tokenize
from nltk.classify.api import ClassifierI

operators=set(('not','never','no'))
stop_words=set(stopwords.words("english"))-operators

text="this restaurant is good but i hate it ."
sent=0.0
x=0
text2=""
xyz=[]
dot=0

if "but" in text:
    i=text.find("but")
    text=text[:i]+"."+text[i+3:]
if "whereas" in text:
    i=text.find("whereas")
    text=text[:i]+"."+text[i+7:]
if "while" in text:
    i=text.find("while")
    text=text[:i]+"."+text[i+5:]

a=open('C:/Users/User/train_words.csv','r')

for w in text.split():
    if w in stop_words:
        continue
    else:
        text2=text2+" "+w

print (text2)

cl=nltk.NaiveBayesClassifier.train(a)

xyz=sent_tokenize(text2)

print(xyz)

for s in xyz:
    x=x+1
    print(s)
    if "not" in s or "n't" in s:
        print(float(cl.classify(s))*-1)
        sent=sent+(float(cl.classify(s))*-1)
    else:
        print(cl.classify(s))
        sent=sent+float(cl.classify(s))
print("sentiment of the overall document:",sent/x)
错误:

    runfile('C:/Users/User/Documents/untitled1.py', wdir='C:/Users  /User/Documents')
 restaurant good . hate .
Traceback (most recent call last):

  File "<ipython-input-8-d03fac6844c7>", line 1, in <module>
    runfile('C:/Users/User/Documents/untitled1.py', wdir='C:/Users/User/Documents')

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
    execfile(filename, namespace)

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/User/Documents/untitled1.py", line 37, in <module>
    cl = nltk.NaiveBayesClassifier.train(a)

  File "C:\ProgramData\Anaconda3\lib\site-packages\nltk\classify\naivebayes.py", line 194, in train
    for featureset, label in labeled_featuresets:

ValueError: too many values to unpack (expected 2)
runfile('C:/Users/User/Documents/untitled1.py',wdir='C:/Users/User/Documents')
餐厅很好。憎恨
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
runfile('C:/Users/User/Documents/untitled1.py',wdir='C:/Users/User/Documents')
文件“C:\ProgramData\Anaconda3\lib\site packages\spyder\utils\site\site customize.py”,第866行,在runfile中
execfile(文件名、命名空间)
文件“C:\ProgramData\Anaconda3\lib\site packages\spyder\utils\site\sitecustomize.py”,第102行,在execfile中
exec(编译(f.read(),文件名,'exec'),命名空间)
文件“C:/Users/User/Documents/untitled1.py”,第37行,在
cl=nltk.NaiveBayesClassifier.train(a)
文件“C:\ProgramData\Anaconda3\lib\site packages\nltk\classify\naivebayes.py”,第194行,在列中
对于featureset,在标记的\u featureset中添加标签:
ValueError:要解压缩的值太多(应为2个)
如果我没弄错,train()将获取元组列表,您将提供文件obj

而不是这个

a = open('C:/Users/User/train_words.csv','r') 
试试这个

a = open('C:/Users/User/train_words.csv','r').read()   # this is string
a_list = a.split('\n')
a_list_of_tuple = [tuple(x.split(',')) for x in a_list]
并将_tuple变量的_list_传递给train()

这将有助于:)

从文档:

def train(cls, labeled_featuresets, estimator=ELEProbDist):
    """
    :param labeled_featuresets: A list of classified featuresets,
        i.e., a list of tuples ``(featureset, label)``.
    """
所以你可以写一些类似的东西:

feature_set = [line.split(',')[::-1] for line in open('filename').readline()]

你能发布完整的stacktrace吗?不,stacktrace。完整错误输出。请编辑您的答案并添加此信息,以便我们更好地诊断问题。哦,对不起。OK这可能是因为您正在将文件对象传递给
.train()
,但它需要一个第一个元素
可哈希的元组。如果有帮助,您可以参考此答案:ValueError:没有足够的值来解包(预期为2,得到1)文件“C:\ProgramData\Anaconda3\lib\site packages\nltk\classify\naivebayes.py”,第196行,在fname的训练中,featureset.items()中的fval:AttributeError:'str'对象没有属性'items',您遇到错误,因为训练()需要类似list[tuple(dict,str)]的数据结构,我们在这里传递list[tuple(str,str)],它根据您的数据更正数据并将其传递给训练,它将正常工作。:)