python - 情绪一次只看一个字符
问题描述
我正在处理 Twitter 中的情绪分析问题。我的目标是根据情绪在 csv 文件中收集数据,分三列。似乎情绪一次只看 1 个字符,而它应该一次看 1 条推文。
任何帮助将不胜感激
pos1, neg1, neu1 = 0, 0, 0
header=['Positive','Negative','Neutral']
#Create an empty csv file which has three headers: 'Positive','Negative','Neutral'
with open('List.csv','w') as file:
write=csv.DictWriter(file,fieldnames=header)
write.writeheader()
#doc for Stream Listener to tweepy: http://docs.tweepy.org/en/latest/streaming_how_to.html
class Listener(StreamListener):
def on_data(self, data):
raw_t=json.loads(data)
data=raw_t['text']
#four lines below will clear the tweets by removing: metions, has tag etc.
data = re.sub('@[A-Za-z0–9]+', '',data) #Removing @mentions
data = re.sub('#', '', data) # Removing '#' hash tag
data = re.sub('RT[\s]+', '', data) # Removing RT
data = re.sub('https?:\/\/\S+', '', data) # Removing hyperlink
global pos1
global neg1
global neu1
pos, neg, neu = 0, 0, 0
for tweet in data:
print(tweet)
analysis = TextBlob(tweet)
#print(analysis.sentiment)
#the below if statement will count the number of tweets based on their sentiment('Positive','Negative','Neutral')
if analysis.sentiment[0]>0:
pos+=1
elif analysis.sentiment[0]<0:
neg+=1
else:
neu+=1
pos1=pos1+pos
neg1=neg1+neg
neu1=neu1+neu
#write the result from counting to the csv file "List.csv"
with open('List.csv', 'a') as file:
writer = csv.DictWriter(file, fieldnames=header)
info={
'Positive':pos1,
'Negative':neg1,
'Neutral':neu1
}
writer.writerow(info)
print(data)
return True
def on_error(self, status):
print(status)
l = Listener()
stream = Stream(auth, l)
stream.filter(track=['trump'])
解决方案
推荐阅读
- c# - 将 Unity 粒子系统放入 Apple 位置
- tensorflow - 每个 .tfrecord 文件有多少张图像(用于训练 tensorflow 对象检测 API)?
- dataweave - Mulesoft:生成 txt 文件需要 3 分钟以上
- django - 如何从模型中的外键关系中检索对象
- c# - ArgumentException:已添加具有相同键的项目错误
- google-cloud-platform - 修改和删除 GCP 数据流作业
- mysql - 设置 SQL 以使用 python 远程访问
- node.js - 更新纱线后,由于节点模块相关的导入错误,反应应用程序无法编译
- python - 大熊猫的最大值和总和
- javascript - React 错误边界被调用但不渲染