Does this lib has feature of Chinese to English?
yanlee26 opened this issue · 7 comments
`#!/usr/bin/env python3
-- coding: utf-8 --import re
from translate import Translator
translator= Translator(to_lang="zh")
translation = translator.translate("me")
print(translation) # 我` confused...
Yes. But your example is translating from en to zh.
from translate import Translator
print(Translator(from_lang='zh',to_lang="en").translate('我')) #Me
@terryyin @pengyao1207
can you please tell me how to apply this into a pandas dataframe ?
I have a column df['Text'] which contain text from different languages and I want to translate it into a new column called df['Text transalted']
Hi @mostafagafer it's probably not a good idea to apply this directly to pandas. Two reasons:
- we are using free translation service and there's limit amount you can use daily.
- It uses the network to do the translation, so very slow.
Probably build a local cache for the translations.
Thanks for your respond @terryyin , I applied google cloud translation using api , and it worked, although the data set was only 30 row and I didn't test it on a huge data set . but as a starter it was great
@mostafagafer Is you have a little data or you want to use a Paid provider you can use like this to populate your data frame:
PS: I'm using Brazilian Portuguese as output
In [1]: from translate import Translator
In [2]: import pandas as pd
# If you want a diferente provider you must add in your Translator constructor
In [3]: translator = Translator(from_lang='en', to_lang='pt-BR')
# Example of data to translate
In [4]: data = [
{'text': 'the book'},
{'text': 'the flower'},
{'text': 'the colors'},
{'text': 'the music'}
]
# Create you Pandas Data Frame
In [37]: df = pd.DataFrame(data, columns=['text'])
# Only show you to check
In [38]: df
Out[38]:
text
0 the book
1 the flower
2 the colors
3 the music
# Add a new column
In [39]: df['translated'] = [translator.translate(text) for text in df['text']]
# now to you have a new column with the translated text.
In [40]: df
Out[40]:
text translated
0 the book o livro
1 the flower a flor
2 the colors as cores
3 the music A música,
Following the documentation in case you want to use a paid provider like Microsoft.
https://translate-python.readthedocs.io/en/latest/providers.html