Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)

I am a newbie in python and currently learning the use of TextBlob and Pandas for sentiment analysis on the CSV file. What I performed so far I will attach here:

Import csv

from textblob import TextBlob

import pandas as pd

df = pd.read_csv('Movie_reviews.csv', delimiter='\t', header=None)

Movie_review_texts = df[2]

Movie_review_texts

for intex, review_text in enumerate (Movie_review_texts):

    blob = TextBlob(review_text)

    print('Analysing review\t', review_text)

    for sentence in blob.sentences: 

        print('--------SENTIMENT OF SENTENCE--------')

        print(sentence, '\t', sentence.sentiment.polarity)

        print('-------END-------')

I want to know how can I aggregate the sentiment scores of the constituent sentences and then change the aggregate score into a boolean value. 

1 Answer

0 votes
by (107k points)

Your code is good. Kindly refer to the below code that will help you to achieve your end goal. 

from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer

import time

analyzer = SentimentIntensityAnalyzer()

pos_count = 0

pos_correct = 0

with open("D:/Corona_Vac/pythonprogramnet/Positive BOW.txt","r") as f:

    for line in f.read().split('\n'):

        vs = analyzer.polarity_scores(line)

        if not vs['neg'] > 0.1:

            if vs['pos']-vs['neg'] > 0:

                pos_correct += 1

            pos_count +=1

neg_count = 0

neg_correct = 0

with open("D:/Corona_Vac/pythonprogramnet/Positive BOW.txt","r") as f:

    for line in f.read().split('\n'):

        vs = analyzer.polarity_scores(line)

        if not vs['pos'] > 0.1:

            if vs['pos']-vs['neg'] <= 0:

                neg_correct += 1

            neg_count +=1

print("Positive accuracy = {}% via {} samples".format(pos_correct/pos_count*100.0, pos_count))

print("Negative accuracy = {}% via {} samples".format(neg_correct/neg_count*100.0, neg_count))

Hope this helps!

Interested in learning Python? Enroll in our Python online Course now!

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...