我正在尝试为推文创建实时情绪分析图。该程序从本地存储的JSON文件加载数据并转换" created_at"使用VaderSentiment分析文本并更新正在运行的情绪总数时,数据到unix时间。
fig = plt.figure()
ax1 = fig.add_subplot(1, 1, 1)
yar = []
total = []
created = []
sid = SentimentIntensityAnalyzer()
#ss = sid.polarity_scores(sentence)
def animate(i):
compound_total = 0
with open('python.json', 'r') as f:
line = f.readline()
for line in f:
tweet = json.loads(line)
#print(sid.polarity_scores(tweet['text']))
total.append(sid.polarity_scores(tweet['text'])['compound'])
created_at = tweet['created_at']
tweet_time = time.mktime(time.strptime(created_at,"%a %b %d %H:%M:%S +0000 %Y"))
created.append(tweet_time)
for each in total:
compound_total += each
yar.append(compound_total)
print (yar[len(yar)-1], ", ", created[len(created)-1])
ax1.clear()
ax1.plot(created, yar, 'bo')
#plt.plot(np.unique(xar), np.poly1d(np.polyfit(xar, yar, 1))(np.unique(xar)))
ani = animation.FuncAnimation(fig, animate, interval=1000)
plt.show()
数据存储在标准python列表中。我认为列表长度相同,错误表明不然。
任何人都可以建议解决方案/原因列表大小不同吗?
PS:奇怪的是程序在抛出错误之前将第一对坐标返回给系统
对于任何愚蠢的事情也感到抱歉 - 我不擅长这个