【发布时间】:2014-03-09 09:51:24
【问题描述】:
我正在尝试进行情绪分析。我已经设法通过 nltk 使用朴素贝叶斯来分类负面和正面推文的语料库。但是我不想在每次运行这个程序时都经历运行这个分类器的过程,所以我尝试使用 pickle 来保存,然后将分类器加载到不同的脚本中。但是,当我尝试运行脚本时,它返回错误 NameError: name classifier is not defined,虽然我认为它是通过 def load_classifier() 定义的:
我有atm的代码如下:
import nltk, pickle
from nltk.corpus import stopwords
customstopwords = ['']
p = open('xxx', 'r')
postxt = p.readlines()
n = open('xxx', 'r')
negtxt = n.readlines()
neglist = []
poslist = []
for i in range(0,len(negtxt)):
neglist.append('negative')
for i in range(0,len(postxt)):
poslist.append('positive')
postagged = zip(postxt, poslist)
negtagged = zip(negtxt, neglist)
taggedtweets = postagged + negtagged
tweets = []
for (word, sentiment) in taggedtweets:
word_filter = [i.lower() for i in word.split()]
tweets.append((word_filter, sentiment))
def getwords(tweets):
allwords = []
for (words, sentiment) in tweets:
allwords.extend(words)
return allwords
def getwordfeatures(listoftweets):
wordfreq = nltk.FreqDist(listoftweets)
words = wordfreq.keys()
return words
wordlist = [i for i in getwordfeatures(getwords(tweets)) if not i in stopwords.words('english')]
wordlist = [i for i in getwordfeatures(getwords(tweets)) if not i in customstopwords]
def feature_extractor(doc):
docwords = set(doc)
features = {}
for i in wordlist:
features['contains(%s)' % i] = (i in docwords)
return features
training_set = nltk.classify.apply_features(feature_extractor, tweets)
def load_classifier():
f = open('my_classifier.pickle', 'rb')
classifier = pickle.load(f)
f.close
return classifier
while True:
input = raw_input('I hate this film')
if input == 'exit':
break
elif input == 'informfeatures':
print classifier.show_most_informative_features(n=30)
continue
else:
input = input.lower()
input = input.split()
print '\nSentiment is ' + classifier.classify(feature_extractor(input)) + ' in that sentence.\n'
p.close()
n.close()
任何帮助都会很棒,脚本似乎在返回错误之前打印 '\nSentiment is ' + classifier.classify(feature_extractor(input)) + ' in that sentence.\n'"...
【问题讨论】:
标签: python classification pickle sentiment-analysis