Skip to content

Instantly share code, notes, and snippets.

@mingsai
Created March 23, 2014 23:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mingsai/9731262 to your computer and use it in GitHub Desktop.
Save mingsai/9731262 to your computer and use it in GitHub Desktop.
Convert Chinese Characters to Pinyi
This script is part two of a solution to convert Chinese characters to pinyin. Please note the reference link for part one (a simple utility to read the Chinese dictionaries on MacOSX).
To create:
Start a new Automator Service project
Add a Run Shell Script action
Change Pass input menu to "as arguments"
Paste in the script above (requires that you add rdef utility to the /usr/local/bin)
Save your Automator service as Convert to Pinyin
To use:
Right click any Chinese characters on your MacOSX
Select Services sub menu (at the bottom of the context menu)
Select Convert to PinYin
Result:
You will see Chinese text converted to pinyin
e.g. 第二次世界大战正在迫近,
becomes dì èr cì shì jiè dà zhàn zhēng zài pò jìn
Reference:
Utility to get definition of a Chinese character output to the MacOSX Terminal - https://github.com/mingsai/rdef
Full Solution - http://stackoverflow.com/questions/22580539/looking-for-a-terminal-command-to-parse-macosx-dictionary-data-file
# Assumes you have rdef utility installed in /usr/local/bin
# rdef can be found here: https://github.com/mingsai/rdef
# See accompanying readme.txt for instructions to create MacOSX service
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin/:
export PATH
LC_CTYPE=UTF-8
x=$1
for ((i=0;i<${#x};i++)); do rdef "${x:i:1}" | awk -F '\|' 'BEGIN {ORS=" "}{ gsub(/^ | +?/, "", $2); if (length($2) > 0) print $2 ; exit}'; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment