Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Restore translation, and switch to v2 of the API

  • Loading branch information...
commit 79212f5abbcf3288faf33fdfd4d1139466f45014 1 parent e241467
@offby1 authored
Showing with 181 additions and 0 deletions.
  1. +4 −0 irc-process-line.rkt
  2. +177 −0 xlate.rkt
View
4 irc-process-line.rkt
@@ -11,6 +11,7 @@
"git-version.rkt"
"userinfo.rkt"
"utils.rkt"
+ (except-in "xlate.rkt" main)
(except-in "spelled-out-time.rkt" main)
(except-in "quotes.rkt" main)
(except-in "re.rkt" main)
@@ -432,6 +433,9 @@
(describe-since *start-time*)
(describe-since (*connection-start-time*))))
+(defverb #:whine (t8 from to text ...) "translate TEXT from FROM to TO"
+ (reply (xlate from to (string-join text " "))))
+
(defverb #:hidden (ping) "am I alive?"
(reply "pong"))
View
177 xlate.rkt
@@ -0,0 +1,177 @@
+#! /bin/sh
+#| Hey Emacs, this is -*-scheme-*- code!
+exec racket -l errortrace --require "$0" --main -- ${1+"$@"}
+|#
+
+#lang racket
+(require net/url
+ (planet dherman/json:3:0)
+ (planet schematics/schemeunit:3)
+ (planet schematics/schemeunit:3/text-ui))
+
+(provide xlate t8 main)
+
+;; The returned data sometimes has HTML entities in it; the functions
+;; on this page translate those to regular characters.
+
+;; From http://htmlhelp.com/reference/html40/entities/special.html
+(define entity-integers-by-name
+ #hash(
+ ("quot" . 34)
+ ("amp" . 38)
+ ("lt" . 60)
+ ("gt" . 62)
+ ("OElig" . 338)
+ ("oelig" . 339)
+ ("Scaron" . 352)
+ ("scaron" . 353)
+ ("Yuml" . 376)
+ ("circ" . 710)
+ ("tilde" . 732)
+ ("ensp" . 8194)
+ ("emsp" . 8195)
+ ("thinsp" . 8201)
+ ("zwnj" . 8204)
+ ("zwj" . 8205)
+ ("lrm" . 8206)
+ ("rlm" . 8207)
+ ("ndash" . 8211)
+ ("mdash" . 8212)
+ ("lsquo" . 8216)
+ ("rsquo" . 8217)
+ ("sbquo" . 8218)
+ ("ldquo" . 8220)
+ ("rdquo" . 8221)
+ ("bdquo" . 8222)
+ ("dagger" . 8224)
+ ("Dagger" . 8225)
+ ("permil" . 8240)
+ ("lsaquo" . 8249)
+ ("rsaquo" . 8250)
+ ("euro" . 8364)
+ ))
+
+(define (replace-html-entities str)
+ (define (numeric str)
+ (regexp-replace*
+ #px"&#([0-9]+);"
+ str
+ (lambda (whole-match digits)
+ (string (integer->char (string->number digits))))))
+ (define (named str)
+ (regexp-replace*
+ #px"&([a-z]+);"
+ str
+ (lambda (whole-match word)
+ (let ([replacement (hash-ref entity-integers-by-name word #f)])
+ (if replacement
+ (format "&#~a;" replacement)
+ str)))))
+ (numeric (named str)))
+
+(define-test-suite replace-tests
+ (check-equal?
+ (replace-html-entities "")
+ "")
+
+ (check-equal?
+ (replace-html-entities "frotz")
+ "frotz")
+
+ (check-equal?
+ (replace-html-entities "&frotz;")
+ "&frotz;")
+
+ (check-equal?
+ (replace-html-entities "&")
+ "&")
+
+ (check-equal?
+ (replace-html-entities ""plonk"")
+ "\"plonk\"")
+
+ (check-equal?
+ (replace-html-entities "frotzM")
+ "frotzM")
+
+ (check-equal?
+ (replace-html-entities "frotz{why notA")
+ "frotz{why notA"))
+
+
+;; Translate text using Google's translation API v2.
+
+(define (snag text from to)
+
+ (call/input-url
+ (make-url
+ "https" ;; scheme
+ #f ;; user
+ "www.googleapis.com" ;; host
+ #f ;; port
+ #t ;; path-absolute?
+
+ ;; path
+ (map ((curryr make-path/param) '()) (list "language" "translate" "v2"))
+
+ ;; query
+ `([key . ,(bytes->string/utf-8 (get-preference 'google-API-key))]
+ [q . ,text]
+ [target . ,to])
+ #f ;; fragment
+ )
+ get-pure-port
+ read-json))
+
+(define-test-suite snag-tests
+ (check-equal?
+ (hash-ref
+ (hash-ref
+ (snag "print \"hello, world\\n\"" "perl" "java")
+ 'error)
+ 'message)
+ "Invalid Value"))
+
+;; List of language codes, to "from" and "to":
+;; http://code.google.com/apis/ajaxlanguage/documentation/reference.html#LangNameArray
+
+(define (xlate from to text)
+ (let* ([stuff (snag text from to)]
+ [data (hash-ref stuff 'data #f)])
+ (cond
+ [data
+ (replace-html-entities
+ (hash-ref
+ (first
+ (hash-ref
+ (hash-ref
+ stuff
+ 'data)
+ 'translations))
+ 'translatedText))]
+ [else
+ (hash-ref (hash-ref stuff 'error) 'message)])))
+
+(define t8 xlate)
+
+(define-test-suite xlate-tests
+
+ (check-equal?
+ (xlate "en" "it" "forty-five separate amendments")
+ "40-cinque emendamenti separati")
+
+ (check-equal?
+ (xlate "en" "fr" "fledermaus: have I rubbed this in your face yet?")
+ "Chauve-souris: je n'ai frotté dans votre visage, mais?")
+
+ (check-equal?
+ "Invalid Value"
+ (xlate "frotz" "plotz" "I doubt this will get translated properly")))
+
+(define-test-suite all-tests
+ replace-tests
+ snag-tests
+ xlate-tests)
+
+(define (main . args)
+ (exit (run-tests all-tests 'verbose)))
Please sign in to comment.
Something went wrong with that request. Please try again.