未找到Python方法,但在类中定义

未找到Python方法,但在类中定义,第1张

概述我通过转换情绪分析脚本来使用它们来教我自己(可能是我的第一个错误)类和方法.我以为我已经掌握了所有方法,但我一直在努力未定义全局名称“get_bigram_word_feats”我确定我也会收到get_word_feats的错误,如果它到那么远的话.我正在撞击这个伟大的时间.我尝试删除staticmethod并添加self.我究竟做错了什么?这是我的代码:d

我通过转换情绪分析脚本来使用它们来教我自己(可能是我的第一个错误)类和方法.

我以为我已经掌握了所有方法,但我一直在努力

未定义全局名称“get_bigram_word_feats”

我确定我也会收到get_word_feats的错误,如果它到那么远的话.

我正在撞击这个伟大的时间.我尝试删除staticmethod并添加self.我究竟做错了什么?

这是我的代码:

def word_feats(words):    return dict([(word,True) for word in words])class SentClassifIEr:    def __init__(self,name,location):        self.name = name        self.location = location        self.fullpath = location + "/" + name    def doesexist(self):        return os.path.isfile(self.fullpath)    def save_classifIEr(self):        rf = open(self.fullpath,'wb')        pickle.dump(self.fullpath,rf)        rf.close()    def load_classifIEr(self):        sf = open(self.fullpath,'rb')        sclassifIEr = pickle.load(sf)        sf.close()        return sclassifIErclass Training:    def __init__(self,neg,pos):        self.neg = neg        self.pos = pos        self.negIDs = open(self.neg,'rb').read().splitlines(True)        self.posIDs = open(self.pos,'rb').read().splitlines(True)        self.exclude = set(string.punctuation)        self.exclude = self.exclude,'...'        self.swords = stopwords.words('english')    def tokens(self,words):        words = [w for w in nltk.word_tokenize(words) if w not in self.exclude and len(w) > 1            and w not in self.swords and wordnet.synsets(w)]        return words    def IDList(self,words):        thisIDList = [self.tokens(tf) for tf in words]        return thisIDList    @staticmethod    def get_word_feats(words):        return dict([(word,True) for word in words])    @staticmethod    def get_bigram_word_feats(twords,score_fn=BigramAssocmeasures.chi_sq,tn=200):        words = [w for w in twords]        bigram_finder = BigramcollocationFinder.from_words(words)        bigrams = bigram_finder.nbest(score_fn,tn)        return dict([(ngram,True) for ngram in itertools.chain(words,bigrams)])    @staticmethod    def label_feats(theList,label):        return [(get_word_feats(lf),label) for lf in theList]    @staticmethod    def label_grams(theList,label):        return [(get_bigram_word_feats(gf),label) for gf in theList()]    @staticmethod    def combinegrams(grams,feats):        for g in grams():            feats.append(g)        return feats    def negIDList(self):        return self.IDList(self.negIDs)    def posIDList(self):        return self.IDList(self.posIDs)    def posgrams(self):        return self.label_grams(self.posIDList,'pos')    def neggrams(self):        return self.label_grams(self.negIDList,'neg')    def negwords(self):        return self.label_feats(self.negIDList,'neg')    def poswords(self):        return self.label_feats(self.posIDList,'pos')    def negfeats(self):        return self.combinegrams(self.neggrams,self.negwords)    def posfeats(self):        return self.combinegrams(self.posgrams,self.poswords)starttime = time.time()myclassifIEr = SentClassifIEr("sentanalyzer.pickle","classifIErs")if myclassifIEr.doesexist() is False:    print "training new classifIEr"    trainset = Training('data/neg.txt','data/pos.txt')    negfeats = trainset.negfeats()    posfeats = trainset.posfeats()    negcutoff = len(negfeats) * 8 / 10    poscutoff = len(posfeats) * 8 / 10    trainfeats = negfeats[:negcutoff] + posfeats[:poscutoff]    testfeats = negfeats[negcutoff:] + posfeats[poscutoff:]    print 'train on %d instances,test on %d instances' % (len(trainfeats),len(testfeats))    classifIEr = NaiveBayesClassifIEr.train(trainfeats)    print 'accuracy:',nltk.classify.util.accuracy(classifIEr,testfeats)    myclassifIEr.save_classifIEr()else:    print "using existing classifIEr"    classifIEr = myclassifIEr.load_classifIEr()classifIEr.show_most_informative_features(20)mystr = "16 steps to an irresistible sales pitch,via @vladblagi: slIDesha.re/1bVV7OS"myfeat = word_feats(nltk.word_tokenize(mystr))print classifIEr.classify(myfeat)probd = classifIEr.prob_classify(myfeat)print probd.prob('neg')print probd.prob('pos')donetime = time.time() - starttimeprint donetime
最佳答案您需要的所有信息都在异常消息中:

global name ‘get_bigram_word_feats’ is not defined

(我的重点)

Python不理解您要从类中访问该方法,因为您没有将类名指定为方法调用的一部分.因此,它正在全局命名空间中查找该函数,但未能找到它.

如果从调用实例方法中回忆起来,则需要在方法前加上self.使Python解释器看起来正确,虽然你没有指定self,但这也适用于静态方法,而是指定类名.

因此,要解决此问题,请使用类名称对方法的调用作为前缀:

return [(Training.get_bigram_word_feats(gf),label) for gf in theList()]         ^---+---^             |             +-- you need this part
总结

以上是内存溢出为你收集整理的未找到Python方法,但在类中定义全部内容,希望文章能够帮你解决未找到Python方法,但在类中定义所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1205174.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存