2

I'm doing sentiment analysis using naive bayes classifier of nltk. I'm just inserting a csv file that contains words and their labels as training set and not testing it yet. I'm finding sentiment of each sentence and then finding average of sentiments of all sentences in the end. My file contains words in the format:

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

But the file doesn't get trained and the above mentioned error shows up. Here's my python code:

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)

error:

    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)

2 Answers2

0

if am not wrong train() takes list of tuple and you are providing file obj.

Instead of this

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

Try this

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]

and pass a_list_of_tuple variable to train()

hop this will help :)

DexJ
  • 1,264
  • 13
  • 24
  • File "C:\ProgramData\Anaconda3\lib\site-packages\nltk\classify\naivebayes.py", line 196, in train for fname, fval in featureset.items(): AttributeError: 'str' object has no attribute 'items' – RIYA CHAUDHARY B15EE029 Apr 16 '17 at 13:40
  • you are getting error because train() expecting data structure like list[tuple(dict,str)] And we are here passing list[tuple(str,str)] which is according to your data correct data and pass it to train and it will work fine. :) – DexJ Apr 17 '17 at 13:32
0

From the doc:

def train(cls, labeled_featuresets, estimator=ELEProbDist):
    """
    :param labeled_featuresets: A list of classified featuresets,
        i.e., a list of tuples ``(featureset, label)``.
    """

So you can write something similar:

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