首页 > 解决方案 > Python 中的情感分析 - TextBlob

问题描述

我对 python 和学习仍然很陌生,我的一门课程希望我使用 TextBlob 和 Pandas 对 cvs 文件进行情绪分析。到目前为止我所做的我将附在此处:

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-------')

但是我现在需要做的是我需要汇总组成句子的情感分数,然后将汇总分数转换为布尔值。我真的很挣扎,我准备在这一点上放弃!

标签: pythonpandasanalysistextblob

解决方案


到目前为止,这很好。这是我的一项工作,它将帮助您执行您正在寻找的内容。

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))

希望你能找到方法。谢谢。


推荐阅读