{"id":13713425,"url":"https://github.com/breezewish/pinyinlite","last_synced_at":"2025-12-12T04:21:52.156Z","repository":{"id":41278017,"uuid":"51123939","full_name":"breezewish/pinyinlite","owner":"breezewish","description":"Lightweight and Lightning-Fast :zap: Pinyin Library for JavaScript","archived":false,"fork":false,"pushed_at":"2017-08-30T03:01:56.000Z","size":268,"stargazers_count":235,"open_issues_count":5,"forks_count":31,"subscribers_count":9,"default_branch":"master","last_synced_at":"2024-12-12T03:36:06.102Z","etag":null,"topics":["javascript","npm","npm-package","pinyin"],"latest_commit_sha":null,"homepage":"https://github.com/breeswish/pinyinlite","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/breezewish.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-02-05T03:35:09.000Z","updated_at":"2024-11-30T05:29:45.000Z","dependencies_parsed_at":"2022-09-04T11:50:29.861Z","dependency_job_id":null,"html_url":"https://github.com/breezewish/pinyinlite","commit_stats":null,"previous_names":["breeswish/pinyinlite","summerwish/pinyinlite"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/breezewish%2Fpinyinlite","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/breezewish%2Fpinyinlite/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/breezewish%2Fpinyinlite/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/breezewish%2Fpinyinlite/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/breezewish","download_url":"https://codeload.github.com/breezewish/pinyinlite/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230415318,"owners_count":18222158,"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":["javascript","npm","npm-package","pinyin"],"created_at":"2024-08-02T23:01:35.887Z","updated_at":"2025-12-12T04:21:52.123Z","avatar_url":"https://github.com/breezewish.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# pinyinlite\n\n[![Build Status](https://travis-ci.org/breeswish/pinyinlite.svg?branch=master)](https://travis-ci.org/breeswish/pinyinlite) [![Coverage Status](https://coveralls.io/repos/github/breeswish/pinyinlite/badge.svg?branch=master)](https://coveralls.io/github/breeswish/pinyinlite?branch=master) [![Dependency Status](https://david-dm.org/breeswish/pinyinlite.svg)](https://david-dm.org/breeswish/pinyinlite) [![npm version](http://img.shields.io/npm/v/pinyinlite.svg?style=flat)](https://npmjs.org/package/pinyinlite \"View this project on npm\") [![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)\n\n​最快 :zap: 而又轻量的 JavaScript 拼音库，适合在前后端实现拼音输入匹配。\n\n- 内存占用最小（~ 2 MB）且最快（~ 10,000,000 字/s）的 JavaScript 拼音库。\n- 字典包含约 2.7 万简体繁体字，覆盖全面（常用汉字不到五千个）。\n- 体积小，gzip 后约 80 KB。\n- 另提供基于【现代汉语通用字表（2013）】的常用字版，包含约 8000 个汉字，gzip 后 25 KB。\n- 支持多音字。\n- 没有任何依赖项。\n\n## Usage\n\n### Node.js\n\n```bash\nnpm install pinyinlite\n```\n\n```js\nvar pinyinlite = require('pinyinlite');\npinyinlite('增长');\n// =\u003e [ [ 'ceng', 'zeng' ], [ 'zhang', 'chang' ] ]\n```\n\n### In Browsers\n\n```html\n\u003cscript charset=\"UTF-8\" src=\"dist/pinyinlite_full.min.js\"\u003e\u003c/script\u003e\n\u003cscript\u003e\nconsole.log(pinyinlite('世界你好'));\n// =\u003e [ [ 'shi' ], [ 'jie' ], [ 'ni' ], [ 'hao' ] ]\n\u003c/script\u003e\n```\n\n引用脚本时请务必保留 `charset=\"UTF-8\"`，否则在部分浏览器中会由于编码识别错误而产生问题。\n\n若要用体积更小的常用字版本，请改成引入 `dist/pinyinlite_common.min.js`。\n\n## Options\n\n```js\npinyinlite(str, options)\n```\n\n#### options.keepUnrecognized\n\n是否保留无法获得拼音的全角字符和半角标点符号，默认为 `false`，即不保留，相应位置将是空数组。\n\n字母和数字总是会原样输出。\n\n```js\npinyinlite('4C，测试');\n// =\u003e [ [ '4' ], [ 'C' ], [], [ 'ce' ], [ 'shi' ] ]\n\npinyinlite('4C，测试', {\n  keepUnrecognized: true\n});\n// =\u003e [ [ '4' ], [ 'C' ], [ '，' ], [ 'ce' ], [ 'shi' ] ]\n```\n\n## FAQ\n\n#### Q: 什么功能是不支持的?\n\n以下功能现在不支持、未来也不会支持：\n\n- 智能选择多音字的拼音\n- 音调\n\n以下功能现在不支持、未来可以支持：\n\n- 兼容 Unicode BMP 以外的汉字\n- 给出的拼音按照频率排序\n\n#### Q: 为什么不支持音调？\n\n这个拼音库用于提供基于拼音的搜索和匹配，例如实现支持拼音筛选的自动完成组件等，因此不需要音调。\n\n#### Q: 为什么不支持智能选择多音字？\n\n如上所述，这个拼音库用于提供基于拼音的搜索和匹配。在这种场景下，最重要的是确保用户输入的各种有效的拼音都能被对应到候选项上去，而不是尽可能地只使用一个准确的拼音去匹配用户的输入。\n\n使用准确拼音进行匹配是不现实的：\n\n- 例如专有名词中一般有固有发音，词典覆盖各种特殊情况是不现实的，另外带来的体积极度膨胀也是不科学的。\n- 用户很可能会输入不准确的拼音，例如地域、个人习惯、方言等因素会影响用户多音字的读音，因此即使程序生成了一个准确的拼音，将它与用户的输入进行“相似度匹配”也是不合理的，很可能会出现预期项被漏匹配的情况。\n\n#### Q: 我就想用音调或者智能多音字，有没有什么替代?\n\n请移步其他项目，例如 [hotoo/pinyin](https://github.com/hotoo/pinyin/)。\n\n#### Q: 为什么 Unicode BMP 以外的汉字没有支持?\n\n需求太小，懒得实现。若您的确需要完整的 UTF-16 支持，请开 issue。\n\n## Examples\n\n### 拼音模糊搜索 (examples/fuzzy-pinyin-search)\n\n对豆瓣 Top N 电影进行全拼或拼音首字母搜索。\n\n```bash\n$ cd examples/fuzzy-pinyin-search\n$ npm install\n\n$ node search.js zhw\n[ '指环王3：王者无敌', '指环王1：魔戒再现', '指环王2：双塔奇兵' ]\n\n$ node search.js \"zhw wangzhe\"\n[ '指环王3：王者无敌' ]\n\n$ node search.js agzz\n[ '阿甘正传' ]\n\n$ node search.js tian\n[ '天使爱美丽', '天空之城', '天堂电影院', '放牛班的春天', '泰坦尼克号' ]\n\n$ node search.js vzi   \n[ 'V字仇杀队' ]\n```\n\n实现原理：\n\n1. 对于每个候选项，生成它所有可能的读音组合(这个库的核心思想)，即通过 pinyinlite 生成各个字的所有候选读音，然后进行笛卡尔积。\n2. 使用 [string_score](https://github.com/joshaven/string_score) 对这些读音组合与用户的输入进行相似度匹配。\n3. 选择相似度最高的几个候选项输出。\n\n## 为什么这个库这么快这么小?\n\n太长不看版：合适的算法和数据结构、针对 JavaScript 引擎特性的定制优化。\n\n完整版：\n\n-   为了减小冗余，字典按照 `[[拼音,该拼音的所有候选字],..]` 存储在源文件中。当然这样的结构也使得 JavaScript 引擎能很快地解析文件。\n\n-   首次运行时对字典进行初始化。\n\n    1. 字典被变幻成以下两个数组：\n\n       ```js\n       char:     [汉 , 字,  世 , 界 , 你, 好 ,...]   // 内存中为 charCode\n       pinyin:   [han, zi, shi, jie, ni, hao,...]   // 内存中为 index\n       ```\n\n    2. 按照汉字 charCode 对两个数组同步地进行排序。另外存储在源文件中的字典是有序的，可降低排序量。排序后，多音字会变成邻居，结构类似于下面这样：\n\n       ```js\n       char:     [世 ,  界 , 降   , 降   , 你, 好 , ...]\n       pinyin:   [shi, jie, jiang, xiang, ni, hao, ...]\n                            ^^^^^^^^^^^^\n       ```\n\n    3. 对汉字进行紧缩并记录拼音区域，减少将来的搜索量：\n\n       ```js\n       char:     [世 ,  界 , 降          , 你, 好 , ...]\n       pinyin:   [shi, jie, jiang, jiang, ni, hao, ...]\n       position: [0  , 1  , 3           , 4 , 5  , ...]\n                            ^^^^^^^^^^^^\n       ```\n\n-   由于这三个数组都为紧密数值数组，因此内存占用非常小。\n\n-   需要查询拼音时，对 `char` 数组进行二分查找，时间复杂度 O(logn)，然后 O(1) 取得所有候选拼音。\n\n## Benchmark\n\n```bash\ncd pinyinlite\nnpm install\nnpm run benchmark\n```\n\n以下结果基于 Node.js v6.9.4, OS X v10.10.5:\n\n| 测试项                 | 字典大小     | require() 内存和耗时    | 长句耗时      | 速度        |\n| ------------------- | -------- | ------------------ | --------- | --------- |\n| pinyinlite (full)   | ~27000 字 | +2.3 MB, 12.2 ms   | ~2.2 ms   | ~10^7 字/s |\n| pinyinlite (common) | ~8000 字  | +1.0 MB, 7.3 ms    | ~2.3 ms   | ~10^7 字/s |\n| hotoo/pinyin (web)  | ~3500 字  | +3.2 MB, 20.2 ms   | ~128.3 ms | ~10^6 字/s |\n| hotoo/pinyin (node) | ~41000 字 | +32.4 MB, 196.7 ms | ~573.4 ms | ~10^5 字/s |\n\n配置均为：标注全部多音字、不智能选择多音字，长句长度约 20000 字。\n\n```bash\n$ npm run benchmark\n\nRunning hotoopinyin-web...\n{ 'hotoo/pinyin require': { time: 20.29, memory: 3274833.92 },\n  'hotoo/pinyin first call': { time: 132.9, memory: 9616465.92 },\n  'hotoo/pinyin second call': { time: 128.38, memory: 11864145.92 } }\n\nRunning hotoopinyin-full...\n{ 'hotoo/pinyin require': { time: 196.7, memory: 32487751.68 },\n  'hotoo/pinyin first call': { time: 626.3, memory: 19345817.6 },\n  'hotoo/pinyin second call': { time: 573.43, memory: 3927777.28 } }\n\nRunning pinyinlite-common...\n{ 'breeswish/pinyinlite require': { time: 7.34, memory: 1095884.8 },\n  'breeswish/pinyinlite first call': { time: 5.95, memory: 2166988.8 },\n  'breeswish/pinyinlite second call': { time: 2.3, memory: 946421.76 } }\n\nRunning pinyinlite-full...\n{ 'breeswish/pinyinlite require': { time: 12.25, memory: 2319196.16 },\n  'breeswish/pinyinlite first call': { time: 5.93, memory: 1806090.24 },\n  'breeswish/pinyinlite second call': { time: 2.19, memory: 987136 } }\n```\n\n## Contribute\n\n字典现已改成基于 [pinyin-data](https://github.com/mozillazg/pinyin-data) 生成，若要对原始字典提出修改，请直接向 [pinyin-data](https://github.com/mozillazg/pinyin-data) 发起 Pull Request。\n\n若 [pinyin-data](https://github.com/mozillazg/pinyin-data) 已有更新，你希望本项目能用上最新字典，请执行以下命令并发起 Pull Request：\n\n```bash\nnpm run gen:dict\nnpm run build\n```\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbreezewish%2Fpinyinlite","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbreezewish%2Fpinyinlite","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbreezewish%2Fpinyinlite/lists"}