简体   繁体   English

csv 文件的 Textblob 情感分析

[英]Textblob sentiment analysis on a csv file

I have a csv file with around 50 rows of sentences.我有一个 csv 文件,其中包含大约 50 行句子。 I'm using the textblob sentiment analysis tool.我正在使用 textblob 情绪分析工具。 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.但是,它只适用于单个句子,我希望它适用于我拥有的 csv 文件,因为我不能放入每一行并单独测试它们,因为这会花费太长时间。 How would I go about doing this?我将如何 go 这样做?

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. TextBlob 显示这个例子,当我输入一个句子时,极性显示,你不能一次输入两个句子,它不允许你。 How would i input my csv file into the example below to give me the polarity for all rows?我如何将我的 csv 文件输入到下面的示例中,以便为我提供所有行的极性?

>>> 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.编辑了 chishaku 解决方案,它对我有用。 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

I really like pandas when it comes to processing CSVs, even though this a kind of too generic for what you want to achieve. 在处理CSV方面,我真的很喜欢pandas ,尽管这对于您想要实现的目标来说太通用了。 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

You need to iterate over each row in the csv file. 您需要遍历csv文件中的每一行。

First, open the csv file. 首先,打开csv文件。

Then for each row in the file, we can access the first column in the row with row[0] . 然后,对于文件中的每一行,我们可以使用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

thanks for the tipps.感谢您的提示。 I also had to run a sentiment analysis and know I would like to export the analysis i did in pycharm to an excel sheet so that I can create graphics out of the Analyzation.我还必须运行情绪分析,并且知道我想将我在 pycharm 中所做的分析导出到 excel 表,以便我可以从分析中创建图形。 Thanks a million for your future answers!感谢您未来的答案一百万! kind regards p亲切的问候 p

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM