This repository has been archived by the owner on Aug 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
47 lines (38 loc) · 1.58 KB
/
app.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
punctuation_chars = ["'", '"', ",", ".", "!", ":", ";", '#', '@']
positive_words = []
with open("Input/positive_words.txt") as pos_f:
for lin in pos_f:
if lin[0] != ';' and lin[0] != '\n':
positive_words.append(lin.strip())
negative_words = []
with open("Input/negative_words.txt") as pos_f:
for lin in pos_f:
if lin[0] != ';' and lin[0] != '\n':
negative_words.append(lin.strip())
tweets = []
with open("Input/sample_twitter_data.csv", "r") as twitter_data:
lines = twitter_data.readlines()
# header = lines[0]
# field_names = header.strip().split(',')
for row in lines[1:]:
tweet = row.strip().split(',')
tweets.append(tweet)
def strip_punctuation(word):
word = [char for char in word if char not in punctuation_chars]
word = ''.join(word)
return word
def get_scores(data):
data = data.split()
positive_score = len([w.lower() for w in data if w in positive_words])
negative_score = len([w.lower() for w in data if w in negative_words])
net_score = positive_score - negative_score
sentiment = positive_score, negative_score, net_score
sentiment = list(sentiment)
return sentiment
with open("Output/resulting_data.csv", "w") as results:
results.write('Number of Retweets, Number of Replies, Positive Score, Negative Score, Net Score')
results.write('\n')
for tweet in tweets:
result = get_scores(strip_punctuation(tweet[0]))
row_string = '{},{},{},{},{}'.format(tweet[1], tweet[2], result[0], result[1], result[2])
results.write(row_string + '\n')