【问题标题】:Loading a classifier using Pickle?使用 Pickle 加载分类器?
【发布时间】: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


    【解决方案1】:

    好吧,您已经声明并定义load_classifier() 方法,但从未调用它并分配使用它的变量。这意味着,当执行到达print '\nSentiment is... ' 行时,没有变量名classifier。自然,执行会引发异常。

    在 while 循环之前添加行 classifier = load_classifier()。 (没有任何缩进)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-03
      • 2020-05-15
      • 1970-01-01
      • 2019-01-13
      • 2022-08-10
      • 2020-07-14
      • 2019-03-25
      相关资源
      最近更新 更多