在Python中检测语言并将字符串翻译为英语

2024-10-04 01:23:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我想检测熊猫系列中字符串的语言,并将字符串从检测到的语言翻译成英语

df['Comments'][:7]
0                                                        @bertelsen1986
1                               20 or 30 mm rise on the Renthal Fatbar?
2                                            Luckily I have one to 🔥💪🏻
3                                   @hatsofftojj isn’t this a dope bike
4                      My bike came with a old GX eagle I have big scam
5                                                                   NaN
6   Also ist ein Spulenschock jetzt ein Daumen hoch auf einem Spektral?
Name: Comments, dtype: object

下面是用来检测字符串语言的代码。 对于22000行,代码已经运行了几乎一整天(仍在运行)

from langdetect import detect
lang = []    
for row in df.Comments:
    lang = detect(r)
    df['Comments'] = str(df['Comments'])
    df['Language'] = df['Comments'].apply(detect)

Tags: or字符串代码语言dflangonhave