【问题标题】:Sentiment analysis for Dutch tweets using NLTK Corpus conll2002使用 NLTK 语料库 conll2002 对荷兰推文进行情感分析
【发布时间】: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


    【解决方案1】:

    fileids() 方法接受 categories 参数,但仅在分类语料库中。例如:

    >>> from nltk.corpus import brown
    >>> brown.fileids("mystery")
    ['cl01', 'cl02', 'cl03', 'cl04', 'cl05', 'cl06', 'cl07', 'cl08', 'cl09', 
    'cl10', 'cl11', 'cl12', 'cl13', 'cl14', 'cl15', 'cl16', 'cl17', 'cl18', 
    'cl19', 'cl20', 'cl21', 'cl22', 'cl23', 'cl24']
    

    您的调用失败,因为 CONLL 语料库没有类别。这是因为它们没有针对情感进行注释:CONLL 2000 和 CONLL 2002 都是分块语料库(分别为 NP/PP 和命名实体)。

    >>> conll2002.categories()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'ConllChunkCorpusReader' object has no attribute 'categories'
    

    所以对您的问题的简短回答是,您无法在 conll2002 语料库上训练情感分析器。

    【讨论】:

    • 感谢您指出这一点。您能否建议一些其他方法来对荷兰语推文进行情绪分析?我是否应该通过将正面推文和负面推文分类为“好”和“坏”的列表来尝试神经网络,然后基于此训练模型?请提出替代方案。
    • 我不明白你的问题。如果您有荷兰语的情感语料库(正如您所说的那样),您可以使用它来训练NaiveBayesClassifier,如书中所示。如果您没有情感语料库,那么不同的监督算法将如何帮助您?
    • 现在我有一个包含 500 个荷兰语文本陈述的列表,以及基于他们的情绪的相应分数。例如,正面推文的 %age 约为 90,而负面推文的 %age 约为 50。我可以将其用作我的人工神经网络模型的训练数据集并训练它来预测其他推文的情绪得分吗?感谢您的帮助。
    • 试试看哪个分类器可以提供更好的性能。
    猜你喜欢
    • 2017-07-15
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 2022-08-10
    • 1970-01-01
    • 2018-04-07
    • 2017-03-09
    • 2012-07-02
    相关资源
    最近更新 更多