0% found this document useful (0 votes)
181 views3 pages

Sentiment Analysis Script

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
181 views3 pages

Sentiment Analysis Script

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

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

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


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

word = [Link](ch)
word = [Link]('.','')
return word

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

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

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

word = [Link](ch)
word = [Link]('.','')
return word

def get_pos(strng):
strng = [Link](" ")
#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([Link]())
#print(positive_words)
!!!!!!!!!!!!!!!1

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

word = [Link](ch)
word = [Link]('.','')
return word

def get_neg(nstr):
nstr = [Link](" ")
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([Link]())

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

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

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

word = [Link](ch)
word = [Link]('.','')
return word

def get_neg(nstr):
nstr = [Link](" ")
print(nstr)
negative = 0
for wrd in nstr:
if strip_punctuation(wrd) in negative_words:
negative +=1

return negative

def get_pos(strng):
strng = [Link](" ")
#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([Link]())

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([Link]())

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

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

lines = [Link]()
print(lines)
header = lines[0]
field_names = [Link]().split(',')
print(field_names)
for row in lines[1:]:

vals = [Link]().split(',')
row_string = '{},{},{},{},
{}'.format(vals[1],vals[2],get_pos(vals[0]),get_neg(vals[0]),get_pos(vals[0])-
get_neg(vals[0]))
[Link](row_string)
[Link]('\n')

[Link]()
!!!!!!!!!!!!!!!!!!!!!!!!

You might also like