-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsentiment_code.py
More file actions
76 lines (56 loc) · 2.12 KB
/
sentiment_code.py
File metadata and controls
76 lines (56 loc) · 2.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
def strip_punctuation(word):
"""removes characters considered punctuation from everywhere in the word"""
for char in punctuation_chars:
if char in word:
word = word.replace(char,'')
return word
def get_pos(sentence):
'''Returns the count of positive words in a sentence'''
words = sentence.split()
count = 0
for word in words :
word = strip_punctuation(word)
word = word.lower()
if word in positive_words:
count = count+1
return count
def get_neg(sentence):
'''Returns the count of negative words in a sentence'''
words = sentence.split()
count = 0
for word in words:
word = strip_punctuation(word)
word = word.lower()
if word in negative_words:
count = count+1
return count
punctuation_chars = ["'", '"', ",", ".", "!", ":", ";", '#', '@']
# lists of words to use
positive_words = []
with open("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("negative_words.txt") as pos_f:
for lin in pos_f:
if lin[0] != ';' and lin[0] != '\n':
negative_words.append(lin.strip())
resulting_data = open('resulting_data.csv', 'w')
resulting_data.write('Number of Retweets, Number of Replies, Positive Score, Negative Score, Net Score')
resulting_data.write('\n')
with open('project_twitter_data.csv') as project_twitter_data:
header = project_twitter_data.readline()
line = 0
for row in project_twitter_data:
row = row.strip()
item = row.split(',')
NumberOfRetweets = item[1]
NumberOfReplies = item[2]
PositiveScore = get_pos(item[0])
NegativeScore = get_neg(item[0])
NetScore = PositiveScore - NegativeScore
row_string = '{}, {}, {}, {}, {}'.format(NumberOfRetweets, NumberOfReplies, PositiveScore, NegativeScore, NetScore)
resulting_data.write(row_string)
resulting_data.write('\n')
resulting_data.close()