Chinese character to Pinyin conversion with intelligent phrase matching and multiple pronunciation support
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Seven different Pinyin output formats providing flexibility for various use cases from tone-marked pronunciation guides to search-friendly formats.
Complete set of supported Pinyin output styles with multiple format variations for compatibility.
type IPinyinStyle =
// Recommended lowercase formats (consistent with output)
| "normal" | "tone" | "tone2" | "to3ne" | "initials" | "first_letter" | "passport"
// Uppercase formats (v2.x compatibility)
| "NORMAL" | "TONE" | "TONE2" | "TO3NE" | "INITIALS" | "FIRST_LETTER" | "PASSPORT"
// Numeric formats (legacy compatibility)
| 0 | 1 | 2 | 3 | 4 | 5 | 6;1. TONE Style (Default)
"tone", "TONE", 1pinyin("中心", { style: "tone" });
// Result: [["zhōng"], ["xīn"]]
pinyin("我爱你", { style: "tone" });
// Result: [["wǒ"], ["ài"], ["nǐ"]]2. TONE2 Style
"tone2", "TONE2", 2pinyin("中心", { style: "tone2" });
// Result: [["zhong1"], ["xin1"]]
pinyin("我爱你", { style: "tone2" });
// Result: [["wo3"], ["ai4"], ["ni3"]]3. TO3NE Style
"to3ne", "TO3NE", 5pinyin("中心", { style: "to3ne" });
// Result: [["zho1ng"], ["xi1n"]]
pinyin("我爱你", { style: "to3ne" });
// Result: [["wo3"], ["a4i"], ["ni3"]]4. NORMAL Style
"normal", "NORMAL", 0pinyin("中心", { style: "normal" });
// Result: [["zhong"], ["xin"]]
pinyin("我爱你", { style: "normal" });
// Result: [["wo"], ["ai"], ["ni"]]5. INITIALS Style
"initials", "INITIALS", 3pinyin("中心", { style: "initials" });
// Result: [["zh"], ["x"]]
pinyin("我爱你", { style: "initials" });
// Result: [[""], [""], [""]] // No initials for these characters
pinyin("北京", { style: "initials" });
// Result: [["b"], ["j"]]6. FIRST_LETTER Style
"first_letter", "FIRST_LETTER", 4pinyin("中心", { style: "first_letter" });
// Result: [["z"], ["x"]]
pinyin("我爱你", { style: "first_letter" });
// Result: [["w"], ["a"], ["n"]]7. PASSPORT Style
"passport", "PASSPORT", 6pinyin("中心", { style: "passport" });
// Result: [["ZHONG"], ["XIN"]]
pinyin("绿色", { style: "passport" });
// Result: [["LYU"], ["SE"]] // ü becomes YUThe pinyin function object exposes style constants for backward compatibility:
interface IPinyin {
// Style constants (legacy v2.x compatibility)
STYLE_TONE: number; // 1
STYLE_TONE2: number; // 2
STYLE_TO3NE: number; // 5
STYLE_NORMAL: number; // 0
STYLE_INITIALS: number; // 3
STYLE_FIRST_LETTER: number; // 4
STYLE_PASSPORT: number; // 6
}Usage Examples:
import pinyin from "pinyin";
// Using legacy constants
pinyin("中心", { style: pinyin.STYLE_TONE2 });
// Same as: pinyin("中心", { style: "tone2" });
pinyin("中心", { style: pinyin.STYLE_NORMAL });
// Same as: pinyin("中心", { style: "normal" });Different styles can produce different results when combined with heteronym support:
// Multiple pronunciations with tone marks
pinyin("中", { style: "tone", heteronym: true });
// Result: [["zhōng", "zhòng"]]
// Multiple pronunciations with numeric tones
pinyin("中", { style: "tone2", heteronym: true });
// Result: [["zhong1", "zhong4"]]
// Initials may deduplicate identical consonants
pinyin("中", { style: "initials", heteronym: true });
// Result: [["zh"]] // Both pronunciations have same initialStyles work consistently with segmentation options:
// Tone style with phrase segmentation
pinyin("中国人", { style: "tone", segment: true, group: true });
// Result: [["zhōngguó"], ["rén"]]
// Normal style for search indexing
pinyin("中国人", { style: "normal", segment: true });
// Result: [["zhong"], ["guo"], ["ren"]]Characters starting with vowels (a, e, o, i, u, ü) may show different patterns across styles:
pinyin("爱", { style: "initials" });
// Result: [[""]] // No initial consonant
pinyin("爱", { style: "first_letter" });
// Result: [["a"]] // Vowel first letterNon-Chinese characters are passed through unchanged regardless of style:
pinyin("hello中国", { style: "tone" });
// Result: [["hello"], ["zhōng"], ["guó"]]
pinyin("hello中国", { style: "passport" });
// Result: [["hello"], ["ZHONG"], ["GUO"]]Install with Tessl CLI
npx tessl i tessl/npm-pinyin