A little Hangul converter


WikiDbImage hangul.jpg

Richard Suchenwirth 2002-07-10 - Here is a tiny tool that may be helpful when dealing with Korean Hangul \ characters. You can paste in Hangul strings into the upper entry, and convert it to (approximated) Hanglish transliteration with <Return>; or type Hanglish into the bottom entry, and convert it with <Return> to Hangul if possible. You can also visualize any Unicode in the \u.... notation, as the example shows.

proc ui {} {
    set font {Helvetica 14}
    label .1 -text Hangul:
    entry .2 -textvar ::hangul -font $font
    label .3 -text =
    entry .4 -textvar ::hanglish

    bind .2 <Return> {set ::hanglish [hangul2hanglish $::hangul]}
    bind .4 <Return> {set ::hangul   [hanglish $::hanglish]}

    grid .1 .2 -sticky news
    grid .3 .4 -sticky news
    grid columnconfigure . 1 -weight 1
}
proc hangul2hanglish s {
    set lead {g gg n d dd r m b bb s ss "" j jj c k t p h}
    set vowel {a ae ya yai e ei ye yei o oa oai oi yo u ue uei ui yu w wi i}
    set tail {"" g gg gs n nj nh d l lg lm lb ls lt lp lh m b bs s ss 
        ng j c k t p h}
    set res ""
    foreach c [split $s ""] {
        scan $c %c cnum
        if {$cnum>=0xAC00 && $cnum<0xD7A3} {
            incr cnum -0xAC00
            set l [expr {$cnum / (28*21)}]
            set v [expr {($cnum/28) % 21}]
            set t [expr {$cnum % 28}]
            append res  [lindex $lead $l ]
            append res  [lindex $vowel $v]
            append res "[lindex $tail $t] "
        } else {append res $c}
    }
    set res
}
proc hanglish2uc hanglish {
    set L ""; set V "" ;# in case regexp doesn't hit
    set hanglish [string map {
        AE R SH S R L NG Q YE X YAI F AI R YA V YO Y YU Z VI F
    } [string toupper $hanglish]]
    regexp {^([GNDLMBSQJCKTPH]+)?([ARVFEIXOYUZW]+)([GNDLMBSQJCKTPH]*)$} \
            $hanglish ->  L V T ;# lead cons.-vowel-trail cons.
        if {$L==""} {set L Q}
    if {$V==""} {return $hanglish}
    set l [lsearch {G GG N D DD L M B BB S SS Q J JJ C K T P H} $L]
    set v [lsearch {A R V F E EI X XI O OA OR OI Y U UE UEI UI Z W WI I} $V]
    set t [lsearch {"" G GG GS N NJ NH D L LG LM LB LS LT LP LH  \
            M B BS S SS Q J C K T P H} $T] ;# trailing consonants
    if {[min $l $v $t] < 0} {return $hanglish}
    format %c [expr {$l*21*28 + $v*28 + $t + 0xAC00}]
}
proc min args {lindex [lsort -real $args] 0}
proc hanglish argl {
    set res ""
    foreach i $argl {
        foreach j [split $i -] {append res [hanglish2uc $j]}
    }
    append res " "
}
ui

After downloading a Hangul font, it only took to change the second line of ui to

set font {"Baekmuk Dotum" 13 bold}

to port the above code onto my iPAQ ;-)