[英]Textblob sentiment error after using googletrans
so I want to use Textblob sentiment to calculate the sentiment of my data.所以我想使用 Textblob sentiment 来计算我的数据的情绪。 But, before calculating the sentiment, I translated the data from Indonesian to English.
但是,在计算情绪之前,我将数据从印度尼西亚语翻译成英语。
Here are my codes这是我的代码
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'])
But then I got this error但后来我得到了这个错误
TypeError: The `text` argument passed to `__init__(text)` must be a string, not <class 'googletrans.models.Translated'>
Any Solution?任何解决方案? By the way, the translation results were fine.
顺便说一句,翻译结果很好。
An error occurs because you are giving something other than a string to TextBlob.发生错误是因为您向 TextBlob 提供的不是字符串。
df['english']
type is <class 'googletrans.models.Translated'>
, so it must be changed to a string. 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'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.