-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab.py
82 lines (64 loc) · 2.17 KB
/
lab.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
punctuation_chars = ["'", '"', ",", ".", "!", ":", ";", '#', '@']
def strip_punctuation(wrd):
"""
Removes punctuation characters from a word.
Args:
wrd (str): The input word.
Returns:
str: The word without punctuation characters.
"""
for c in punctuation_chars:
wrd = wrd.replace(c, '')
return wrd
negative_words = []
with open("negative_words.txt") as neg_f:
for line in neg_f:
if line[0] != ';' and line[0] != '\n':
negative_words.append(line.strip())
def get_neg(sentences):
"""
Counts the number of negative words in a sentence.
Args:
sentences (str): The input sentence.
Returns:
int: The count of negative words.
"""
wrds = sentences.split()
nag_count = 0
for wrd in wrds:
temp_wrd = strip_punctuation(wrd.lower())
if temp_wrd in negative_words:
nag_count += 1
return nag_count
positive_words = []
with open("positive_words.txt") as pos_f:
for line in pos_f:
if line[0] != ';' and line[0] != '\n':
positive_words.append(line.strip())
def get_pos(sentences):
"""
Counts the number of positive words in a sentence.
Args:
sentences (str): The input sentence.
Returns:
int: The count of positive words.
"""
wrds = sentences.split()
pos_count = 0
for wrd in wrds:
temp_wrd = strip_punctuation(wrd.lower())
if temp_wrd in positive_words:
pos_count += 1
return pos_count
with open("project_twitter_data.csv", "r") as input_file, open("resulting_data.csv", "w") as output_file:
header = "Number of Retweets, Number of Replies, Positive Score, Negative Score, Net Score\n"
output_file.write(header)
lines = input_file.readlines()[1:]
for line in lines:
data = line.strip().split(',')
twt_txt, rtwt_num, twt_rpl = data[0], int(data[1]), int(data[2])
pos_score = get_pos(twt_txt)
neg_score = get_neg(twt_txt)
net_score = pos_score - neg_score
result = f"{rtwt_num},{twt_rpl},{pos_score},{neg_score},{net_score}\n"
output_file.write(result)