2014-01-14

Romanizing Chinese Characters From the Command Line

Often, I am reading some tidbit of Chinese on G+ or Facebook, and I come across a character I don't know. Since I'm such a great student, I first look at the various radicals it is made up of and try to guess what it means and sounds like. Using the surrounding context I can guess the meaning about 50% of the time and the correct sound about 25% of the time.

Since I have such dismal guessing rates I use various programs to check myself, before I wreck myself. If I am on the go, I draw the character with my finger in Pleco. Otherwise, I may try my guessed sound via pinyin. Both methods are slow and error prone. So instead, I often copy and paste the character into Google Translate. There are times when I do not have Internet, or I don't want to fire up a web browser. When that happens I utilize a nifty Perl module by Yusuke Kawasaki called Lingua::ZH::Romanize::Pinyin. I put the following one liner into my bashrc as an alias.

alias romanize="perl -MLingua::ZH::Romanize::Pinyin -E 'say Lingua::ZH::Romanize::Pinyin->new->chars(\$ARGV[0]);'"

This allows me to use the command line like below.
user@system:~$ romanize 骆驼
luo tuo

It is as simple as that! Not only does he have a module for Chinese, but also modules for Korean and Japanese.

No comments:

Post a Comment