Tetroy64
Tetroy64

Reputation: 15

Sentiment Analysis does not display correct results

def sentiment(polarity):
   if blob.sentiment.polarity < 0:
     print("Negative")
   elif blob.sentiment.polarity > 0:
     print("Positive")
   else:
     print("Neutral")

Above is defining polarity

f = open("data3.txt", "r")
for x in f:
    print(x)
print(blob.sentiment)
sentiment(blob.sentiment.polarity)

Above is reading line by line the txt file as well as printing the sentence, sentiment and polarity

Unfortunately, when running the file, it shows .5 polarity for every single sentence. I am unsure how to fix it.

Upvotes: 0

Views: 106

Answers (1)

Omar Alhinnawi
Omar Alhinnawi

Reputation: 36

f = open("data3.txt", "r") for x in f: print(x) print(blob.sentiment) sentiment(blob.sentiment.polarity)

Based on your code here, it doesn't seem like you're giving TextBlob your string input in each iteration. I haven't worked extensively with blob but from my understanding, each blob instance is unique and you need to make a new blob for each line. So instead of the above it should be something like this:

f = open("data3.txt", "r") for x in f: blob=TextBlob(x) print(x) print(blob.sentiment) sentiment(blob.sentiment.polarity)

I hope that helps!

Upvotes: 2

Related Questions