Top Related Projects
汉字转拼音(pypinyin)
A copy of http://sourceforge.net/projects/pinyin4j, then deploy it to maven central repository.
Quick Overview
Hotoo/pinyin is a JavaScript library for converting Chinese characters to their corresponding Pinyin (romanization) representation. It supports both simplified and traditional Chinese characters and offers various output options, including tones and diacritical marks.
Pros
- Supports both simplified and traditional Chinese characters
- Offers multiple output formats (with/without tones, with diacritical marks)
- Provides customization options for handling polyphones and heteronyms
- Lightweight and easy to integrate into web projects
Cons
- Limited to Mandarin Chinese Pinyin (doesn't support other Chinese dialects)
- May require additional dictionaries for accurate handling of less common characters
- Occasional inaccuracies with complex phrases or names
Code Examples
- Basic usage:
import pinyin from 'pinyin';
console.log(pinyin('中文'));
// Output: [ [ 'zhong' ], [ 'wen' ] ]
- Using tone numbers:
console.log(pinyin('中文', { style: pinyin.STYLE_TONE2 }));
// Output: [ [ 'zhong1' ], [ 'wen2' ] ]
- Using diacritical marks:
console.log(pinyin('中文', { style: pinyin.STYLE_NORMAL }));
// Output: [ [ 'zhōng' ], [ 'wén' ] ]
- Customizing heteronym handling:
console.log(pinyin('省长', {
heteronym: true,
segment: true
}));
// Output: [ [ 'shěng', 'xǐng' ], [ 'zhǎng', 'cháng' ] ]
Getting Started
To use hotoo/pinyin in your project, follow these steps:
-
Install the package:
npm install pinyin
-
Import and use in your JavaScript code:
import pinyin from 'pinyin'; const result = pinyin('你好,世界'); console.log(result); // Output: [ [ 'ni' ], [ 'hao' ], [ 'shi' ], [ 'jie' ] ]
-
Customize options as needed:
const result = pinyin('你好,世界', { style: pinyin.STYLE_TONE2, heteronym: true }); console.log(result); // Output: [ [ 'ni3' ], [ 'hao3' ], [ 'shi4' ], [ 'jie4' ] ]
Competitor Comparisons
汉字转拼音(pypinyin)
Pros of python-pinyin
- Written in Python, making it more accessible for Python developers
- Supports multiple pinyin styles (e.g., NORMAL, TONE, TONE2, INITIALS, FIRST_LETTER)
- Provides flexible customization options for handling non-Chinese characters
Cons of python-pinyin
- Limited to Python environments, unlike pinyin which is JavaScript-based
- May have slower performance compared to pinyin due to language differences
- Less extensive documentation and examples compared to pinyin
Code Comparison
python-pinyin:
from pypinyin import pinyin, Style
print(pinyin('中心', style=Style.TONE))
# Output: [['zhōng'], ['xīn']]
pinyin:
const pinyin = require("pinyin");
console.log(pinyin('中心', {style: pinyin.STYLE_TONE}));
// Output: [['zhōng'], ['xīn']]
Both libraries offer similar functionality for converting Chinese characters to pinyin, with slight differences in syntax and available options. The python-pinyin library provides more granular control over pinyin styles, while pinyin offers a simpler API with fewer configuration options.
The choice between these libraries largely depends on the programming language preference and specific project requirements. Python developers may find python-pinyin more convenient, while JavaScript developers might prefer pinyin for its integration with Node.js and browser environments.
A copy of http://sourceforge.net/projects/pinyin4j, then deploy it to maven central repository.
Pros of pinyin4j
- Written in Java, offering better integration with Java-based projects
- Provides more extensive tone mark and number notation options
- Includes support for multiple pinyin systems (e.g., Wadegiles, Yale)
Cons of pinyin4j
- Less frequently updated compared to pinyin
- Requires more setup and configuration for non-Java projects
- Limited customization options for output formats
Code Comparison
pinyin4j:
PinyinHelper.toHanyuPinyinStringArray('中')[0]; // "zhong"
pinyin:
pinyin('中')[0][0]; // "zhong"
Additional Notes
Both libraries aim to convert Chinese characters to pinyin, but they cater to different ecosystems. pinyin4j is more suitable for Java developers and offers more comprehensive pinyin notation options. On the other hand, pinyin is a JavaScript library that integrates more easily with web-based projects and has a simpler API.
The choice between these libraries largely depends on the programming language of your project and the specific pinyin conversion requirements you have. Consider factors such as ease of integration, performance, and available features when making your decision.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
pīnyīn (v3)
pÄ«nyÄ«n, æ±åæ¼é³è½¬æ¢å·¥å ·ã
Web Site: ç®ä½ä¸æ | English | íêµì´
README: ç®ä½ä¸æ | English | íêµì´
转æ¢ä¸æå符为æ¼é³ãå¯ä»¥ç¨äºæ±å注é³ãæåºãæ£ç´¢ã
注ï¼è¿ä¸ªçæ¬åæ¶æ¯æå¨ Node å Web æµè§å¨ç¯å¢è¿è¡ï¼
Python çè¯·å ³æ³¨ mozillazg/python-pinyin
ç¹æ§
- æ ¹æ®è¯ç»æºè½å¹é ææ£ç¡®çæ¼é³ã
- æ¯æå¤é³åã
- ç®åçç¹ä½æ¯æã
- æ¯æå¤ç§ä¸åæ¼é³é£æ ¼ã
å®è£
via npm:
npm install pinyin --save
ç¨æ³
å¼åè ï¼
import pinyin from "pinyin";
console.log(pinyin("ä¸å¿")); // [ [ 'zhÅng' ], [ 'xÄ«n' ] ]
console.log(pinyin("ä¸å¿", {
heteronym: true, // å¯ç¨å¤é³å模å¼
})); // [ [ 'zhÅng', 'zhòng' ], [ 'xÄ«n' ] ]
console.log(pinyin("ä¸å¿", {
heteronym: true, // å¯ç¨å¤é³å模å¼
segment: true, // å¯ç¨åè¯ï¼ä»¥è§£å³å¤é³åé®é¢ãé»è®¤ä¸å¼å¯ï¼ä½¿ç¨ true å¼å¯ä½¿ç¨ Intl.Segmenter åè¯åºã
})); // [ [ 'zhÅng' ], [ 'xÄ«n' ] ]
console.log(pinyin("ä¸å¿", {
segment: "@node-rs/jieba", // æå®åè¯åºï¼å¯ä»¥æ¯ "Intl.Segmenter", "nodejieba"ã"segmentit"ã"@node-rs/jieba"ã
})); // [ [ 'zhÅng' ], [ 'xÄ«n' ] ]
console.log(pinyin("æåæ¬¢ä½ ", {
segment: "segmentit", // å¯ç¨åè¯
group: true, // å¯ç¨è¯ç»
})); // [ [ 'wÇ' ], [ 'xÇhuÄn' ], [ 'nÇ' ] ]
console.log(pinyin("ä¸å¿", {
style: "initials", // 设置æ¼é³é£æ ¼ã
heteronym: true, // å³ä½¿æå¤é³åï¼å 为æ¼é³é£æ ¼éæ©ï¼éå¤çä¹ä¼å并ã
})); // [ [ 'zh' ], [ 'x' ] ]
console.log(pinyin("å夫人", {
mode: "surname", // å§å模å¼ã
})); // [ ['huà '], ['fū'], ['rén'] ]
å½ä»¤è¡ï¼
$ pinyin ä¸å¿
zhÅng xÄ«n
$ pinyin -h
ç±»å
IPinyinOptions
ä¼ å ¥ç» pinyin æ¹æ³ç第äºä¸ªåæ°çé项类åã
export interface IPinyinOptions {
style?: IPinyinStyle; // æ¼é³è¾åºå½¢å¼
mode?: IPinyinMode, // æ¼é³æ¨¡å¼
// æå®åè¯åºã
// 为äºå
¼å®¹èçæ¬ï¼å¯ä»¥ä½¿ç¨ boolean ç±»åæå®æ¯å¦å¼å¯åè¯ï¼é»è®¤å¼å¯ã
segment?: IPinyinSegment | boolean;
// æ¯å¦è¿åå¤é³å
heteronym?: boolean;
// æ¯å¦åç»è¯ç»æ¼é³
group?: boolean;
compact?: boolean;
}
IPinyinStyle
è¾åºæ¼é³æ ¼å¼ãå¯ä»¥ä»ç´æ¥ä½¿ç¨ä»¥ä¸å符串ææ°åï¼ä¹å
¼å®¹ v2 çæ¬ä¸ pinyin.STYLE_TONE
è¿æ ·çå½¢å¼ã
export type IPinyinStyle =
"normal" | "tone" | "tone2" | "to3ne" | "initials" | "first_letter" | "passport" | // æ¨è使ç¨å°åï¼åè¾åºçæ¼é³ä¸è´
"NORMAL" | "TONE" | "TONE2" | "TO3NE" | "INITIALS" | "FIRST_LETTER" | "PASSPORT" | // æ¹ä¾¿èçæ¬è¿ç§»
0 | 1 | 2 | 5 | 3 | 4; // å
¼å®¹èçæ¬
IPinyinMode
æ¼é³æ¨¡å¼ï¼é»è®¤æ®é模å¼ï¼å¯ä»¥æå®äººå模å¼ã
// - NORMAL: æ®é模å¼
// - SURNAME: å§æ°æ¨¡å¼ï¼ä¼å
使ç¨å§æ°çæ¼é³ã
export type IPinyinMode =
"normal" | "surname" |
"NORMAL" | "SURNAME";
IPinyinSegment
åè¯æ¹å¼ã
- é»è®¤å
³é
false
ï¼ - ä¹å¯ä»¥è®¾ç½®ä¸º
true
å¼å¯ï¼Web å Node çä¸åä½¿ç¨ "Intl.Segmenter" åè¯ã - ä¹å¯ä»¥å£°æ以ä¸å符串æ¥æå®åè¯ç®æ³ãä½ç®å Web çåªæ¯æ "Intl.Segmenter" å "segmentit" åè¯ã
export type IPinyinSegment = "Intl.Segmenter" | "nodejieba" | "segmentit" | "@node-rs/jieba";
API
æ¹æ³ <Array> pinyin(words: string[, options: IPinyinOptions])
å°ä¼ å ¥çä¸æå符串 (words) 转æ¢ææ¼é³ç¬¦å·ä¸²ã
options æ¯å¯éçï¼å¯ä»¥è®¾å®æ¼é³é£æ ¼ï¼ææå¼å¤é³åé项ã
è¿åäºç»´æ°ç»ï¼ç¬¬ä¸ç»´æ¯ä¸ªæ°ç»é¡¹ä½ç½®å¯¹åºæ¯ä¸ªä¸æå符串ä½ç½®ã 第äºç»´æ¯å个æ±åç读é³å表ï¼å¤é³åä¼æå¤ä¸ªæ¼é³é¡¹ã
æ¹æ³ Number compare(a, b)
ææ¼é³æåºçé»è®¤ç®æ³ã
æ¹æ³ string[][] compact(pinyinResult array[][])
å°æ¼é³å¤é³å以åç§å¯è½çç»åæååæ¢æç´§åå½¢å¼ãåè options.compact
åæ°
<Boolean|IPinyinSegment> options.segment
æ¯å¦å¯ç¨åè¯æ¨¡å¼ï¼ä¸æåè¯æå©äºæ大çéä½å¤é³åé®é¢ã ä½æ§è½ä¼æ大çä¸éï¼å åä¹ä¼ä½¿ç¨æ´å¤ã
- é»è®¤ä¸å¯ç¨åè¯ã
- å¦æ
segemnt = true
ï¼é»è®¤ä½¿ç¨ Intl.Segmenter åè¯ã - å¯ä»¥æå® "Intl.Segmenter"ã"nodejieba"ã"segmentit"ã"@node-rs/jieba" è¿è¡åè¯ã
<Boolean> options.heteronym
æ¯å¦å¯ç¨å¤é³å模å¼ï¼é»è®¤å ³éã
å ³éå¤é³å模å¼æ¶ï¼è¿åæ¯ä¸ªæ±å第ä¸ä¸ªå¹é çæ¼é³ã
å¯ç¨å¤é³å模å¼æ¶ï¼è¿åå¤é³åçæææ¼é³å表ã
<Boolean> options.group
æè¯ç»åç»æ¼é³ï¼ä¾å¦ï¼
æå欢ä½
wÇ xÇhuÄn nÇ
<IPinyinStyle> options.style
æå®æ¼é³é£æ ¼ãå¯ä»¥ä½¿ç¨ä»¥ä¸ç¹å®å符串ææ°å¼æå®ï¼
IPinyinStyle =
"normal" | "tone" | "tone2" | "to3ne" | "initials" | "first_letter" | "passport" | // æ¨è使ç¨å°åï¼åè¾åºçæ¼é³ä¸è´
"NORMAL" | "TONE" | "TONE2" | "TO3NE" | "INITIALS" | "FIRST_LETTER" | "PASSPORT" | // æ¹ä¾¿èçæ¬è¿ç§»
0 | 1 | 2 | 5 | 3 | 4; // å
¼å®¹èçæ¬
NORMAL
, normal
æ®éé£æ ¼ï¼å³ä¸å¸¦å£°è°ã
å¦ï¼pin yin
TONE
, tone
声è°é£æ ¼ï¼æ¼é³å£°è°å¨éµæ¯ç¬¬ä¸ä¸ªåæ¯ä¸ã
注ï¼è¿æ¯é»è®¤çé£æ ¼ã
å¦ï¼pÄ«n yÄ«n
TONE2
, tone2
声è°é£æ ¼ 2ï¼å³æ¼é³å£°è°ä»¥æ°åå½¢å¼å¨å个æ¼é³ä¹åï¼ç¨æ°å [0-4] è¿è¡è¡¨ç¤ºã
å¦ï¼pin1 yin1
TO3NE
, to3ne
声è°é£æ ¼ 3ï¼å³æ¼é³å£°è°ä»¥æ°åå½¢å¼å¨æ³¨é³å符ä¹åï¼ç¨æ°å [0-4] è¿è¡è¡¨ç¤ºã
å¦ï¼pi1n yi1n
INITIALS
, initials
声æ¯é£æ ¼ï¼åªè¿åå个æ¼é³ç声æ¯é¨åã对äºæ²¡æ声æ¯çæ±åï¼è¿å空ç½å符串ã
å¦ï¼ä¸å½
çæ¼é³ zh g
注ï¼å£°æ¯é£æ ¼ä¼åºå zh
å z
ï¼ch
å c
ï¼sh
å s
ã
注æï¼é¨åæ±å没æ声æ¯ï¼å¦ å
ï¼é¥¿
çï¼å¦å¤ y
, w
, yu
é½ä¸æ¯å£°æ¯ï¼
è¿äºæ±åçæ¼é³å£°æ¯é£æ ¼ä¼è¿å ""
ã请ä»ç»èèä½ çéæ±æ¯å¦åºè¯¥ä½¿ç¨é¦åæ¯é£æ ¼ã
详æ
请åè [为ä»ä¹æ²¡æ y, w, yu å 个声æ¯](#为ä»ä¹æ²¡æ -y-w-yu- å 个声æ¯)
FIRST_LETTER
, first_letter
é¦åæ¯é£æ ¼ï¼åªè¿åæ¼é³çé¦åæ¯é¨åã
å¦ï¼p y
PASSPORT
, passport
æ¤ç
§é£æ ¼ã转æ¢æ大åå½¢å¼ï¼å¹¶ä¸ ü
ä¼ä»¥ YU
å½¢å¼è¾åºã
å½å®¶ç§»æ°ç®¡çå±é¨æ·ç½ç«äº2021å¹´9æ29æ¥åå¸äºãå ³äºå å°å± æ°æ¼é³å§åä¸åæ¯âüâå¨åºå ¥å¢è¯ä»¶ä¸æå°è§åçå ¬åãï¼ä»¥ä¸ç®ç§°å ¬åï¼ï¼æ ¹æ®ãä¸å½äººåæ±è¯æ¼é³åæ¯æ¼åè§åãåãå ³äºæºè¯»æ è¡è¯ä»¶çç¸å ³å½é éç¨è§èãï¼ å å°å± æ°ç³ååºå ¥å¢è¯ä»¶ï¼åºå ¥å¢è¯ä»¶ä¸æå°çæè¯äººæ¼é³å§åä¸ï¼Lüï¼åçåï¼ãNüï¼å¥³çåï¼ä¸çåæ¯âüâåºå½è½¬æ¢ä¸ºâYUâï¼LüEï¼ç¥çåï¼ãNüEï¼èçåï¼ä¸çåæ¯âüâåºå½è½¬æ¢ä¸ºâUâã
options.mode
æ¼é³æ¨¡å¼ï¼é»è®¤ "NORMAL" æ®é模å¼ã å¦æä½ æç¡®çå¨å§ååºæ¯ä¸ï¼å¯ä»¥ä½¿ç¨ "SURNAME" 让å§æ°ä½¿ç¨æ´åç¡®çæ¼é³ã
NORMAL
ï¼æ®é模å¼ï¼èªå¨è¯å«è¯»é³ãSURNAME
ï¼å§å模å¼ï¼å¯¹äºæç¡®çå§ååºæ¯ï¼å¯ä»¥æ´åç¡®çè¯å«å§æ°ç读é³ã
options.compact
æ¯å¦è¿åç´§å模å¼ï¼é»è®¤ falseï¼ææ åæ ¼å¼è¿åã å¦æ设置为 trueï¼åå°å¤é³åå¯è½çåç§ç»åæååè¿åãä¾å¦ï¼
pinyin("ä½ å¥½å", { compact:false });
> [[nÇ], [hÇo,hà o], [ma,má,mÇ]]
pinyin("ä½ å¥½å", { compact:true });
> [
> [nÇ,hÇo,ma], [nÇ,hÇo,má], [nÇ,hÇo,mÇ],
> [nÇ,hà o,ma], [nÇ,hà o,má], [nÇ,hà o,mÇ],
> ]
ä½ ä¹å¯ä»¥å¿
è¦æ¶ä½¿ç¨ compact()
å½æ°å¤ç pinyin(han, {compact:false})
è¿åçç»æã
Test
npm test
Q&A
å ³äº Web çå¦ä½ä½¿ç¨
é¦å ï¼æ建议大家åºè¯¥ä¼å èèå¨æå¡ç«¯ä¸æ¬¡æ§è½¬æ¢æ¼é³å¹¶å°ç»ææä¹ åï¼é¿å å¨å®¢æ·ç«¯æ¯æ¬¡è½¬æ¢æèæ§è½åä½éªã
å¦æä½ åæå¨å®¢æ·ç«¯ä½¿ç¨ï¼ä½ å¯ä»¥èèä½¿ç¨ Webpack + Babel æ¥è½¬æ¢æä½ç«¯æµè§å¨çå¯æ§è¡ä»£ç ã
å®å¨ä¸æ³æè ¾ï¼å¯ä»¥è¯è¯ https://github.com/hotoo/pinyin/tree/gh-pages/dist
为ä»ä¹æ²¡æ y
, w
, yu
å 个声æ¯ï¼
声æ¯é£æ ¼ï¼INITIALSï¼ä¸ï¼âé¨âãâæâãâåâçæ±åè¿å空å符串ï¼å ä¸ºæ ¹æ®ãæ±è¯æ¼é³æ¹æ¡ãï¼
y
ï¼w
ï¼Ã¼ (yu)
é½ä¸æ¯å£°æ¯ï¼å¨æäºç¹å®éµæ¯æ 声æ¯æ¶ï¼æå ä¸ y
æ w
ï¼è ü
ä¹æå
¶ç¹å®è§åã
å¦æä½ è§å¾è¿ä¸ªç»ä½ 带æ¥äºéº»ç¦ï¼é£ä¹ä¹è¯·å°å¿ä¸äºæ 声æ¯çæ±åï¼å¦âåâãâ饿âãâæâãâæâçï¼ã è¿æ¶åä½ ä¹è®¸éè¦çæ¯é¦åæ¯é£æ ¼ï¼FIRST_LETTERï¼ã
å¦ä½å®ç°ææ¼é³æåºï¼
pinyin 模åæä¾äºé»è®¤çæåºæ¹æ¡ï¼
const pinyin = require('pinyin');
const data = 'æè¦æåº'.split('');
const sortedData = data.sort(pinyin.compare);
å¦æé»è®¤çæ¯è¾æ¹æ³ä¸è½æ»¡è¶³ä½ çéæ±ï¼ä½ å¯ä»¥èªå®ä¹ pinyinCompare æ¹æ³ï¼
const pinyin = require('pinyin');
const data = 'æè¦æåº'.split('');
// 建议å°æ±åçæ¼é³æä¹
ååå¨èµ·æ¥ã
const pinyinData = data.map(han => ({
han: han,
pinyin: pinyin(han)[0][0], // å¯ä»¥èªè¡éæ©ä¸åççææ¼é³æ¹æ¡åé£æ ¼ã
}));
const sortedData = pinyinData.sort((a, b) => {
return a.pinyin.localeCompare(b.pinyin);
}).map(d => d.han);
node çå web çæä»ä¹å¼åï¼
pinyin
ç®åå¯ä»¥åæ¶è¿è¡å¨ Node æå¡å¨ç«¯å Web æµè§å¨ç«¯ã
API å使ç¨æ¹å¼å®æä¸è´ã
ä½ Web çè¾ Node çç¨ç®åï¼æ¼é³åºåªæ常ç¨åé¨åï¼æ²¡æ使ç¨åè¯ç®æ³ï¼ 并ä¸èèäºç½ç»ä¼ è¾å¯¹è¯åºè¿è¡äºå缩å¤çã
ç±äºåè¯åç¹ä½ä¸æçç¹æ§ï¼é¨åæ åµä¸çç»æä¹ä¸å°½ç¸åã
ç¹æ§ | Web ç | Node ç |
---|---|---|
æ¼é³åº | 常ç¨ååºãå缩ãå并 | å®æ´ååºãä¸å缩ãå并 |
åè¯ | 没æåè¯ | 使ç¨åè¯ç®æ³ï¼å¤é³åæ¼é³æ´åç¡®ã |
æ¼é³é¢åº¦æåº | ææ ¹æ®æ¼é³ä½¿ç¨é¢åº¦ä¼å 级æåºã | å Web çã |
ç¹ä½ä¸æ | 没æç¹ä½ä¸ææ¯æã | æç®åçç¹ç®æ±å转æ¢ã |
ç±äºè¿äºåºå«ï¼æµè¯ä¸åè¿è¡ç¯å¢çç¨ä¾ä¹ä¸å°½ç¸åã
æèµ
å¦æè¿ä¸ªæ¨¡åæ帮å©å°æ¨ï¼è¯· Star è¿ä¸ªä»åºã
ä½ ä¹å¯ä»¥éæ©ä½¿ç¨æ¯ä»å®æ微信ç»ææèµ ï¼
许å¯è¯
Top Related Projects
汉字转拼音(pypinyin)
A copy of http://sourceforge.net/projects/pinyin4j, then deploy it to maven central repository.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot