Я работаю над проблемой анализа настроений в твиттере. Моя цель - собрать данные в 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'])