Monday, January 04, 2021

Updated "Nunkish" translator script

Update January 15, 2021: This post has been superseded by the bidirectional Tellsis language translator (which is the enhanced version of the script here) that can be found here. And this version that runs on Windows, Linux, and Android.

 
The online Python script that can be used to translate Nunkish (name given by fans to the language used on the Tellsis continent) into English has an issue with the googletrans module, so I found a way to update it to use the google_trans_new module instead.
(日本語ブログにも掲載しました。)
My review of the 2020 Violet Evergarden movie (VIOLET EVERGARDEN the Movie) can be found here.

To use this, you need to install the google_trans_new module using
pip3 install google_trans_new

Then, run the script using
python3 nunkish_translator_2.py

Script (nunkish_translator_2.py)
-----------------
from google_trans_new import google_translator  
import requests
 
translator = google_translator()  

alphabet = {
    'a': 'u',
    'b': '',
    'c': 'y',
    'd': '',
    'e': 'o',
    'f': '',
    'g': 'v',
    'h': 't',
    'i': 'i',
    'j': '',
    'k': 'r',
    'l': 'i',
    'm': 'p',
    'n': 'n',
    'o': 'e',
    'p': 'm',
    'q': 'l',
    'r': 'k',
    's': 'y',
    't': 'h',
    'u': 'a',
    'v': 'g',
    'w': '',
    'x': '',
    'y': 'c',
    'z': '',
    'A': 'U',
    'B': '',
    'C': 'Y',
    'D': '',
    'E': 'O',
    'F': '',
    'G': 'V',
    'H': 'T',
    'I': 'I',
    'J': '',
    'K': 'R',
    'L': 'I',
    'M': 'P',
    'N': 'N',
    'O': 'E',
    'P': 'M',
    'Q': 'L',
    'R': 'K',
    'S': 'Y',
    'T': 'H',
    'U': 'A',
    'V': 'G',
    'W': '',
    'X': '',
    'Y': 'C',
    'Z': '',
    ' ': ' ',
    '0': '0',
    '1': '1',
    '2': '2',
    '3': '3',
    '4': '4',
    '5': '5',
    '6': '6',
    '7': '7',
    '8': '8',
    '9': '9',
}

tamil_script_url = 'https://inputtools.google.com/request?text={text}&itc=ta-t-i0-und'

source_language = 'ta'
target_language = 'en'

def trans(nunkish):
    tamil = ""
    for char in nunkish:
        if char not in alphabet:
            tamil += char
        else:
            tchar = alphabet[char]
        if tchar:
            tamil += tchar
        else:
            tamil += "?"
    
    print(f"Converted to tamil: {tamil}")
    
    tamil_res = requests.get(tamil_script_url.format(text=tamil), headers={
        'Content-Type': 'application/json'
    }).json()
    
    if (tamil_res[0] == 'SUCCESS'):
        tamil_script = tamil_res[1][0][1][0]
    print(f"In Tamil script: {tamil_script}")
    return translator.translate(f'{tamil_script}', lang_src=
source_language, lang_tgt=target_language)

while True:
    nunkish = input("Input nunkish: ")
    print(f"You entered: {nunkish}")
    print(trans(nunkish.replace('\n', ' ').replace('\r', '')))
---------------------
Update January 14, 2021: To use this script with results in other languages, change the line
target_language = 'en'
to something like the below (example using Japanese as desired output language)
target_language = 'ja'
I also removed the app id and key from the post since I am not sure if the author of the original script actually wants to allow everyone to freely use that id/key pair, given that Google Translate API is a paid service now. The google_trans_new module seems to be free to use.

No comments: