首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用googletrans后的Textblob情感错误

使用googletrans后的Textblob情感错误
EN

Stack Overflow用户
提问于 2020-08-18 14:25:23
回答 1查看 135关注 0票数 0

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

这是我的密码

代码语言:javascript
复制
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'])

但是我得到了这个错误

代码语言:javascript
复制
TypeError: The `text` argument passed to `__init__(text)` must be a string, not <class 'googletrans.models.Translated'>

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-18 15:42:14

出现错误是因为您向TextBlob提供的不是字符串。

df['english']类型为<class 'googletrans.models.Translated'>,因此必须将其更改为字符串。

代码语言:javascript
复制
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'])
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63470743

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档