【发布时间】:2017-02-26 21:33:59
【问题描述】:
我需要对荷兰语推文列表进行情绪分析,我正在使用conll2002 来做同样的事情。这是我正在使用的代码:
import nltk.classify.util
from nltk.classify import NaiveBayesClassifier
from nltk.corpus import conll2002
import time
t=time.time()
def word_feats(words):
return dict([(word, True) for word in words])
#negids = conll2002.fileids('neg')
def train():
#negids = conll2002.fileids('neg')
#posids = conll2002.fileids('pos')
negids = conll2002.fileids()
posids = conll2002.fileids()
negfeats = [(word_feats(conll2002.words(fileids=[f])), 'neg') for f in negids]
posfeats = [(word_feats(conll2002.words(fileids=[f])), 'pos') for f in posids]
negcutoff = len(negfeats)*3/4
poscutoff = len(posfeats)*3/4
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)
classifier.show_most_informative_features()
x=train()
print x
print time.time()-t
上述代码有效,但输出如下:
train on 8 instances, test on 4 instances
accuracy: 0.5
Most Informative Features
poderlas = True pos : neg = 1.0 : 1.0
voert = True pos : neg = 1.0 : 1.0
contundencia = True pos : neg = 1.0 : 1.0
encuestocracia = None pos : neg = 1.0 : 1.0
alivien = None pos : neg = 1.0 : 1.0
Bogotá = True pos : neg = 1.0 : 1.0
Especialidades = True pos : neg = 1.0 : 1.0
hoofdredacteurs = True pos : neg = 1.0 : 1.0
quisieron = True pos : neg = 1.0 : 1.0
asciendan = None pos : neg = 1.0 : 1.0
None
9.21083234
在所有情况下,pos:neg 的比率都是 1:1。我该如何解决?我认为问题可能出在我目前在代码中注释掉的以下语句中:
negids = conll2002.fileids('neg')
posids = conll2002.fileids('pos')
如果我不注释掉以上两个语句,我得到的错误是:
Traceback (most recent call last):
File "naive1.py", line 31, in <module>
x=train()
File "naive1.py", line 13, in train
negids = conll2002.fileids('neg')
TypeError: fileids() takes exactly 1 argument (2 given)
我尝试使用 self 来解决此问题,但仍然无法正常工作。有人可以指出我正确的方向吗?提前致谢。
【问题讨论】:
标签: python twitter nltk sentiment-analysis corpus