dimanche 7 juin 2020

Writing a new row in .csv file based on if (Sentiment TextBlob) condition

Im working in a sentiment analysis project (as a practice) using TextBlob.

All good so far, basically Im analizing twits (in terms of polarity) and I got very good results.

Im writing the percentage of positive, strongly positive, weakly positive, negative, strongly negative, weakly negative and neutral results.

I would like to know now how to append a .csv just with a Column called sentiment based on the percentage reflected below.

My .csv (Input)

Input

Piece of the code

    #how people are reacting
    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("How people are reacting on " + searchTerm + " by analyzing " + str(NoOfTerms) + " tweets.")
    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) + "% people thought it was neutral")

Output of the current code

Current_output

10 rows represent the 100%

Desired output:

Desired output

Aucun commentaire:

Enregistrer un commentaire