Saturday, March 26, 2011

Tcl and Greek

While some may say that Tcl looks like Greek to them, I have Tcl doing my Greek homework!


This is a window with just an single line text entry. It is built with the following code:

toplevel .t
pack [entry .t.e]
bind .t.e <return> {
set s [.t.e get]
.t.e delete 0 end
.t.e insert 0 [revGreek $s]
}
When I hit enter, I get:


My English/Greek transliteration uses the "_" character as an escape (to handle the two letter English versions, and s/s at the end). The revGreek function is a simple state machine to check for _, and build the escaped sequence. Everything is passed through a dictionary which maps English sequences to Greek letters.

Here is the Greek to English map:
set greekDict {α a β b γ g δ d ε e ζ z η _ae θ th ι i κ k λ l μ m ν n ξ x ο o π p ρ r σ s ς _s τ t υ u φ _ph χ _ch ψ _ps ω _oe}

Greek can be converted to English with a simple function:
string map $greekDict $s

This is because each Greek letter is all by itself.

Building the English to Greek map is easy:
foreach {g e} $greekDict {
dict lappend revGreekDict $e $g
}

Where the Greek to English map is a simple list (where the first subelement is one Greek letter, and the second element is the English transliteration), the use of multiple English characters requires a full map (or "dict" in Tcl).

No comments: