0% found this document useful (0 votes)
155 views

Sentiment 1 Assign

The document contains code to analyze sentiment in text data. It defines functions to strip punctuation from words, count positive words by comparing to a list in a text file, count negative words by comparing to a separate text file, and write the results to a new CSV file with sentiment scores added. The functions are used to analyze tweet text from a CSV and output a new CSV with sentiment values.

Uploaded by

blakmamaba
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
155 views

Sentiment 1 Assign

The document contains code to analyze sentiment in text data. It defines functions to strip punctuation from words, count positive words by comparing to a list in a text file, count negative words by comparing to a separate text file, and write the results to a new CSV file with sentiment scores added. The functions are used to analyze tweet text from a CSV and output a new CSV with sentiment values.

Uploaded by

blakmamaba
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

punctuation_chars = ["'", '"', ",", ".", "!

", ":", ";", '#', '@']


def strip_punctuation(word):
for ch in punctuation_chars:
if ch in word:

word = word.strip(ch)
word = word.replace('.','')
return word

!!!!!!!!!!!!!!!!!!!

punctuation_chars = ["'", '"', ",", ".", "!", ":", ";", '#', '@']

def strip_punctuation(word):
for ch in punctuation_chars:
if ch in word:

word = word.strip(ch)
word = word.replace('.','')
return word

def get_pos(strng):
strng = strng.split(" ")
#print(strng)
positive = 0
for wod in strng:
if strip_punctuation(wod) in positive_words:
positive+=1

return positive

# list of positive 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())
#print(positive_words)
!!!!!!!!!!!!!!!1

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

word = word.strip(ch)
word = word.replace('.','')
return word

def get_neg(nstr):
nstr = nstr.split(" ")
print(nstr)
negative = 0
for wrd in nstr:
if strip_punctuation(wrd) in negative_words:
negative +=1

return 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 ch in punctuation_chars:
if ch in word:

word = word.strip(ch)
word = word.replace('.','')
return word

def get_neg(nstr):
nstr = nstr.split(" ")
print(nstr)
negative = 0
for wrd in nstr:
if strip_punctuation(wrd) in negative_words:
negative +=1

return negative

def get_pos(strng):
strng = strng.split(" ")
#print(strng)
positive = 0
for wod in strng:
if strip_punctuation(wod) in positive_words:
positive+=1

return positive

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

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

fileconnection = open("project_twitter_data.csv", 'r')

lines = fileconnection.readlines()
print(lines)
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')

outfile.close()
!!!!!!!!!!!!!!!!!!!!!!!!

You might also like