2

I have a csv file with around 50 rows of sentences. I'm using the textblob sentiment analysis tool. To test the polarity of a sentence, the example shows you write a sentence and the polarity and subjectivity is shown. However, it only works on a single sentence, I want it to work for the csv file that I have, as I can't put in each row and test them individually as it would take too long. How would I go about doing this?

TextBlob show this example, when I type in a sentence, the polarity shows, you can't input two sentences at one time, it doesn't let you. How would i input my csv file into the example below to give me the polarity for all rows?

>>> testimonial = TextBlob("Textblob is amazingly simple to use. What great fun!")
>>> testimonial.sentiment
Sentiment(polarity=0.39166666666666666, subjectivity=0.4357142857142857)
>>> testimonial.sentiment.polarity
0.39166666666666666

edited chishaku solution and it worked for me. Solution:

import csv
from textblob import TextBlob

infile = 'xxx.csv'

with open(infile, 'r') as csvfile:
    rows = csv.reader(csvfile)
    for row in rows:
        sentence = row[0]
        blob = TextBlob(sentence)
        print blob.sentiment
5
  • You want the sentiment analysis of all the rows combined? Or you want the sentiment analysis of each row/sentence? Commented Feb 22, 2016 at 16:53
  • sentiment analysis of each row Commented Feb 22, 2016 at 16:54
  • Did you try anything? What is the format of the .csv file? Commented Feb 22, 2016 at 16:55
  • Ive tried using import csv, opening the csv file, however the polarity returned is 0, showing me it doesn't work Commented Feb 22, 2016 at 16:56
  • format is one column with 50 rows of sentences Commented Feb 22, 2016 at 16:56

2 Answers 2

6

I really like pandas when it comes to processing CSVs, even though this a kind of too generic for what you want to achieve. But maybe you'll want to do more processing with your data so I'm posting the pandas solution.

import pandas as pd

# To read a CSV file
# df = pd.read_csv('sentences.csv')
df = pd.DataFrame({'sentence': ['I am very happy', 'I am very sad', 'I am sad but I am happy too']})

from textblob import TextBlob

# The x in the lambda function is a row (because I set axis=1)
# Apply iterates the function accross the dataframe's rows
df['polarity'] = df.apply(lambda x: TextBlob(x['sentence']).sentiment.polarity, axis=1)
df['subjectivity'] = df.apply(lambda x: TextBlob(x['sentence']).sentiment.subjectivity, axis=1)

>>> print(df)
                      sentence  polarity  subjectivity
0              I am very happy      1.00             1
1                I am very sad     -0.65             1
2  I am sad but I am happy too      0.15             1
Sign up to request clarification or add additional context in comments.

2 Comments

from textblob import TextBlob gives me a ModuleNotFoundError: No module named 'textblob'
^ pip install textblob ; refer - textblob.readthedocs.io/en/dev/index.html
4

You need to iterate over each row in the csv file.

First, open the csv file.

Then for each row in the file, we can access the first column in the row with row[0].

import csv
from textblob import TextBlob

infile = '/path/to/file.csv'

with open(infile, 'r') as csvfile:
    rows = csv.reader(csvfile)
    for row in rows:
        sentence = row[0]
        blob = TextBlob(sentence)
        print sentence
        print blob.sentiment.polarity, blob.sentiment.subjectivity

3 Comments

i edited your answer and it now works! thanks a lot!
@u.1234 Glad to hear. As it seems you are new to Stack Overflow, I'd like to say welcome. If you find a solution that works for you and you'd like to share it to benefit others, it's best to edit your original question and add the solution there. For any answer that is helpful or satisfies your question, you can upvote and/or "accept" the answer. Learn more on how to make the most of Stack Overflow here: stackoverflow.com/tour
@u.1234 I don't understand the attribute sentiment.polarity is taken from where? we know that we open a csv file, so it may contain 3 column by row

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.