Open Chinese Convert(OpenCC)是一個開源的中文簡繁轉換項目,致力於製作高質量的基於統計預料的簡繁轉換詞庫,還提供函數庫(libopencc)、命令行簡繁轉換工具、人工校對工具、詞典生成程序、在線轉換服務及圖形用戶界面。
現在OpenCC支持Node.js了。
安裝方法
npm install opencc
NPM: https://npmjs.org/package/opencc
Github: https://github.com/BYVoid/OpenCC
在線轉換: http://opencc.byvoid.com/ (代碼 https://github.com/BYVoid/opencc-web )
使用方法
var OpenCC = require('opencc');
// Load the default Simplified to Traditional config
var opencc = new OpenCC('zhs2zht.ini');
// Set conversion mode
opencc.setConversionMode(OpenCC.CONVERSION_FAST);
// Sync API
var converted = opencc.convertSync("汉字");
console.log(converted);
// Async API
opencc.convert("汉字", function (err, converted) {
console.log(converted);
});
16 回复
看来有用。不过安装macosx出错
make: *** No rule to make target `Release/opencc_dict', needed by `Release/simp_to_trad_characters.ocd'. Stop.
gyp ERR! build error
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack at ChildProcess.onExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:236:23)
gyp ERR! stack at ChildProcess.EventEmitter.emit (events.js:96:17)
gyp ERR! stack at Process._handle.onexit (child_process.js:678:10)
gyp ERR! System Darwin 12.3.0
gyp ERR! command "node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /Users/yliu0/dev/node_modules/opencc
gyp ERR! node -v v0.8.12
gyp ERR! node-gyp -v v0.7.0
gyp ERR! not ok
npm ERR! [email protected] install: `node-gyp rebuild`
npm ERR! `sh "-c" "node-gyp rebuild"` failed with 1
繁体 -> 简体
yliu0@u12:~/dev/test/node-module/opencc$ cat test2.js
var OpenCC = require('opencc');
// Load the default Simplified to Traditional config
var opencc = new OpenCC('zht2zhs.ini');
// Set conversion mode
opencc.setConversionMode(OpenCC.CONVERSION_FAST);
// Sync API
var converted = opencc.convertSync("漢字");
console.log(converted);
// Async API
opencc.convert("漢字", function (err, converted) {
console.log(converted);
});
yliu0@u12:~/dev/test/node-module/opencc$ node test2.js
汉字
汉字
yliu0@u12:~/dev/test/node-module/opencc$
就是文件名,opencc的node包僅僅是對libopencc進行了簡單的封裝,libopencc內置了許多配置文件,見 https://github.com/BYVoid/OpenCC/tree/master/data/config