
- Document:
- install:
pip install googletrans - install:
conda install -c conda-forge googletrans
- author: Prasert Kanawattanachai
- YouTube: https://www.youtube.com/prasertcbs
- Chulalongkorn Business School
from IPython.display import YouTubeVideo
YouTubeVideo('9tR1aqQEwGY', width=720, height=405)
from googletrans import Translator
import pandas as pd
# pandas display options
pd.set_option('display.max_rows', 100)
dlang = pd.read_csv('https://github.com/prasertcbs/basic-dataset/raw/master/google_translate_language_code.csv')
dlang
| language | code | |
|---|---|---|
| 0 | Afrikaans | af |
| 1 | Irish | ga |
| 2 | Albanian | sq |
| 3 | Italian | it |
| 4 | Arabic | ar |
| 5 | Japanese | ja |
| 6 | Azerbaijani | az |
| 7 | Kannada | kn |
| 8 | Basque | eu |
| 9 | Korean | ko |
| 10 | Bengali | bn |
| 11 | Latin | la |
| 12 | Belarusian | be |
| 13 | Latvian | lv |
| 14 | Bulgarian | bg |
| 15 | Lithuanian | lt |
| 16 | Catalan | ca |
| 17 | Macedonian | mk |
| 18 | Chinese Simplified | zh-CN |
| 19 | Malay | ms |
| 20 | Chinese Traditional | zh-TW |
| 21 | Maltese | mt |
| 22 | Croatian | hr |
| 23 | Norwegian | no |
| 24 | Czech | cs |
| 25 | Persian | fa |
| 26 | Danish | da |
| 27 | Polish | pl |
| 28 | Dutch | nl |
| 29 | Portuguese | pt |
| 30 | English | en |
| 31 | Romanian | ro |
| 32 | Esperanto | eo |
| 33 | Russian | ru |
| 34 | Estonian | et |
| 35 | Serbian | sr |
| 36 | Filipino | tl |
| 37 | Slovak | sk |
| 38 | Finnish | fi |
| 39 | Slovenian | sl |
| 40 | French | fr |
| 41 | Spanish | es |
| 42 | Galician | gl |
| 43 | Swahili | sw |
| 44 | Georgian | ka |
| 45 | Swedish | sv |
| 46 | German | de |
| 47 | Tamil | ta |
| 48 | Greek | el |
| 49 | Telugu | te |
| 50 | Gujarati | gu |
| 51 | Thai | th |
| 52 | Haitian Creole | ht |
| 53 | Turkish | tr |
| 54 | Hebrew | iw |
| 55 | Ukrainian | uk |
| 56 | Hindi | hi |
| 57 | Urdu | ur |
| 58 | Hungarian | hu |
| 59 | Vietnamese | vi |
| 60 | Icelandic | is |
| 61 | Welsh | cy |
| 62 | Indonesian | id |
| 63 | Yiddish | yi |
translator = Translator() # create object/instance from Translator class
r=translator.translate('สวัสดี', dest='ja', src='th')
print(f'{r.text}: {r.pronunciation}')
こんにちは!: Kon’nichiwa!
r=translator.translate('こんにちは', dest='ko')
print(f'{r.text}: {r.pronunciation}')
안녕하세요: annyeonghaseyo
r=translator.translate('안녕하세요', dest='th')
print(f'{r.text}: {r.pronunciation}')
สวัสดี: S̄wạs̄dī
r=translator.translate("life is beautiful", dest='th')
r.text
'ชีวิตช่างสวยงาม'
r=translator.translate("life is beautiful", dest='it')
r.text
'la vita è bella'
r=translator.translate("ฉันรักคุณ", dest='ja')
print(f'{r.text}: {r.pronunciation}')
わたしは、あなたを愛しています: Watashi wa, anata o aishiteimasu
r=translator.translate('わたしは、あなたを愛しています', dest='th')
print(f'{r.text}: {r.pronunciation}')
ฉันรักคุณ: C̄hạn rạk khuṇ
words=['water', 'toilet', 'thank you']
translations = translator.translate(words, dest='th')
for t in translations:
print(f'{t.origin} -> {t.text} ({t.pronunciation})')
water -> น้ำ (N̂ả)
toilet -> ห้องน้ำ (H̄̂xngn̂ả)
thank you -> ขอบคุณ (None)
words=['hello', 'thank you', 'water', 'toilet', 'thank you', 'egg tart', 'muffin', 'green tea']
for w in words:
for lang in ['th', 'ja', 'de']:
t=translator.translate(w, dest=lang)
print(f'{w} -> {t.text} ({t.pronunciation})')
hello -> สวัสดี (S̄wạs̄dī)
hello -> こんにちは (Kon'nichiwa)
hello -> Hallo (None)
thank you -> ขอบคุณ (None)
thank you -> ありがとうございました (None)
thank you -> Vielen Dank (thank you)
water -> น้ำ (N̂ả)
water -> 水 (Mizu)
water -> Wasser (None)
toilet -> トイレ (Toire)
toilet -> Toilette (None)
thank you -> ขอบคุณ (None)
thank you -> ありがとうございました (None)
thank you -> Vielen Dank (thank you)
egg tart -> ทาร์ตไข่ (None)
egg tart -> エッグタルト (None)
egg tart -> Eitörtchen (egg tart)
muffin -> มัฟฟิน (Mạffin)
muffin -> マフィン (Mafin)
muffin -> Muffin (None)
green tea -> ชาเขียว (None)
green tea -> 緑茶 (None)
green tea -> grüner Tee (green tea)
words=['สวัสดี', 'ขอบคุณ', 'ข้าว', 'น้ำ', 'ชา', 'กาแฟ']
for w in words:
for lang in ['en', 'ja', 'fr']:
t=translator.translate(w, dest=lang)
print(f'{w} -> {t.text} | {t.pronunciation} ({lang})')
สวัสดี -> Hello! | None (en)
สวัสดี -> こんにちは! | Kon'nichiwa! (ja)
สวัสดี -> salut! | None (fr)
ขอบคุณ -> thank | None (en)
ขอบคุณ -> 感謝 | Kansha (ja)
ขอบคุณ -> remercier | None (fr)
ข้าว -> rice | None (en)
ข้าว -> ご飯 | Gohan (ja)
ข้าว -> riz | None (fr)
น้ำ -> water | None (en)
น้ำ -> 水 | Mizu (ja)
น้ำ -> l'eau | None (fr)
ชา -> tea | None (en)
ชา -> お茶 | Ocha (ja)
ชา -> thé | None (fr)
กาแฟ -> coffee | None (en)
กาแฟ -> コーヒー | Kōhī (ja)
กาแฟ -> café | None (fr)
translate and save to CSV, Excel
words=['สวัสดี', 'ขอบคุณ', 'ข้าว', 'น้ำ', 'ชา', 'กาแฟ', 'โรงแรม', 'ห้องน้ำ', 'สนามบิน', 'ร้านอาหาร']
df=pd.DataFrame(words, columns=['th'])
df
| th | |
|---|---|
| 0 | สวัสดี |
| 1 | ขอบคุณ |
| 2 | ข้าว |
| 3 | น้ำ |
| 4 | ชา |
| 5 | กาแฟ |
| 6 | โรงแรม |
| 7 | ห้องน้ำ |
| 8 | สนามบิน |
| 9 | ร้านอาหาร |
df.to_csv('basic_th.csv', index=False)
df = pd.read_csv('basic_th.csv')
df
| th | |
|---|---|
| 0 | สวัสดี |
| 1 | ขอบคุณ |
| 2 | ข้าว |
| 3 | น้ำ |
| 4 | ชา |
| 5 | กาแฟ |
| 6 | โรงแรม |
| 7 | ห้องน้ำ |
| 8 | สนามบิน |
| 9 | ร้านอาหาร |
lang=['en', 'la', 'fr', 'ja', 'zh-CN', 'fa']
for dest in lang:
df[dest]=df.apply(lambda r: translator.translate(r['th'], dest=dest).text, axis=1)
df
| th | en | la | fr | ja | zh-CN | fa | |
|---|---|---|---|---|---|---|---|
| 0 | สวัสดี | Hello! | Salve! | salut! | こんにちは! | 你好! | سلام! |
| 1 | ขอบคุณ | thank | gratias ago | remercier | 感謝 | 谢谢 | تشکر |
| 2 | ข้าว | rice | rice | riz | ご飯 | 白饭 | برنج |
| 3 | น้ำ | water | aqua | l'eau | 水 | 水 | اب |
| 4 | ชา | tea | tea | thé | お茶 | 茶 | چای |
| 5 | กาแฟ | coffee | capulus | café | コーヒー | 咖啡 | قهوه |
| 6 | โรงแรม | hotel | deversorium | Hôtel | ホテル | 旅馆 | هتل |
| 7 | ห้องน้ำ | toilet | CULTUS | toilette | トイレ | 厕所 | توالت |
| 8 | สนามบิน | airport | aeroportus | aéroport | 空港 | 飞机场 | فرودگاه |
| 9 | ร้านอาหาร | Restaurant | popina | Restaurant | 飲食店 | 餐厅 | رستوران |
df.to_csv('translation.csv', index=False)
df.to_excel('translation.xlsx', index=False)
อีกหนึ่งตัวอย่างการนำไปใช้งาน
‘’’
Create By Joompot Sriyapan
Date 29/3/2019
Name
Google Translate Example 1
Description
แปลภาษาอังกฤษเป็นไทยด้วย Python
Required
python 3.7.3
googletrans 2.4.0
Pre-programmed
pip install googletrans
‘’’
from googletrans import Translator
T = Translator()
source = “Hello, world”
try:
dest = T.translate(source,src=”en”,dest=”th”).text
print(dest)
except:
print (“Error!!!”)
Reference : - https://medium.com/@JoTaijiquan/google-translate-with-python-2b4db669f6fa
goslate
ิอีกโมดูลหนึ่งคือ goslate เป็นโมดูลที่นำ Google Translate มาใช้งานร่วมกับ Python รองรับทั้ง Python 2 และ Python 3 (License: MIT) https://pypi.python.org/pypi/goslate
สามารถติดตั้งได้ง่าย ๆ โดยใช้คำสั่ง pip ดังนี้ครับ
pip install goslate
ในการใช้งานสามารถใช้งานได้ง่าย ๆ ดังนี้ครับ
gs.translate(‘ข้อความ’, ‘รหัสภาษา เช่น en คือ ภาษาอังกฤษ th คือ ภาษาไทย’)
ตัวอย่าง
>>> import goslate
>>> gs = goslate.Goslate()
>>> print(gs.translate('hello world', 'th')) #
สวัสดีชาวโลก
>>> print(gs.translate('สวัสดีชาวโลก', 'en'))
Hello world!
ตัวอย่างการนำไปประยุกต์ใช้
นำโมดูล SpeechRecognition มาใช้รับเสียงจากชาวต่างชาติ แล้วใช้โมดูล goslate มาแปลเป็นข้อความภาษาไทย
import goslate
import speech_recognition as sr
gs = goslate.Goslate()
r = sr.Recognizer()
with sr.Microphone() as source: # เรียกใช้ Microphone พื้นฐานของระบบ
audio = r.listen(source) # รับเสียงเข้ามาแล้วประมวลผลส่งไปยัง Google Speech Recognition API
try:
a = r.recognize(audio)
b = gs.translate(a, 'th') # แสดงข้อความจากเสียงด้วย Google Speech Recognition
print(b)
except LookupError: # ประมวลผลแล้วไม่รู้จักหรือเข้าใจเสียง
print("Could not understand audio")
Reference : https://python3.wannaphong.com/2015/01/python.html