[英]Sentiment analysis on a csv file using textblob
我在CSV
文件上使用了情感分析,输出显示了句子的极性和主观性。 如何以表格格式获得输出以及添加到句子中的肯定或否定或中性的句子分类?
import csv
from textblob import TextBlob
infile = 'sentence.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)
我的代码的输出是:
i am very happy
1.0 1.0
its very sad
-0.65 1.0
they are bad
-0.6999999999999998 0.6666666666666666
hate the life
-0.8 0.9
she is so fantastic
0.4 0.9
提前致谢。
我建议创建一个列表列表,并将其导入到pandas数据框中以获取表结构
import csv
from textblob import TextBlob
import pandas as pd
import numpy as np
infile = 'sentence.csv'
bloblist = list()
with open(infile, 'r') as csvfile:
rows = csv.reader(csvfile)
for row in rows:
sentence = row[0]
blob = TextBlob(sentence)
bloblist.append((sentence,blob.sentiment.polarity, blob.sentiment.subjectivity))
这将为您提供一个名为bloblist
的列表列表,将其转换为pandas数据bloblist
,例如
df = pd.DataFrame(bloblist, columns = ['sentence','sentiment','polarity'])
添加后,您可以创建自定义计算,如下所示:
df['positive'] = np.where(df.sentiment > .5,1,0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.