情感分析比较分配情绪分数的两个列表。 JSON ValueError

时间:2016-05-01 20:42:12

标签: python dictionary sentiment-analysis

我是python的新手

这是我的问题。我想比较一个csv文件(message_ID,Comment),其中包含带有情感分数(单词,分数)的单词的句子。最后的输出应该根据被击中的单词给出每个句子的分数。我在网上读到,即使在读取其他文件时也应该使用json,因为它提供了内置函数。

data.csv:

1,经济衰退很严重

2,发生了很多事情

sentiment.txt:

经济衰退,-0.58

很好,0.15

输出:

1,经济衰退很大,-0.43 ( - 0.58 + 0.15)

2,发生了很大的事情,0.15

执行代码时收到此错误: ValueError:额外数据:第1行第2行 - 第2行第1列(字符1 - 72)

import csv
import json

scores = {}  

with open('sentiment.txt') as f:
    reader = csv.reader(f, delimiter=',')
    for row in reader:
        scores[row[0].strip()] = float(row[1].strip()) 


with open('data.csv') as f:
    reader = csv.reader(f, delimiter='\t')
    for line in f:
        tweet = json.loads(line)
        text = tweet.get('text','').encode('utf-8')
        if text:
            total_sentiment = sum(scores.get(word,0) for word in text.split())
            print("{}: {}".format(text,score))

我从how to properly loop through two files comparing strings in both files against each other

获得的代码的主要部分

感谢您的帮助

0 个答案:

没有答案
相关问题