我真的很不想发一个有关整个代码块的问题,但是我已经在这上面工作了3个小时,而且我无法理解正在发生的事情。我从CSV文件中检索了大约600条推文,其得分值(介于-2到2之间)反映了对总统候选人的情感。
然而,当我在任何其他数据上运行此训练样本时,只返回一个值(积极的)。我已经检查过得分是否被正确添加,它们确实被正确添加了。对于来自600个不同来源的多元化训练集,85000条推文全部被评为“积极”,这对我来说毫无意义。有人知道这里发生了什么吗?谢谢!
import nltk
import csv
tweets = []
import ast
with open('romney.csv', 'rb') as csvfile:
mycsv = csv.reader(csvfile)
for row in mycsv:
tweet = row[1]
try:
score = ast.literal_eval(row[12])
if score > 0:
print score
print tweet
tweets.append((tweet,"positive"))
elif score < 0:
print score
print tweet
tweets.append((tweet,"negative"))
except ValueError:
tweet = ""
def get_words_in_tweets(tweets):
all_words = []
for (words, sentiment) in tweets:
all_words.extend(words)
return all_words
def get_word_features(wordlist):
wordlist = nltk.FreqDist(wordlist)
word_features = wordlist.keys()
return word_features
def extract_features(document):
document_words = set(document)
features = {}
for word in word_features:
features['contains(%s)' % word] = (word in document_words)
return features
word_features = get_word_features(get_words_in_tweets(tweets))
training_set = nltk.classify.apply_features(extract_features, tweets)
classifier = nltk.NaiveBayesClassifier.train(training_set)
c = 0
with open('usa.csv', "rU") as csvfile:
mycsv = csv.reader(csvfile)
for row in mycsv:
try:
tweet = row[0]
c = c + 1
print classifier.classify(extract_features(tweet.split()))
except IndexError:
tweet = ""
extract_features
函数中的document
参数是什么类型? - Joel Cornettcontains-word(%s)
,而不是contains(%s)
。 - Joel Cornett