Skip to content

Instantly share code, notes, and snippets.

@jjsantanna
Last active March 3, 2022 08:49
Show Gist options
  • Save jjsantanna/a021a86b17ec625c9a1e2d7cd8a8cd67 to your computer and use it in GitHub Desktop.
Save jjsantanna/a021a86b17ec625c9a1e2d7cd8a8cd67 to your computer and use it in GitHub Desktop.
Google colab translate
I've tried many googltrans versions and other libraries; none worked except this one! Libraries that I've tried:
- !pip install googletrans; from googletrans import Translator
- !pip install googletrans==3.1.0a0; from googletrans import Translator
- !pip install googletrans==4.0.0-rc1; from googletrans import Translator
- !pip install google_trans_new; from google_trans_new import google_translator; translator = google_translator()
- !pip install translate ; from translate import Translator **WORKS TEMPORARILY**
- At Google colab googletrans==3.1.0a0 **WORKS**
from google.colab import files
uploaded = files.upload()
##
import pandas as pd
import io
df = pd.read_csv(io.BytesIO(uploaded['remaining2translate.csv']), index_col=[0])
print(len(df))
###
!pip install googletrans==3.1.0a0
from googletrans import Translator
translator = Translator(raise_exception=True)
print(translator.translate('हॅलो वर्ल्ड').text)
##
df_temp = df[df['body_en'].isna()]
for idx, row in df_temp.iterrows():
try:
translated = translator.translate(df.loc[idx,'body'])
df.loc[idx,'body_en'] = translated.text
df.loc[idx,'body_language'] = translated.src
print(df.loc[idx,'body'], "->", translated.text, "->",translated.src)
except:
break
###
len(df), len(df[df['body_en'].isna()])
##
df.to_csv('remaining2translate.csv')
from google.colab import files
files.download('remaining2translate.csv')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment