-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsentiment_analysis.py
More file actions
116 lines (99 loc) · 4.12 KB
/
Copy pathsentiment_analysis.py
File metadata and controls
116 lines (99 loc) · 4.12 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
###############################################################
# imports
###############################################################
import pandas as pd
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.sentiment.vader import SentimentIntensityAnalyzer
import string
from collections import Counter
import matplotlib.pyplot as plt
import get_tweets
###############################################################
# read csv and create a list of tweets
###############################################################
def tweets_csv_to_list(file):
tweets_list = []
tweets = pd.read_csv(str(file))
tweets_df = pd.DataFrame(tweets, columns=['tweet'])
print(type(tweets_df['tweet'][0]))
tweets_list.extend(tweet for tweet in tweets_df['tweet'])
return tweets_list
###############################################################
# transfer tweets to text file
###############################################################
def tweets_file(tweets_list):
tweets_file = open("text.txt", "w")
for line in tweets_list:
tweets_file.write(line.encode('unicode-escape').decode('utf-8'))
tweets_file.close()
###############################################################
# transfer tweets to text file
###############################################################
def tweets_read_file():
text = open('text.txt', encoding= 'utf-8').read()
return text
###############################################################
# convert text to lowercase and remove punctuation
###############################################################
def tweets_lower(text):
lower = text.lower()
clean_text = lower.translate(str.maketrans('','',string.punctuation))
return clean_text
###############################################################
# preprocessing for tweets using nltk stopwords
###############################################################
def tweets_preprocessing(clean_text):
tokenized = word_tokenize(clean_text, "english")
final_text = []
for word in tokenized:
if word not in stopwords.words('english'):
final_text.append(word)
return final_text
###############################################################
# find the emotions in the tweets
###############################################################
def tweets_emotion(final_text):
emotions_list =[]
with open('emotions.txt', 'r') as file:
for line in file:
final_line = line.replace('\n', '').replace(',','').replace("'",'').strip()
word, emotion = final_line.split(':')
if word in final_text:
emotions_list.append(emotion)
emotions_count = Counter(emotions_list)
print(emotions_count)
return emotions_count
###############################################################
# find sentiment score of the users tweets
###############################################################
def tweets_sentiment_score(sentiment_text):
sentiment_score = SentimentIntensityAnalyzer().polarity_scores(sentiment_text)
print(sentiment_score)
###############################################################
# plot the graph of the different emotions
###############################################################
def tweets_emotion_graph(emotions_count):
fig, ax = plt.subplots()
ax.bar(emotions_count.keys(), emotions_count.values())
fig.autofmt_xdate()
plt.title('Emotion Analysis')
plt.savefig('emotions_count.png')
plt.show()
###############################################################
# main
###############################################################
def main():
tweets_list = tweets_csv_to_list('tweets.csv')
tweets_file(tweets_list)
text = tweets_read_file()
clean_text = tweets_lower(text)
final_text = tweets_preprocessing(clean_text)
emotions_count = tweets_emotion(final_text)
tweets_sentiment_score(clean_text)
tweets_emotion_graph(emotions_count)
###############################################################
# driver code
###############################################################
if __name__ == '__main__':
main()