使用 googletrans 后出现 Textblob 情绪错误

Textblob sentiment error after using googletrans

所以我想使用 Textblob 情感来计算我的数据的情感。但是,在计算情绪之前,我将数据从印度尼西亚语翻译成英语。

这是我的代码

import pandas as pd
df = pd.read_csv('file.csv', encoding="utf-16")
from googletrans import Translator
translator = Translator()
df['english'] = df['Comment'].apply(translator.translate, src='id', dest='en')
#print(df)
#print(df['english'])
from textblob import TextBlob
def sentiment_calc(text):
    try:
        return TextBlob(text).sentiment
    except:
        return None
    
df['sentiment']=df['english'].apply(lambda text: TextBlob(text).sentiment)
print(df['sentiment'])

但后来我得到了这个错误

TypeError: The `text` argument passed to `__init__(text)` must be a string, not <class 'googletrans.models.Translated'>

有什么解决办法吗?顺便说一句,翻译结果很好。

发生错误,因为您向 TextBlob 提供的不是字符串。
df['english'] 类型是 <class 'googletrans.models.Translated'>,因此必须将其更改为字符串。

import pandas as pd
df = pd.read_csv('file.csv', encoding="utf-8")
from googletrans import Translator
translator = Translator()
df['english'] = df['Comment'].apply(translator.translate, src='id', dest='en')
#print(df)
#print(df['english'])
from textblob import TextBlob
def sentiment_calc(text):
    try:
        return TextBlob(text).sentiment
    except:
        return None
df['english'] = df['english'].astype(str) #change type to string 
df['sentiment']=df['english'].apply(lambda text: TextBlob(text).sentiment)
print(df['sentiment'])