【发布时间】:2014-09-05 00:29:14
【问题描述】:
更新:以下代码似乎可以解决问题:
for word in words:
if word not in unique_words: ##skips replicates in a given list
unique_words.append(word)
对于我的一生,我无法弄清楚如何计算一个单词在列表中的数量,而不是在列表中找到一个单词的次数。
如果我设置一个计数器...它会计算单词的出现次数。但是,我需要一些东西,因为它遍历单词列表(每个列表都是一条推文),计算一个单词的 UNIQUE 出现次数(例如,包含该单词的推文数量)。计数器,我知道如何使用它们,并且与集合 Counter...count 所有实例相同。
仅供参考...循环迭代的是推文,每个推文都应用了 .split() 函数...所以,每条推文都是单词列表。
这是我的代码没有做我需要做的事情。
sentiment = 0
for word in words:
if word in AFINN:
sentiment += AFINN[word]
for word in words:
if word not in AFINN and word not in new_sent:
new_sent[word] = sentiment
tweet_count[word] = 1
elif word in new_sent:
new_sent[word] = new_sent[word] + sentiment
tweet_count[word] += 1
另外...我有两个单词的陈述,因为这是我能够获得总情绪分数的唯一方法,而不仅仅是找到新单词的分数。不过,我在想,如果我“回归”情绪,我可能不需要双循环?但是,就目前而言,这不是我最关心的问题。
谢谢!!!
【问题讨论】:
-
该代码正在为未包含在预定义词典 (AFINN) 中的单词导出情绪分数。所以,AFINN 有一堆单词作为键,一个整数作为它的值。
-
AFINN 是我的情绪分析词表,可从www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6010获得
标签: python count unique counter tweets