-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvader_analysis_musks_tweets.py
64 lines (42 loc) · 1.9 KB
/
vader_analysis_musks_tweets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import nltk
import pandas
import numpy
import matplotlib.pyplot as matplot
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
analyzer = SentimentIntensityAnalyzer()
tweets = pandas.read_csv("2022.csv") # Create a data frame with the Musks's tweet objects.
vader_compound = []
vader_positive = []
vader_negative = []
vader_neutral = []
for tweet in tweets["tweet"]: # For every Elon Musk tweet,
positive_score = analyzer.polarity_scores(tweet)["pos"] # Perform sentiment analysis,
vader_positive.append(positive_score) # Add to a list.
negative_score = analyzer.polarity_scores(tweet)["neg"]
vader_negative.append(negative_score)
neutral_score = analyzer.polarity_scores(tweet)["neu"]
vader_neutral.append(neutral_score)
compound_score = analyzer.polarity_scores(tweet)["compound"]
vader_compound.append(compound_score)
# Add each of the lists as a column to the data frame.
tweets["vader_positive"] = vader_positive
tweets["vader_negative"] = vader_negative
tweets["vader_neutral"] = vader_neutral
tweets["vader_compound"] = vader_compound
print()
# Overall Averages, All Tweets
print("Positive Average: " + str(tweets["vader_positive"].mean())) # Positive Average
print("Negative Average: " + str(tweets["vader_negative"].mean())) # Negative Average
print("Neutral Average: " + str(tweets["vader_neutral"].mean())) # Neutral Average
print("Compound Average: " + str(tweets["vader_compound"].mean())) # Compound Average
print()
# Line Plot, January Tweets, Compound Score
matplot.style.use("_mpl-gallery")
compound_january = []
tweet_number = 0
for date in tweets["date"]:
if date[0:7] == "2022-01":
compound_january.append(tweets["vader_compound"][tweet_number])
tweet_number += 1
matplot.plot(compound_january)
matplot.show()