TextBlob朴素贝叶斯文本分类

时间:2016-04-11 22:19:20

标签: python text-classification textblob

我试图在python中使用TextBlob在推文上实现朴素贝叶斯分类器。我已经能够训练数据集,并且可以使用以下方法成功地对单个推文进行分类:

print cl.classify("text")

现在我想打开一个csv文件并对该文件中的所有推文进行分类。有关如何实现这一目标的任何建议?我的代码如下:

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'对象没有属性'分类'

1 个答案:

答案 0 :(得分:0)

你也需要先训练(不清楚你是否做过这个?),

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)