def sentiment(text)
................//some code
pos=round(pos,3)
neu=round(neu,3)
neg=round(neg,3)
result={'pos':[pos] , 'neg':[neg],'neu':[neu] }
return result
count=0;
f1 = open('testData.txt')
sentence= f1.readline()
result={'pos':[] , 'neg':[],'neu':[] }
for sentence in f1:
vs =sentiment(sentence)
result['pos'].append(vs['pos'])
result['neg'].append(vs['neg'])
result['neu'].append(vs['neu'])
sentence=f1.readline()
print ('sum:')
for i in result.keys():
print ('\t',i, '=>', sum(result[i]))
I got an error
Traceback (most recent call last):
File "C:\Users\DIl\Desktop\MYChoi\lex\sentiment_lexian.py", line 305, in <module>
print ('\t',i, '=>', sum(result[i]))
TypeError: unsupported operand type(s) for +: 'int' and 'list'
The default for sum()
is 0
-- it's meant for adding up numbers. However, you have given it a list
of list
s instead:
# 'pos' maps to a list with a single number in it
result = {'pos':[pos], 'neg':[neg], 'neu':[neu]}
# everytime this runs, 'pos' gets a new list
result['pos'].append(vs['pos'])
# i == 'pos'
# result[i] == [[0.827], [0.273], [0.111]] (for example)
for i in result.keys():
print ('\t',i, '=>', sum(result[i]))
If you change the line in sentiment
from
result = {'pos':[pos], 'neg':[neg], 'neu':[neu]}
to
result = {'pos':pos, 'neg':neg, 'neu':neu}
then that should do what you want, if pos
, neg
, and neu
are single numbers.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.