-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSentiment_Analysis.py
More file actions
56 lines (43 loc) · 1.31 KB
/
Sentiment_Analysis.py
File metadata and controls
56 lines (43 loc) · 1.31 KB
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
from Processed import *
from nltk.sentiment.vader import SentimentIntensityAnalyzer
nltk.download('vader_lexicon')
def sentiments(text):
"""
This compiles a list of sentiment results of each tweet in text.
text: list of tweets
"""
result = []
for tweet in text:
score = SentimentIntensityAnalyzer().polarity_scores(tweet)
result.append(score)
return result
# Finding the average of the positivity and negativity score of the 100 tweets
def positivity(x):
pos = float()
for i in x:
pos += i['pos']
pos = pos/len(x)
return pos
def negativity(x):
neg = float()
for i in x:
neg += i['neg']
neg = neg/len(x)
return neg
def main():
elon = open_file('elontweets.pickle')
trump = open_file('trumptweets.pickle')
elons = sentiments(elon)
trumps = sentiments(trump)
write_file(elons, 'sentimentsElon')
write_file(trumps, 'sentimentsTrump')
print('Mean positivity in Elon\'s tweets:', end=" ")
print(positivity(elons))
print('Mean positivity in Trump\'s tweets:', end=" ")
print(positivity(trumps))
print('Mean negativity in Elon\'s tweets:', end=" ")
print(negativity(elons))
print('Mean negativity in Trump\'s tweets:', end=" ")
print(negativity(trumps))
if __name__ == '__main__':
main()