1
votes

I'm trying to implement the Naive Bayes classifier on tweets using TextBlob in python. I have been able to train the dataset and can successfully classify individual tweets using:

print cl.classify("text")

Now I want to open a csv file and classify all the tweets in that file. Any suggestions on how I can achieve this? My code is as below:

import csv
from textblob import TextBlob

with open(test_path, 'rU') as csvfile:
    lineReader = csv.reader(csvfile,delimiter=',',quotechar="\"")
    lineReader = csv.reader(csvfile,delimiter=',')

    test = []
    for row in lineReader:
      blob = (row[0]) 
      blob = TextBlob(blob)
      test.append([blob])

      print (test.classify())

AttributeError: 'list' object has no attribute 'classify'

1
You're calling classify() on list. you should do that to the blob - Vaishak Suresh
Why are there two linereader assignments? - trans1st0r
Thank you for the solution! - Ashwin

1 Answers

0
votes

You need to train first too also (not clear if you have done this?),

train = [] 
# then repeat your above lines, appending each tweet to train set
# but for a separate training set (or slice up the rows)

# do your test append loop -----

# 1. Now train a model
my_classifier = NaiveBayesClassifier(train)

# 2. test given to the model to get accuracy
accu = my_classifier.accuracy(test)