{"id":15645936,"url":"https://github.com/oyyd/wasm-opencc","last_synced_at":"2025-04-15T16:58:40.957Z","repository":{"id":41810181,"uuid":"148776731","full_name":"oyyd/wasm-opencc","owner":"oyyd","description":"OpenCC compiled by Emscripten so that you can run it on browsers or nodejs without compiling.","archived":false,"fork":false,"pushed_at":"2023-03-02T07:48:44.000Z","size":13314,"stargazers_count":56,"open_issues_count":11,"forks_count":7,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-15T16:58:35.057Z","etag":null,"topics":["chinese-translation","emscripten","opencc","simplified-chinese","traditional-chinese","webassembly"],"latest_commit_sha":null,"homepage":"https://oyyd.github.io/wasm-opencc/","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/oyyd.png","metadata":{"files":{"readme":"README.md","changelog":"NEWS.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":"AUTHORS","dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-09-14T10:59:32.000Z","updated_at":"2025-03-14T14:39:15.000Z","dependencies_parsed_at":"2024-10-23T06:34:52.210Z","dependency_job_id":null,"html_url":"https://github.com/oyyd/wasm-opencc","commit_stats":{"total_commits":767,"total_committers":54,"mean_commits":"14.203703703703704","dds":0.6883963494132985,"last_synced_commit":"5775b4bdceb0f453eab7436136449c39f63a9b1a"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oyyd%2Fwasm-opencc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oyyd%2Fwasm-opencc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oyyd%2Fwasm-opencc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oyyd%2Fwasm-opencc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/oyyd","download_url":"https://codeload.github.com/oyyd/wasm-opencc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249116230,"owners_count":21215142,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chinese-translation","emscripten","opencc","simplified-chinese","traditional-chinese","webassembly"],"created_at":"2024-10-03T12:10:38.545Z","updated_at":"2025-04-15T16:58:40.935Z","avatar_url":"https://github.com/oyyd.png","language":"C++","funding_links":[],"categories":["C++"],"sub_categories":[],"readme":"# wasm-opencc\n\n[![npm-version](https://img.shields.io/npm/v/wasm-opencc.svg?style=flat-square)](https://www.npmjs.com/package/wasm-opencc)\n[![travis-ribbon](https://travis-ci.com/oyyd/wasm-opencc.svg?branch=master)](https://travis-ci.com/oyyd/wasm-opencc#)\n\nwasm-opencc开放中文转换[OpenCC](https://github.com/BYVoid/OpenCC)的wasm版本。\n\n这个项目对OpenCC进行了添加修改修改，并利用[Emscripten](https://github.com/kripken/emscripten)进行编译，在OpenCC进行中文简繁体转换的能力上具有以下特性：\n\n- 可在浏览器环境中直接运行。（wasm）\n- 在node，eletron中运行不需要再进行addon编译，避免复杂的addon部署工作。理论上应该也可以在React Native和Web Worker中运行(未经测试)。（wasm）\n- 分离了字典数据的加载和文本转换功能，在浏览器中只加载必要的字典数据，并允许自定义数据加载方式，方便从CDN上加载数据。\n\n## 安装\n\n对于浏览器环境，请直接拷贝dist文件夹中的文件并在浏览器中加载，**注意请一并拷贝dist文件夹下的.mem文件**，该.mem文件为代码运行的必要文件。\n\n```html\n\u003c!DOCTYPE html\u003e\n\u003chtml\u003e\n  \u003chead\u003e\u003c/head\u003e\n  \u003cbody\u003e\n    \u003cscript src=\"./opencc-asm.js\"\u003e\u003c/script\u003e\n    \u003cscript\u003e\n      const { DictSource, Converter } = OpenCCWasm_\n      OpenCCWasm_.ready().then(() =\u003e {\n        // 获取s2t.json字典数据\n        const dictSource = new DictSource('s2t.json');\n        return dictSource.get();\n      }).then((args) =\u003e {\n        const converter = new Converter(...args)\n        console.log(converter.convert('繁体'))\n        // 注意当不再需要使用converter时，请调用delete方法以释放内存\n        converter.delete()\n      })\n    \u003c/script\u003e\n  \u003c/body\u003e\n\u003c/html\u003e\n```\n\n相关文件大小（不包含字典文件）：\n\n- opencc-asm.js (655kB, gzip后164kB)\n- opencc-asm.js.mem (25kB, gzip后8kB)\n\n当前项目中的wasm代码，实际上是asmjs版本，并非用于浏览器原生WebAssembly的版本。asmjs的版本兼容性更好，WebAssembly版本体积更小（约在在250kB左右）。\n\n对于node环境，可以直接利用npm安装:\n\n```\n$ npm i -d wasm-opencc\n```\n\n安装后加载使用：\n\n```js\nconst { DictSource, Converter } = require('wasm-opencc')\nconst dictSource = new DictSource('s2t.json');\n\ndictSource.get().then((args) =\u003e {\n  const converter = new Converter(...args)\n  console.log(converter.convert('繁体'))\n  // 注意當不再需要使用converter時，請調用delete方法以釋放內存\n  converter.delete()\n})\n```\n\n注意OpenCC本身具有[Node Addon版本](https://www.npmjs.com/package/opencc)，请根据自己的需要选择。\n\n## API\n\n### ready()\n\n在浏览器上，请先调用ready函数，并在结束后再进行`Converter`的相关操作。在Node上不需要等待ready函数结束，直接使用即可。\n\n### class DictSource\n\n#### new DictSource(string dictName)\n\n`DictSource`用于获取字典数据，以初始化`Converter`。如果你熟悉OpenCC中Converter所需要的数据格式，和数据构成结构，你也可以完全避开`DictSource`，直接把字典数据的字符串传递给Converter。\n\n`dictName`所接受的参数请参照：[預設配置文件](https://github.com/BYVoid/OpenCC#configurations-%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6)\n\n#### DictSource.get()\n\n`DictSource.get()`默认会根据运行环境不同，采用不同的方式获取数据。\n\n`DictSource.get()`返回一个Promise，这个Promise在resolve时会返回构建`Converter`所需要的参数的数组，当获取字典数据失败时会reject。返回参数的意义请参照`new Converter()`。\n\n#### DictSource.setDictProxy(function proxy)\n\n自定义获取数据的函数:\n\n```js\nconst dictSource = new DictSource('s2t.json')\n\ndictSource.setDictProxy((dictName) =\u003e {\n  // proxy需要返回一个promise\n  return Promise.resolve('僞\\t偽\\n')\n})\n\ndictSource.get() // 会调用proxy函数\n```\n\nDictSource会给`proxy`函数传入所需要的字典名称，而`proxy`函数需要返回一个promise以告知DictSource请求结束或请求失败。成功的话需要resolve对应的字典数据内容，失败的话请reject一个`Error`对象。\n\n### class Conveter\n\n#### new Converter(string semgentation, Array\u003cArray\u003cstring\u003e|string\u003e convertionChain)\n\n`Converter`用来进行文本转换。\n\n其中semgentation为分词用的数据；convertionChain按顺序定义了转换时所使用的一系列数据字典。\n\n#### Converter.convert(string text)\n\n`Converter.convert`进行文本转换操作，同步操作。\n\n#### Converter.delete()\n\n销毁该实例在wasm中对应对象的实例。**请注意：在不需要使用converter以后，一定要手动调用delete方法销毁内存。**\n\n目前要求用户手动调用delete这点不可避免，这是Embind和目前wasm机制所决定的，可参照[Embind -- Memory Management](https://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/embind.html#memory-management)。\n\n## 已知问题\n  - 暂不接受`.ocd`类型的字典数据\n  - uglifyjs相关问题：在开发过程中发现，用uglifyjs处理wasm编译的代码后，会导致其在Chrome上无法正常运行（但在safari上可以正常运行）。个人猜测是Chrome引发的问题。\n  - 因为上述原因，加之编译后代码本身的一些问题，作者对于在浏览器中运行的文件进行了一些自定义的处理并进行了bundle。如果你的项目要运行在浏览器环境上的话，不建议你对wasm-opencc自己进行bundle。\n\n## 其他可能会实行的计划\n  - 进行Benchmark，探究Cpp版本，Node Addon版本和wasm版本之间的性能差距。\n  - 提供WebAssembly版本。\n  - 用closure进行编译提高效率。\n\n## Contribute\n\n请先安装Emscripten和OpenCC所需依赖，然后：\n\n进行Emscripten编译代码：\n\n```\nmake -f WasmMakefile\n```\n\n构建js相关代码：\n\n```\ncd wasm \u0026\u0026 npm run build\n```\n\n构建文档：\n\n```\ncd wasm \u0026\u0026 npm run docs\n```\n\n运行测试：\n\n```\ncd wasm \u0026\u0026 npm run test\n```\n\n## License\n\n这个项目在OpenCC的基础上添加了`/src/wasm`文件夹下的代码。原项目中多个文件夹下的CMakeLists.txt都被进行了一定程度上的修改。wasm相关的js代码主要放在`/wasm`文件夹下，为新增代码。`/docs`中的代码用于gh-pages展现文档。\n\n[Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0.html)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foyyd%2Fwasm-opencc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foyyd%2Fwasm-opencc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foyyd%2Fwasm-opencc/lists"}