-
Notifications
You must be signed in to change notification settings - Fork 2
/
gogtrans.py
24 lines (16 loc) · 1.06 KB
/
gogtrans.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from googletrans import Translator
import re
def translate_japanese(text_dict,bahasa,asal):
translator = Translator()
# Menghapus karakter "[" atau "]" dari setiap nilai dalam kamus
cleaned_text_dict2 = {key: value.replace('[', '').replace('[', '').replace(']', '').replace('「', '').replace('」', '').replace('「', '') for key, value in text_dict.items()}
cleaned_text_dict = {key: value if value else '・' for key, value in cleaned_text_dict2.items()}
print(cleaned_text_dict)
# Pisahkan teks menjadi kalimat-kalimat
sentences = [value for key, value in cleaned_text_dict.items()]
# Terjemahkan setiap kalimat
translated_sentences = [translator.translate(sentence, src=asal, dest=bahasa).text for sentence in sentences]
# Gabungkan kembali hasil terjemahan ke dalam bentuk awal
translated_text_dict = {key: translated_sentences[i] for i, key in enumerate(cleaned_text_dict.keys())}
hasil_terjemah = {key: value.replace("\'", '’') for key, value in translated_text_dict.items()}
return hasil_terjemah