[Python] Python Sentiment Analysis on Excel File

Discussão em 'Python' iniciado por Stack, Agosto 15, 2025.

  1. Stack

    Stack Membro Participativo

    I'm very new to python but I would like to run a sentiment analysis on customer reviews I have collected in a form of excel files. I got this code originally from a friend who was doing sentiment analysis on Tweets. Could someone guide me on which parts to cut out to exclude the Tweets aspect and what I need to add to the code in order to make it run Sentiment Analysis on the data I've collected?

    import sys,tweepy,csv,re
    from textblob import TextBlob
    import matplotlib.pyplot as plt


    class SentimentAnalysis:

    def __init__(self):
    self.tweets = []
    self.tweetText = []

    def DownloadData(self):
    # authenticating
    consumerKey = 'JUTrQyuKQuIDoEBoJRtSHo1xA'
    consumerSecret = 'XOiTV47BzE1LgWlUaoldEV72RTILDBluQ5Wz21pBQ5bobDCSgP'
    accessToken = '1146742026420944897-haxc4zgGcQFJeZLlTBW1hlzlTfW4Z9'
    accessTokenSecret = 'a6j9Vz4DSQpTc1uycEvMxXm5G59fPy2e86ep5LNLSL9Jo'
    auth = tweepy.OAuthHandler(consumerKey, consumerSecret)
    auth.set_access_token(accessToken, accessTokenSecret)
    api = tweepy.API(auth)

    # input for term to be searched and how many tweets to search
    searchTerm = input("Enter Keyword/Tag to search about: #barkbox")
    NoOfTerms = 500

    # searching for tweets
    self.tweets = tweepy.Cursor(api.search, q=searchTerm, lang = "en").items(NoOfTerms)

    # Open/create a file to append data to
    csvFile = open('resultA.csv', 'w')

    # Use csv writer
    csvWriter = csv.writer(csvFile)


    # creating some variables to store info
    polarity = 0
    positive = 0
    wpositive = 0
    spositive = 0
    negative = 0
    wnegative = 0
    snegative = 0
    neutral = 0
    totalNeg = 0
    totalPos = 0


    # iterating through tweets fetched
    for extended_tweet in self.tweets:
    #Append to temp so that we can store in csv later. I use encode UTF-8
    self.tweetText.append(self.cleanTweet(extended_tweet.text).encode('utf-8'))
    # print (tweet.text.translate(non_bmp_map)) #print tweet's text
    analysis = TextBlob(extended_tweet.text)
    # print(analysis.sentiment) # print tweet's polarity
    polarity += analysis.sentiment.polarity # adding up polarities to find the average later

    if (analysis.sentiment.polarity == 0): # adding reaction of how people are reacting to find average later
    neutral += 1
    elif (analysis.sentiment.polarity > 0 and analysis.sentiment.polarity <= 0.3):
    wpositive += 1
    elif (analysis.sentiment.polarity > 0.3 and analysis.sentiment.polarity <= 0.6):
    positive += 1
    elif (analysis.sentiment.polarity > 0.6 and analysis.sentiment.polarity <= 1):
    spositive += 1
    elif (analysis.sentiment.polarity > -0.3 and analysis.sentiment.polarity <= 0):
    wnegative += 1
    elif (analysis.sentiment.polarity > -0.6 and analysis.sentiment.polarity <= -0.3):
    negative += 1
    elif (analysis.sentiment.polarity > -1 and analysis.sentiment.polarity <= -0.6):
    snegative += 1


    # Write to csv and close csv file

    csvWriter.writerow(self.tweetText)
    csvFile.close()

    # finding average of how people are reacting
    totalPos = self.percentage((positive + wpositive + spositive), NoOfTerms)
    totalNeg = self.percentage((negative + wnegative + snegative), NoOfTerms)

    positive = self.percentage(positive, NoOfTerms)
    wpositive = self.percentage(wpositive, NoOfTerms)
    spositive = self.percentage(spositive, NoOfTerms)
    negative = self.percentage(negative, NoOfTerms)
    wnegative = self.percentage(wnegative, NoOfTerms)
    snegative = self.percentage(snegative, NoOfTerms)
    neutral = self.percentage(neutral, NoOfTerms)

    # finding average reaction
    polarity = polarity / NoOfTerms

    # printing out data
    print("General Reaction on " + searchTerm )
    print()
    print("General Report: ")

    if (polarity == 0):
    print("Neutral")
    elif (polarity > 0 and polarity <= 0.3):
    print("Weakly Positive")
    elif (polarity > 0.3 and polarity <= 0.6):
    print("Positive")
    elif (polarity > 0.6 and polarity <= 1):
    print("Strongly Positive")
    elif (polarity > -0.3 and polarity <= 0):
    print("Weakly Negative")
    elif (polarity > -0.6 and polarity <= -0.3):
    print("Negative")
    elif (polarity > -1 and polarity <= -0.6):
    print("Strongly Negative")

    print()
    print("Detailed Report: ")
    print(str(positive) + "% people thought it was positive")
    print(str(wpositive) + "% people thought it was weakly positive")
    print(str(spositive) + "% people thought it was strongly positive")
    print(str(negative) + "% people thought it was negative")
    print(str(wnegative) + "% people thought it was weakly negative")
    print(str(snegative) + "% people thought it was strongly negative")
    print(str(neutral) + "% Overall Neutral")
    print(str(positive + wpositive + spositive) + "% Overall Positive")
    print(str(negative + wnegative + snegative) + "% Overall Negative")



    self.plotPieChart(positive, wpositive, spositive, negative, wnegative, snegative, neutral, searchTerm, NoOfTerms,totalPos, totalNeg)


    def cleanTweet(self, tweet):
    # Remove Links, Special Characters etc from tweet
    return ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t]) | (\w +:\ / \ / \S +)", " ", tweet).split())

    # function to calculate percentage
    def percentage(self, part, whole):
    temp = 100 * part / whole
    return format(temp, '.2f')

    def plotPieChart(self, positive, wpositive, spositive, negative, wnegative, snegative, neutral, searchTerm, noOfSearchTerms, totalPos, totalNeg):
    labels = ['Neutral Positive [' + str(positive) + '%]', 'Weakly Positive [' + str(wpositive) + '%]','Strongly Positive [' + str(spositive) + '%]', 'Neutral [' + str(neutral) + '%]',
    'Neutral Negative [' + str(negative) + '%]', 'Weakly Negative [' + str(wnegative) + '%]', 'Strongly Negative [' + str(snegative) + '%]']
    sizes = [positive, wpositive, spositive, neutral, negative, wnegative, snegative]
    colors = ['blue','lightblue','darkblue', 'grey', 'red','lightsalmon','darkred']
    patches, texts = plt.pie(sizes, colors=colors, startangle=90)
    plt.legend(patches, labels, loc="best")
    plt.title('Peoples Reaction on "' + searchTerm + '".')
    plt.axis('equal')
    plt.xlabel(('Total Positive [' + str(totalPos) + '%] | Total Negative [' + str(totalNeg) + '%]' ))
    plt.tight_layout()
    plt.show()



    if __name__== "__main__":
    sa = SentimentAnalysis()
    sa.DownloadData()

    Continue reading...

Compartilhe esta Página