4

我正在使用 python NLTK 对 Twitter 数据进行情感分析。我需要一本字典,其中包含单词的 +ve 和 -ve 极性。我已经阅读了很多关于 sentiwordnet 的内容,但是当我将它用于我的项目时,它并没有提供高效和快速的结果。我想我没有正确使用它。谁能告诉我正确的使用方法?以下是我到目前为止所做的步骤:

  1. 推文的标记化
  2. 代币的 POS 标记
  3. 将每个标签传递给哨兵

我正在使用 nltk 包进行标记化和标记。请参阅下面我的部分代码:

import nltk
from nltk.stem import *
from nltk.corpus import sentiwordnet as swn

tokens=nltk.word_tokenize(row) #for tokenization, row is line of a file in which tweets are saved.
tagged=nltk.pos_tag(tokens) #for POSTagging

for i in range(0,len(tagged)):
     if 'NN' in tagged[i][1] and len(swn.senti_synsets(tagged[i][0],'n'))>0:
            pscore+=(list(swn.senti_synsets(tagged[i][0],'n'))[0]).pos_score() #positive score of a word
            nscore+=(list(swn.senti_synsets(tagged[i][0],'n'))[0]).neg_score()  #negative score of a word
    elif 'VB' in tagged[i][1] and len(swn.senti_synsets(tagged[i][0],'v'))>0:
           pscore+=(list(swn.senti_synsets(tagged[i][0],'v'))[0]).pos_score()
           nscore+=(list(swn.senti_synsets(tagged[i][0],'v'))[0]).neg_score()
    elif 'JJ' in tagged[i][1] and len(swn.senti_synsets(tagged[i][0],'a'))>0:
           pscore+=(list(swn.senti_synsets(tagged[i][0],'a'))[0]).pos_score()
           nscore+=(list(swn.senti_synsets(tagged[i][0],'a'))[0]).neg_score()
    elif 'RB' in tagged[i][1] and len(swn.senti_synsets(tagged[i][0],'r'))>0:
           pscore+=(list(swn.senti_synsets(tagged[i][0],'r'))[0]).pos_score()
           nscore+=(list(swn.senti_synsets(tagged[i][0],'r'))[0]).neg_score()

最后,我将计算有多少条推文是正面的,有多少条推文是负面的。我哪里错了?我应该如何使用它?还有其他类似的易于使用的字典吗?

4

2 回答 2

4

是的,您还可以使用其他词典。你可以在这里找到一小部分词典:http ://sentiment.christopherpotts.net/lexicons.html#resources 刘冰的意见词典似乎很容易使用。

除了链接到那些词典之外,该网站还是一个非常好的情感分析教程。

于 2015-12-23T11:28:45.503 回答
0

计算情绪

alist = [all_tokens_in_doc]

totalScore = 0

count_words_included = 0

for word in all_words_in_comment:

    synset_forms = list(swn.senti_synsets(word[0], word[1]))

    if not synset_forms:

        continue

    synset = synset_forms[0] 

    totalScore = totalScore + synset.pos_score() - synset.neg_score()

    count_words_included = count_words_included +1

final_dec = ''

if count_words_included == 0:

    final_dec = 'N/A'

elif totalScore == 0:

    final_dec = 'Neu'        

elif totalScore/count_words_included < 0:

    final_dec = 'Neg'

elif totalScore/count_words_included > 0:

    final_dec = 'Pos'

return final_dec
于 2018-07-31T10:31:58.900 回答