OpenCC 字典格式轉換教程:TXT、OCD、OCD2 完整指南
OpenCC 是常用的繁簡轉換工具,其字典主要有三種格式:
- TXT(可讀可編輯)
- OCD(舊版字典格式)
- OCD2(新版字典格式)
本文介紹所有格式之間的互相轉換方法,適用於 Linux、Ubuntu、Debian、CentOS、Alibaba Cloud Linux 等服務器。
📌 一、檢查 opencc 與 opencc_dict 是否安裝
✔ 查看 OpenCC 版本:
opencc -v
✔ 檢查是否存在 opencc_dict:
opencc_dict -h
如果提示 command not found → 你的系統沒有安裝字典工具。
📌 二、TXT → OCD2(新版格式)
TXT 文件必須用 TAB 分隔:
麼 么
裏 里
麼麼 么么
使用以下命令轉換為 ocd2:
opencc_dict -i dict.txt -o dict.ocd2 -f text -t ocd2
📌 三、OCD2 → TXT
將字典從 ocd2 解壓回 txt:
opencc_dict -i dict.ocd2 -o dict.txt -f ocd2 -t text
📌 四、TXT → OCD(舊格式)
opencc_dict -i dict.txt -o dict.ocd -f text -t ocd
📌 五、OCD → TXT(舊格式)
opencc_dict -i dict.ocd -o dict.txt -f ocd -t text
📌 六、如果你的系統沒有 opencc_dict
不同系統安裝方法不同。
✔ Ubuntu / Debian
sudo apt install opencc-tools
或:
sudo apt install libopencc-tools
✔ CentOS / Alibaba Cloud Linux / Anolis OS
sudo yum install opencc-tools
若無該包,需要源碼編譯:
📌 七、源碼編譯 opencc_dict(通用方法)
① 安裝必要環境
sudo yum install -y gcc gcc-c++ cmake git make
或 Ubuntu:
sudo apt install -y gcc g++ cmake git make
② 下載源碼
git clone https://github.com/BYVoid/OpenCC.git
cd OpenCC
③ 編譯
make
④ 安裝
sudo make install
⑤ 添加軟鏈接
sudo ln -s /usr/local/bin/opencc_dict /usr/bin/opencc_dict
📌 八、常見問題解答
❓ TXT 轉換出錯:“Tabular not found”
原因:使用了空格,應改為 TAB。
錯 錯 ❌(錯誤,空格)
錯 錯 ✔(正確,TAB)
❓ TXT 第一行亂碼?(BOM 問題)
sed -i '1s/^\xEF\xBB\xBF//' dict.txt
❓ JSON 無法加載 ocd / ocd2?
檢查 JSON 中的 file 路徑是否一致。
📌 九、轉換命令總結(速查表)
| 操作 | 命令 |
|---|---|
| TXT → OCD2 | opencc_dict -i t.txt -o t.ocd2 -f text -t ocd2 |
| OCD2 → TXT | opencc_dict -i t.ocd2 -o t.txt -f ocd2 -t text |
| TXT → OCD | opencc_dict -i t.txt -o t.ocd -f text -t ocd |
| OCD → TXT | opencc_dict -i t.ocd -o t.txt -f ocd -t text |
📌 十、總結
通過本教程,你可以輕鬆實現:
- TXT ↔ OCD(舊格式)
- TXT ↔ OCD2(新格式)
- 通過源碼方式安裝 opencc_dict 工具
發布者:彬彬筆記,轉載請註明出處:https://www.binbinbiji.com/zh-hant/linux/3780.html