Paste: google.translate
Author: | erg |
Mode: | factor |
Date: | Wed, 16 Mar 2011 16:20:50 |
Plain Text |
USING: assocs http.client json.reader kernel namespaces
sequences combinators grouping fry ;
IN: google.translate
CONSTANT: google-translate-url "http://ajax.googleapis.com/ajax/services/language/translate"
CONSTANT: maximum-translation-size 5120
: parameters>assoc ( text from to -- assoc )
"|" glue [
[ "q" set ] [ "langpair" set ] bi*
"1.0" "v" set
] { } make-assoc ;
: assoc>query-response ( assoc -- response )
google-translate-url http-post nip ;
ERROR: response-error response error ;
: throw-response-error ( response -- * )
"responseDetails" over at response-error ;
: check-response ( response -- response )
"responseStatus" over at {
{ 200 [ ] }
{ 400 [ throw-response-error ] }
[ drop throw-response-error ]
} case ;
: query-response>text ( response -- text )
json> check-response
"responseData" swap at
"translatedText" swap at ;
: (translate) ( text from to -- text' )
parameters>assoc
assoc>query-response
query-response>text ;
: translate ( text from to -- text' )
[ maximum-translation-size group ] 2dip
'[ _ _ (translate) ] map concat ;
New Annotation