顯示具有 cmake 標籤的文章。 顯示所有文章
顯示具有 cmake 標籤的文章。 顯示所有文章

2012年7月14日

使用opencc in python繁體(正體)簡體中文轉換

opencc看起來是個很棒的open source project
但是安裝opencc時遇到許多死路,正確做法筆記如下

環境
mac 10.6.8
ubuntu 12.04 LTS
cmake 2.8.8

到opencc下載對應的opencc檔案

準備編譯...
如果沒有cmake
在mac上下載並安裝cmake 的dmg 或source
在ubuntu上安裝cmake
sudo apt-get update sudo apt-get install opencc opencc --version

安裝opencc command line
按照opencc/INSTALL的方法安裝

1. Make a directory and check in. mkdir build cd build 2. Build sources. cmake -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE=Release -D ENABLE_GETTEXT:BOOL=ON .. make 3. Install. sudo make install
在cmake編譯時如果遇到Error missing: GETTEXT_MSGMERGE_EXECUTABLE 請改用cmake GUI編譯

然後下載distribute_setup.py至site-packages
升級distribute後
python distribute_setup.py which easy_install whcih pip 安裝opencc python wrapper
pip install distribute -U pip install opencc-python 
把opencc執行檔複製到wrapper對應位置
  cp /usr/local/bin/opencc /venv/lib/python2.7/site-packages/opencc/bin/ 
(也可能在/usr/bin/opencc)
接下來就可以使用了
python >>> import opencc >>> cc = opencc.OpenCC('s2t') >>> cc.convert(u'\u9093') u'\u9129'
如果出現
OSError: [Errno 2] No such file or directory
代表你的執行檔不在site-packages/opencc/bin裡面
然後裡面的exe和dll檔可以砍了