Skip to content

Instantly share code, notes, and snippets.

@ayato-p
Forked from valvallow/mtran.scm
Created May 7, 2013 03:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayato-p/5529997 to your computer and use it in GitHub Desktop.
Save ayato-p/5529997 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/gosh
(use sxml.ssax)
(use sxml.sxpath)
(use rfc.http)
(use rfc.uri)
(use gauche.parseopt)
(use gauche.charconv)
(define api-key "SET YOUR API-KEY INTO THIS!")
(define (usage)
(print "Usage: mtran [options ...] text")
(print " - f|from : from lang")
(print " - t|to : to lang")
(print " - s|swap : swap 'from lang' to 'to lang'")
(print " - h|help : usage")
(exit 2))
(define (print-languages)
(print "English - en")
(print "Japanese - ja")
(print "French - fr")
(exit 2))
(define (call-translator-api text :optional (from "en")(to "ja"))
(receive (status head body)
(http-get "api.microsofttranslator.com"
(string-append
"/V2/Http.svc/Translate?appid=" api-key
"&from=" from
"&to=" to
"&text=" text))
(ssax:xml->sxml (open-input-string body) '())))
(define (main args)
(let-args (cdr args)
((from "f|from=s" "en")
(to "t|to=s" "ja")
(swap "s|swap")
(lang "l|languages" => print-languages)
(help "h|help" => usage)
(else (opt . _)
(print "Unknown option : " opt)
(usage))
. rest)
(let ((text (if (null? rest)
(port->string (current-input-port))
(car rest))))
(when swap
(let1 temp from
(set! from to)
(set! to temp)))
(let1 ret ((sxpath "/*/text()")
(call-translator-api (uri-encode-string text) from to))
(print (if (null? ret)
"something wrong."
(car ret)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment