在python的情感分析中用什么数据结构来存储相应单词的情绪计数?

时间:2013-12-18 04:25:16

标签: python twitter sentiment-analysis

我们正在python的twitter情绪分析器上做一个项目。为了提高系统的效率,在训练期间我们希望将特定单词的出现存储在正,负和中性推文中。最后,我们将把这个词的情绪作为最大发生的词。哪种数据结构适合动态存储单词及其情感(正面,负面和中性)? 例如:

            positive  negative   neutral
 market       45        12         2
 quite        35         67        5
 good         98         2         7

我们需要动态地向结构添加单词。

1 个答案:

答案 0 :(得分:2)

这样的事情可能会对你有所帮助:

sentiment_words = {}  # this will be a dict of 3-member lists, with word as key

for word in words:
    if not word in sentiment_words:  # initialize the word if it's not present yet
        sentiment_words[word] = [0, 0, 0]
    if ispositive(word):  # increment the right sentiment item in the list
        sentiment_words[word][0] += 1
    elif isnegative(word):
        sentiment_words[word][1] += 1
    elif isneutral(word):
        sentiment_words[word][2] += 1

如果您可以详细说明具体细节,我可能会为您调整一下。

相关问题