{"id":13396802,"url":"https://github.com/hotoo/pinyin","last_synced_at":"2025-05-13T11:03:29.166Z","repository":{"id":993490,"uuid":"802229","full_name":"hotoo/pinyin","owner":"hotoo","description":":cn: 汉字拼音 ➜ hàn zì pīn yīn","archived":false,"fork":false,"pushed_at":"2025-05-13T01:45:17.000Z","size":11915,"stargazers_count":7659,"open_issues_count":21,"forks_count":858,"subscribers_count":143,"default_branch":"master","last_synced_at":"2025-05-13T02:36:40.335Z","etag":null,"topics":["chinese","hanzi","pinyin","zhongwen"],"latest_commit_sha":null,"homepage":"https://pinyin.js.org","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hotoo.png","metadata":{"files":{"readme":"README-v2.en-US.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["hotoo"]}},"created_at":"2010-07-28T05:31:07.000Z","updated_at":"2025-05-12T15:58:58.000Z","dependencies_parsed_at":"2022-07-12T05:30:36.439Z","dependency_job_id":"e40ad9d2-8f54-4326-80f5-c3a9e77e77d3","html_url":"https://github.com/hotoo/pinyin","commit_stats":{"total_commits":352,"total_committers":29,"mean_commits":"12.137931034482758","dds":"0.14488636363636365","last_synced_commit":"8ceb480e6132fee143a693afc56f48b373d133d7"},"previous_names":[],"tags_count":45,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hotoo%2Fpinyin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hotoo%2Fpinyin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hotoo%2Fpinyin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hotoo%2Fpinyin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hotoo","download_url":"https://codeload.github.com/hotoo/pinyin/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253862231,"owners_count":21975579,"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","hanzi","pinyin","zhongwen"],"created_at":"2024-07-30T18:01:03.500Z","updated_at":"2025-05-13T11:03:29.123Z","avatar_url":"https://github.com/hotoo.png","language":"JavaScript","funding_links":["https://github.com/sponsors/hotoo"],"categories":["中国","JavaScript","Uncategorized","工具库"],"sub_categories":["Uncategorized","Data"],"readme":"# pīnyīn (v2)\n\npinyin, The convert tool of chinese pinyin.\n\n---\n\n[![NPM version][npm-badge]][npm-url]\n[![Build Status][build-badge]][build-url]\n[![Coverage Status][coveralls-badge]][coveralls-url]\n[![Language Grade: JavaScript][lgtm-badge]][lgtm-url]\n[![NPM downloads][npm-downloads]][npm-url]\n\n\n[npm-badge]: https://img.shields.io/npm/v/pinyin.svg?style=flat\n[npm-url]: https://www.npmjs.com/package/pinyin\n[npm-downloads]: http://img.shields.io/npm/dm/pinyin.svg?style=flat\n[build-badge]: https://github.com/hotoo/pinyin/actions/workflows/node.js.yml/badge.svg\n[build-url]: https://github.com/hotoo/pinyin/actions\n[coveralls-badge]: https://coveralls.io/repos/hotoo/pinyin/badge.svg?branch=master\n[coveralls-url]: https://coveralls.io/r/hotoo/pinyin\n[lgtm-badge]: https://img.shields.io/lgtm/grade/javascript/g/hotoo/pinyin.svg?logo=lgtm\u0026logoWidth=18\n[lgtm-url]: https://lgtm.com/projects/g/hotoo/pinyin/context:javascript\n\n\nWeb Site: [简体中文](/README-v2) | English\n\nREADME: [简体中文](README-v2.md) | English\n\n\nConvert Han to pinyin. useful for phonetic notation, sorting, and searching.\n\n\u003e Note: This module both support Node and Web browser.\n\u003e\n\u003e Python version see [mozillazg/python-pinyin](https://github.com/mozillazg/python-pinyin)\n\n---\n\n## Feature\n\n* Segmentation for heteronym words.\n* Support Traditional and Simplified Chinese.\n* Support multiple pinyin style.\n\n## Install\n\nvia npm:\n\n```bash\nnpm install pinyin@2.0 --save\n```\n\n## Usage\n\nfor developer:\n\n```js\nvar pinyin = require(\"pinyin\");\n\nconsole.log(pinyin(\"中心\"));    // [ [ 'zhōng' ], [ 'xīn' ] ]\nconsole.log(pinyin(\"中心\", {\n  heteronym: true                // Enable heteronym mode.\n}));                            // [ [ 'zhōng', 'zhòng' ], [ 'xīn' ] ]\nconsole.log(pinyin(\"中心\", {\n  heteronym: true,              // Enable heteronym mode.\n  segment: true                 // Enable Chinese words segmentation, fix most heteronym problem.\n}));                            // [ [ 'zhōng' ], [ 'xīn' ] ]\nconsole.log(pinyin(\"我喜欢你\", {\n  segment: true,                // Enable segmentation. Needed for grouping.\n  group: true                   // Group pinyin segments\n}));                            // [ [ 'wǒ' ], [ 'xǐhuān' ], [ 'nǐ' ] ]\nconsole.log(pinyin(\"中心\", {\n  style: pinyin.STYLE_INITIALS, // Setting pinyin style.\n  heteronym: true\n}));                            // [ [ 'zh' ], [ 'x' ] ]\n```\n\nfor cli:\n\n```bash\n$ pinyin 中心\nzhōng xīn\n$ pinyin -h\n```\n\n## API\n\n### `\u003cArray\u003e pinyin(words[, options])`\n\nConvert Han （汉字） to pinyin.\n\n`options` argument is optional, for sepcify heteronym mode and pinyin styles.\n\nReturn a `Array\u003cArray\u003cString\u003e\u003e`. If one of Han is heteronym word, it would be\nhave multiple pinyin.\n\n### `Number pinyin.compare(a, b)`\n\nDefault compare implementation for pinyin.\n\n\n## Options\n\n#### `\u003cBoolean\u003e options.segment`\n\nEnable Chinese word segmentation. Segmentation is helpful for fix heteronym\nproblem, but performance will be more slow, and need more CPU and memory.\n\nDefault is `false`.\n\n### `\u003cBoolean\u003e options.heteronym`\n\nEnable or disable heteronym mode. default is disabled, `false`.\n\n### `\u003cBoolean\u003e options.group`\n\nGroup pinyin by phrases. for example:\n\n```\n我喜欢你\nwǒ xǐhuān nǐ\n```\n\n### `\u003cObject\u003e options.style`\n\nSpecify pinyin style. please use static properties like `STYLE_*`.\ndefault is `.STYLE_TONE`. see Static Property for more.\n\n### `options.mode`\n\npinyin mode, default is `pinyin.MODE_NORMAL`. If you cleared in surname scene,\nuse `pinyin.MODE_SURNAME` maybe better.\n\n## Static Property\n\n### `.STYLE_NORMAL`\n\nNormal mode.\n\nExample: `pin yin`\n\n### `.STYLE_TONE`\n\nTone style, this is default.\n\nExample: `pīn yīn`\n\n### `.STYLE_TONE2`\n\ntone style by postfix number [0-4].\n\nExample: `pin1 yin1`\n\n### `.STYLE_TO3NE`\n\ntone style by number [0-4] after phonetic notation character.\n\nExample: `pin1 yin1`\n\n### `.STYLE_INITIALS`\n\nInitial consonant (of a Chinese syllable).\n\nExample: pinyin of `中国` is `zh g`\n\nNote: when a Han （汉字） without initial consonant, will convert to empty string.\n\n### `.STYLE_FIRST_LETTER`\n\nFirst letter style.\n\nExample: `p y`\n\n### `pinyin.MODE_NORMAL`\n\nNormal mode. This is the default mode.\n\n### `pinyin.MODE_SURNAME`\n\nSurname mode. If chinese word is surname, The pinyin of surname is prioritized.\n\n## Test\n\n```\nnpm test\n```\n\n## Q\u0026A\n\n### What's the different Node version and Web version?\n\n`pinyin` support Node and Web browser now, the API and usage is complete same.\n\nBut the Web version is simple than Node version. Just frequently-used dict,\nwithout segmentation, and the dict is compress for web.\n\nBecause of Traditional and Segmentation, the convert result will be not complete same.\nand the test case have some different too.\n\n| Feature      | Web version                     | Node version                     |\n|--------------|---------------------------------|----------------------------------|\n| Dict         | Frequently-used Dict, Compress. | Complete Dict, without Compress. |\n| Segmentation | NO                              | Segmentation options.            |\n| Traditional  | NO                              | Full Traditional support.        |\n\n\n### How to sort by pinyin?\n\nThis module provide default compare implementation:\n\n```js\nconst pinyin = require('pinyin');\n\nconst data = '我要排序'.split('');\nconst sortedData = data.sort(pinyin.compare);\n```\n\nBut if you need different implementation, do it like:\n\n```\nconst pinyin = require('pinyin');\n\nconst data = '我要排序'.split('');\n\n// Suggest you to store pinyin result by data persistence.\nconst pinyinData = data.map(han =\u003e ({\n  han: han,\n  pinyin: pinyin(han)[0][0], // Choose you options and styles.\n}));\nconst sortedData = pinyinData.sort((a, b) =\u003e {\n  return a.pinyin.localeCompare(b.pinyin);\n}).map(d =\u003e d.han);\n```\n\n## Donate\n\nIf this module is helpful for you, please Star this repository.\n\nAnd you have chioce donate to me via Aliapy or WeChat:\n\n\u003cimg src=\"https://hotoo.github.io/images/donate-hotoo.png\" alt=\"Alipay:hotoo.cn@gmail.com, WeChat:hotoome\" width=\"400\" /\u003e\n\n\n## License\n\n[MIT](http://hotoo.mit-license.org/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhotoo%2Fpinyin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhotoo%2Fpinyin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhotoo%2Fpinyin/lists"}