<p>似乎您正在尝试使用TextBlob,但正在培训NLTK NaiveBayesClassifier,正如在其他答案中指出的,它必须通过一个功能字典。</p>
<p>TextBlob有一个默认的特征提取程序,用于指示文档中包含培训集中的哪些单词(如其他答案中所示)。因此,TextBlob允许您按原样传入数据。</p>
<pre><code>from textblob.classifiers import NaiveBayesClassifier
train = [('This is an amazing place!', 'pos'),
('I feel very good about these beers.', 'pos'),
('This is my best work.', 'pos'),
("What an awesome view", 'pos'),
('I do not like this restaurant', 'neg'),
('I am tired of this stuff.', 'neg'),
("I can't deal with this", 'neg'),
('He is my sworn enemy!', 'neg'),
('My boss is horrible.', 'neg') ]
test = [
('The beer was good.', 'pos'),
('I do not enjoy my job', 'neg'),
("I ain't feeling dandy today.", 'neg'),
("I feel amazing!", 'pos'),
('Gary is a friend of mine.', 'pos'),
("I can't believe I'm doing this.", 'neg') ]
classifier = NaiveBayesClassifier(train) # Pass in data as is
# When classifying text, features are extracted automatically
classifier.classify("This is an amazing library!") # => 'pos'
</code></pre>
<p>当然,简单的默认提取器并不适合所有问题。如果您想知道如何提取特征,只需编写一个函数,该函数以文本字符串作为输入,输出特征字典并将其传递给分类器。</p>
<pre><code>classifier = NaiveBayesClassifier(train, feature_extractor=my_extractor_func)
</code></pre>
<p>我建议您在这里查看短TextBlob分类器教程:<a href="http://textblob.readthedocs.org/en/latest/classifiers.html" rel="nofollow noreferrer">http://textblob.readthedocs.org/en/latest/classifiers.html</a></p>