最近、DuoLingoというアプリを使って、フランス語、ドイツ語、およびイタリア語を、すごく楽しく勉強しています。「これは何と言うんだっけ?」と思って、Google翻訳を使って、フランス語やドイツ語やイタリア語を調べることが、しばしば、あります。でも、同じ表現を、いちいち、フランス語とドイツ語とイタリア語に翻訳しなおすのが面倒なんですよね。そこで、Google翻訳のAPIを呼び出して、自動的に、3ヶ国語に翻訳してくれるようなプログラムを書こうと思いました。
なんだかPythonには、もう飽きてきたので、すごく久しぶりにLispで書きたくなりました。僕みたいな老人AI研究者は、昔々、Lispでたくさんプログラムを書いたものでした。昔を思い出して楽しんでみたいと思います。
まずは、最近の生成AIに、そういうプログラムをCommon Lispで書いてもらってみました。しかし、残念ながら、出てきたプログラムは、どれも、動きません。重大な間違いがいくつかあるのを見つけまして、それらを、手で直しました。
そうして動くようになったCommon Lispのプログラムをひとつ、下に、載せておきます。
; 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.
関連項目(自動計算):
MeCabを使っていると、"'utf-8' codec can't decode byte 0xfa in position 0: invalid start byte" というエラーが起こる
Ruby on RailsでURLをredirectする
Google検索の結果にfaviconを表示する
WindowsのコマンドプロンプトでUnicodeを使う
WindowsでPythonを使う
モバイル端末のための css <pre> と <code>
FacebookボタンとTwitterボタンをきちんと並べる
長崎原爆の日
WindowsからMacにVNCで接続しようとするとlogin windowが固まる
UAV/UGV Autonomous Cooperation
五七五, 五七五七七などの俳句や短歌みたいな語列をテキストから抽出するプログラム
文化国家としての技術立国 ー 猪瀬博教授の思い出
UNESCO: `Do you know AI or AI knows you better? Thinking Ethics of AI'
人工知能として認識されない人工知能の埋め込まれる社会に向けて
AI(人工知能)と哲学
機械屋と電気屋では振動のメンタルモデルが異なる
工学と理学の違い
映画「グリーンブック」を観て、年寄り人工知能研究者が考えること - 人種その他の差別とAI(人工知能)について
東京大学学術資産等アーカイブズポータル ー UTokyo Digital Collections
AI ELSI賞
文明と文化と科学と技術
倫理的AIの設計を支援するためのAI
堀 浩一 ホーム
堀 浩一: 最終講義
堀 浩一
人工知能とは