使用 textblob 对 csv 文件进行情感分析
Sentiment analysis on a csv file using textblob
我对 CSV
文件进行了情感分析,输出打印了句子的极性和主观性。我怎样才能获得 table 格式的输出以及添加到其中的句子分类(正面、负面或中性)?
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 数据框以获得 table 结构
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 数据框,例如
df = pd.DataFrame(bloblist, columns = ['sentence','sentiment','polarity'])
添加后,您可以像这样创建自定义计算:
df['positive'] = np.where(df.sentiment > .5,1,0)
我对 CSV
文件进行了情感分析,输出打印了句子的极性和主观性。我怎样才能获得 table 格式的输出以及添加到其中的句子分类(正面、负面或中性)?
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 数据框以获得 table 结构
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 数据框,例如
df = pd.DataFrame(bloblist, columns = ['sentence','sentiment','polarity'])
添加后,您可以像这样创建自定义计算:
df['positive'] = np.where(df.sentiment > .5,1,0)