如何在不同的时间python中获得返回值的总和

时间:2015-06-24 15:16:36

标签: python-2.7 python-3.x

from vaderSentiment.vaderSentiment import sentiment as vaderSentiment

count=0;

f1 = open('testData.txt')#input file 

sentence= f1.readline()

while sentence:

    count += 1

    print (sentence)

    vs =vaderSentiment(sentence)

    print ("\t" + str(vs))

    sentence=f1.readline()

f1.close

这是代码。输入文件中有多行句子。结果是这样的。

例如: 她很好

{'pos': 0.592, 'neu': 0.408, 'neg': 0.0, 'compound': 0.4404}

她很好

{'pos': 0.512, 'neu': 0.488, 'neg': 0.0, 'compound': 0.2732}

我希望整个文本文件正面,负面,中立分开。这意味着作为句子正数的一个例子。所以请帮助我。我不知道该怎么做

1 个答案:

答案 0 :(得分:0)

这是一个例子,希望它有所帮助

from vaderSentiment.vaderSentiment import sentiment as vaderSentiment

testData = ['VADER is smart, handsome, and funny.', 'VADER is smart, handsome, and funny!', 'VADER is very smart, handsome, and funny.', 'VADER is VERY SMART, handsome, and    FUNNY.', 'VADER is VERY SMART, handsome, and FUNNY!!!', 'VADER is VERY SMART, really handsome, and INCREDIBLY FUNNY!!!', 'The book was good.', 'The book was kind of good.',    'The plot was good, but the characters are uncompelling and the dialog is not great.', 'A really bad, horrible book.', "At least it isn't a horrible book.", ':) and :D', '',   'Today sux', 'Today sux!', 'Today SUX!', "Today kinda sux! But I'll get by, lol"]

result = { 'pos':[] , 'neg':[], 'compound':[], 'neu':[] }

for item in testData:
    vs =vaderSentiment(item)
    result['pos'].append(vs['pos'])
    result['neg'].append(vs['neg'])
    result['compound'].append(vs['compound'])
    result['neu'].append(vs['neu'])

print 'sum:'
for i in result.keys():
    print '\t',i, '=>', sum(result[i])

和:     neg => 3.542     neu => 5.59     pos => 6.868     compound => 4.7512