【问题标题】:What data structure to use to store the sentiment count of corresponding word during sentiment analysis in python?在python中进行情感分析时,使用什么数据结构来存储相应单词的情感计数?
【发布时间】:2014-01-06 03:02:22
【问题描述】:

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

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

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

【问题讨论】:

  • 请将此问题扩展为有关 Python 的实际编程问题。一种数据结构是字典,其中单词作为键,情感分数作为值。情感分析是在线教程中一个很好的主题。如果您正在寻找现有的软件包,请参阅 TextBlob,并阅读 this tutorial 以了解一种方法的详细概述。

标签: python twitter sentiment-analysis


【解决方案1】:

这样的事情可能对你有用:

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

如果你能多说一些细节,我可能会为你调整一下。

【讨论】:

  • 我们在使用您提供的代码时出现以下错误。在 中,sentiment_words[word][2] += 1 TypeError: 'tuple' 对象不支持项目分配。请帮忙
  • 啊,对,当然。应该是一个列表而不是一个元组,括号变成方括号。我将编辑回复以反映。
猜你喜欢
  • 2018-11-25
  • 2015-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多