I've been enjoying using Duolingo to learn French, German, and Italian. I often rely on Google Translate to check how certain expressions are rendered in those languages.
However, it can get a bit tedious to repeatedly translate the same English sentence into French, German, and Italian one by one.
So, I decided to write a program that uses the Google Translate API to automatically generate translations of an English expression into all three languages.
This time, I chose to write the program in Common Lisp. I’ve grown a little bored with Python lately, and as an old researcher, I used to write a lot of Lisp code back in the day. I wanted to revisit those good old times.
At first, I tried using modern AI tools to help generate the code. Unfortunately, the programs they produced didn’t work as expected. I found several critical errors and had to fix them manually.
Here’s the corrected Lisp program:
; You should get a Google Cloud API Key from https://cloud.google.com/translate
; In your .bashrc add
; export GOOGLE_API_KEY="YOUR API KEY"
(ql:quickload '(:drakma :cl-json :babel))
(defpackage :google-translate
(:use :cl :drakma :cl-json)
(:export :translate))
(in-package :google-translate)
(defun get-api-key ()
(or (uiop:getenv "GOOGLE_API_KEY")
(error "GOOGLE_API_KEY not found")))
(defun translate-with-google (text source target)
"Translate TEXT from SOURCE language to TARGET language using Google Translate API."
(let* ((url "https://translation.googleapis.com/language/translate/v2")
(api-key (get-api-key))
(params `(("q" . ,text)
("source" . ,source)
("target" . ,target)
("format" . "text")
("key" . ,api-key)))
(response (drakma:http-request url :method :post
:content-type "application/x-www-form-urlencoded"
:parameters params))
(response-string (babel:octets-to-string response :encoding :utf-8))
(json (cl-json:decode-json-from-string response-string)))
(cdr (assoc :translated-text (cadr (assoc :translations (cdr (assoc :data json))))))))
(defun translate (phrase)
(format t "French:~%~a~%~%" (translate-with-google phrase "en" "fr"))
(format t "German:~%~a~%~%" (translate-with-google phrase "en" "de"))
(format t "Italian:~%~a~%~%" (translate-with-google phrase "en" "it")))
;; usage:
;(use-package :google-translate)
;(translate "Hello, world!")
To the extent possible under law,
the person who associated CC0
with this work has waived all copyright and related or neighboring
rights to this work.
Related entries (automatically calculated):
Unicode decode error "'utf-8' codec can't decode byte 0xfa in position 0: invalid start byte" when using MeCab
Redirecting URL in Ruby on Rails
Showing the favicon in Google search results
Using unicode characters in Windows command line
Using Python on Windows
css <pre> and <code> for mobile devices
Aligning Facebook button and Twitter button
On This Day: Atomic Bomb Dropped on Nagasaki
Login window freezes when making VNC connection from Windows to Mac
UAV/UGV Autonomous Cooperation
A small program which extracts rhythmic word sequences such as Tanka(57577) or Haiku(575) from a plain text
Culture as the base of our country: Prof. Inose
UNESCO: `Do you know AI or AI knows you better? Thinking Ethics of AI'
Toward AI-embedded Society where AI is Not Recognized as AI
AI (Artificial Intelligence) and Philosophy
Mechanical engineers and electrical engineers have different mental models of oscillation
Difference between Science and Engineering
What an old AI researcher thinks after watching the movie "Green Book" - about Racism, Discrimination, and AI (Artificial Intelligence)
The University of Tokyo Academic Archives Portal - UTokyo Digital Collections
AI ELSI Award
Civilization, Culture, Science, and Technology
AI support for Ethical AI Design
Koichi Hori Top page
Koichi Hori: Last Lecture
Koichi Hori
What is Artificial Intelligence?