Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

def strip_punctuation(word):

punctuation_chars = ["'", '"', ",", ".", "!", ":", ";", '#', '@']
for char in punctuation_chars:
if char in word:
word = word.replace(char, "")
return word

*******
punctuation_chars = ["'", '"', ",", ".", "!", ":", ";", '#', '@']
def strip_punctuation(word):
for char in punctuation_chars:
if char in word:
word = word.replace(char, "")
return word

# list of positive words to use


def get_pos(string):
string = string.split(" ")
positive = 0
for word2 in string:
word2 = word2.lower()
if strip_punctuation(word2) in positive_words:
positive = positive + 1
return positive

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())

*******
punctuation_chars = ["'", '"', ",", ".", "!", ":", ";", '#', '@']
def strip_punctuation(word):
for char in punctuation_chars:
if char in word:
word = word.replace(char, "")
return word

def get_neg(string_n):
string_n = string_n.split(" ")
count_negative = 0
for word_n in string_n:
word_n = word_n.lower()
if strip_punctuation(word_n) in negative_words:
count_negative = count_negative + 1
return count_negative

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())

********
punctuation_chars = ["'", '"', ",", ".", "!", ":", ";", '#', '@']
def strip_punctuation(word):
for char in punctuation_chars:
if char in word:
word = word.replace(char, "")
return word

def get_pos(string_p):
string_p = string_p.split(" ")
count_positive = 0
for word_p in string_p:
word_p = word_p.lower()
if strip_punctuation(word_p) in positive_words:
count_positive = count_positive + 1
return count_positive

def get_neg(string_n):
string_n = string_n.split(" ")
count_negative = 0
for word_n in string_n:
word_n = word_n.lower()
if strip_punctuation(word_n) in negative_words:
count_negative = count_negative + 1
return count_negative

# 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())

#Opening output csv file


outfile = open('resulting_data.csv', 'w')
outfile.write('Number of Retweets, Number of Replies, Positive Score, Negative
Score, Net Score')
outfile.write('\n')

#Read csv file


fileconnection = open("project_twitter_data.csv", 'r')
lines = fileconnection.readlines()
header = lines[0]
field_names = header.strip().split(',')
print(field_names)

for row in lines[1:]:


vals = row.strip().split(',')
row_string = '{},{},{},{},
{}'.format(vals[1],vals[2],get_pos(vals[0]),get_neg(vals[0]),get_pos(vals[0])-
get_neg(vals[0]))
outfile.write(row_string)
outfile.write('\n')
#fileconnection.close()
outfile.close()

You might also like