I made a CUI-based translation script

Motivation

Recently, I've been working more and more with the CUI. CUI is flexible and very nice ...

I was translating English one by one

(This is a matter of my English ability) I can read error messages such as python traceback to some extent, I couldn't read the Linux man page and --help because they were almost in English. Therefore···

Isn't it possible to create a translation script using a pipe?

I came up with the idea.

specification

--Use googletrans --Accept options with command line arguments --Supports input using | (pipe)

I actually wrote it

I also posted to gist.

#honnyaku.py
# ~Receive text with standard I / O,
# ~Print it
import sys
from googletrans import Translator
from logging import getLogger, StreamHandler, DEBUG
# ~Log settings
logger = getLogger(__name__)
handler = StreamHandler()
handler.setLevel(DEBUG)
logger.setLevel(DEBUG)
logger.addHandler(handler)
logger.propagate = False

trans = Translator()

#Main processing
def convert(text="None", lang="ja"):
    '''Function to translate'''
    #Converts non-numeric characters to strings and excludes types that cannot be converted to str, such as float types
    try:
        text = str(text)
    except:
        logger.error("You have entered something other than a string or an integer.")
    
    conv_text = trans.translate(text, dest=lang)
    return conv_text.text

Test code

This time it was developed in a test-driven development style.

# ~Test code
import honnyaku
try:
    assert "Hello" == honnyaku.convert("hello")
except AssertionError:
    print("Failure")

else:
    print("success")

Actual state 2020-02-01-151007_1824x984_scrot.png

I was able to translate it successfully! I'd like to think about how to implement options a little more, so I'd like to do it next time.

Recommended Posts

I made a CUI-based translation script (2)
I made a CUI-based translation script
I made a script to display emoji
I made a python text
I made a discord bot
I made a script to put a snippet in README.md
I made a C ++ learning site
I made a Line-bot using Python!
I made a wikipedia gacha bot
I made a fortune with Python.
I made a daemon with Python
I made a script to say hello at my Koshien
I installed Taiga.IO on CentOS7 (I made a script while I was there)
I made a dash docset for Holoviews
I made a payroll program in Python!
I touched "Orator" so I made a note
I made a character counter with Python
Beginner: I made a launcher using dictionary
I made a conversation partner like Siri
I made a Hex map with Python
I made a life game with Numpy
I made a stamp generator with GAN
I made a browser automatic stamping tool.
After studying Python3, I made a Slackbot
I made a roguelike game with Python
I made a simple blackjack with Python
I made a configuration file with Python
I made a library for actuarial science
I made a WEB application with Django
I made a neuron simulator with Python
I made a script in python to convert .md files to Scrapbox format
I made a stamp substitute bot with line
I made a python dictionary file for Neocomplete
I made a competitive programming glossary with Python
I made a weather forecast bot-like with Python.
I made a spare2 cheaper algorithm for uWSGI
I made a useful tool for Digital Ocean
I made a GUI application with Python + PyQt5
I made a Twitter fujoshi blocker with Python ①
[Python] I made a Youtube Downloader with Tkinter.
I made a router config collection tool Config Collecor
I made a simple Bitcoin wallet with pycoin
I made a downloader for word distributed expression
I made a LINE Bot with Serverless Framework!
I made a tool to compile Hy natively
I wrote a script to upload a WordPress plugin
I made a tool to get new articles
I made a random number graph with Numpy
I made a peeping prevention product for telework.
I made a simple RSS reader ~ C edition ~
I made a Caesar cryptographic program in Python.
I made a bin picking game with Python
I made a Mattermost bot with Python (+ Flask)
I made a Python Qiita API wrapper "qiipy"
I made a QR code image with CuteR
I made a script to record the active window using win32gui of Python
[AWS] I made a reminder BOT with LINE WORKS
I made a Twitter BOT with GAE (python) (with a reference)
I made a prime number generation program in Python
I made a user management tool for Let's Chat
I made a login / logout process using Python Bottle.