python 类调用不存在的方法_找不到Python方法,但在类中定义
我正在自学(可能是我的第一个错误)课程和方法,把情绪分析脚本转换为使用它们。
我想我已经准备好了所有的方法,但是我一直
global name 'get_bigram_word_feats' is not defined
我敢肯定我也会因为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 sclassifier
class 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() - starttime
print donetime
python 类调用不存在的方法_找不到Python方法,但在类中定义相关推荐
- python 怎么调用 矩阵 第几行_第58集 python机器学习:混淆矩阵精度指标
混淆矩阵的精度计算公式为:精度=(TP+TN)/(TP+TN+FP+FN),也就是说,精度就是指正确的预测数目除以所有样本的数量. 准确率.召回率与f-分数:总结混淆矩阵还有几种方法,其中最常见的就是 ...
- python 类的内置方法_【转】[python] 类常用的内置方法
原文:http://xukaizijian.blog.163.com/blog/static/170433119201111894228877/ 内置方法 说明 __init__(self,...) ...
- python中import cv2遇到的错误及安装方法_独家利用OpenCV,Python和Ubidots来构建行人计数器程序(附代码amp;解析)...
作者:Jose Garcia 翻译:吴振东 校对:张一豪 本文约4000字,建议阅读14分钟. 本文将利用OpenCV,Python和Ubidots来编写一个行人计数器程序,并对代码进行了较为详细的讲 ...
- python中字符串类型的encode()方法_第五章 Python字符串常用方法详解
5.1 Python字符串拼接(包含字符串拼接数字) 在 Python中拼接(连接)字符串很简单,可以直接将两个字符串紧挨着写在一起,具体格式为: strname = "str1" ...
- python语言有几种编程方式_零基础自学python语言,有哪几种方法?龟叔说:这套教程带你入门...
第一我要说的是:你应该先了解python语言的起源,这句话对于自学python的小白来说,本身就是耍流氓. 我们从根源上分析这个问题,这个问题是(从零基础如何自学python编程)? 我的提取关键字: ...
- python写对象提供的方法_下面哪些是python读对象提供的“写”方法?
[简答题]土的物理性指标中六个换算指标是哪些? [判断题]定义毛坯中,常用的形状有多边体.长方体.圆柱体. [单选题]_______ difficult questions they are! I c ...
- python如何调用c++numpy.ndarray代码_从C++到Numpy ndarray的转换非常简单
我使用Boost Python来计算程序的计算密集型部分,它工作得很好,除了将数组从C++传递到Python,反之亦然也很慢,这是程序整体效率的限制因素.在 这里有一个例子来说明我的观点.在C++侧, ...
- python的模块提供了许多文件处理方法_详解使用Python处理文件目录的相关方法
所有文件都包含在各个不同的目录下,不过Python也能轻松处理.os模块有许多方法能帮你创建,删除和更改目录. mkdir()方法 可以使用os模块的mkdir()方法在当前目录下创建新的目录们.你需 ...
- python使用复合语句def创建函数对象_【收藏】Python实用技巧-成为Pythoner必经之路...
前言 本文主要记录 Python 中一些常用技巧,所描述的是告诉你怎么写才是更好? 如果你并不熟悉Python语法,希望你能在下面代码片段中看到Python的简单.优雅; 如果你象我这样,对 Pyt ...
- python写入excel表格数据绘制图表_(原创)xlsxwriter,python excel 写入数据\图表等操作_图表操作(二)...
前面分享了使用xlsxwriter创建excel和写入数据GUC:(原创)xlsxwriter,python excel 写入数据\图表等操作_创建.写入(一)zhuanlan.zhihu.com ...
最新文章
- (初学必看)deep graph library(dgl)库的入门引导
- php curl基本操作
- C# button重绘
- python多线程下载编程_python多线程下载图片
- pytorch图片数据归一化,通常传入transforms.Normalize(mean,std,inplace=False)中的mean和std是如何获取的?
- 雷达的工作原理示意图_平板吊袋离心机工作原理及结构示意图
- 汉王考勤管理系统使用教程
- 论文中的统计学如何计算机,统计学教学中如何利用Excel软件及其利弊分析论文...
- 前端三级联动 distpicker插件
- 2.4G天线在PCB板上的设计资料
- 130292015015-傅志彬-第1章作业
- 第四代测序技术(纳米孔测序技术)
- 正则表达式-re.error: unbalanced parenthesis at position 7
- 给力文:网络营销就像娶美女做老婆(转)
- 记一次Redis哨兵机制【主机挂了无法切换其他从机】的问题
- 【职场心路】一个老DBA的自白
- 【高级开发进阶】6.1 现代计算机模型基础
- 2010年知名大企业工资大曝光
- 按计算机的用途分类可将电子计算机分为,第一章 计算机基础知识
- 【软考系统架构设计师】2015年下系统架构师论文写作历年真题