Skip to content

polyphonic v3.15.0+

Use the polyphonic API to get all Pinyins of Chinese characters.

TIP

In most cases, you only need to use the pinyin API, which automatically recognizes the correct pinyin of polyphonic Chinese characters in text. polyphonic is used to get all Pinyins of each Chinese characters, rather than the correct Pinyin of polyphonic Chinese words in a sentence.

Example

Basic Use

Use the type attribute to control the format of the return value:

js
import { polyphonic } from 'pinyin-pro';

const resultString = polyphonic('好好学习'); // ['hǎo hào', 'hǎo hào', 'xué', 'xí']

const resultArray = polyphonic('好好学习', { type: 'array' }); // [['hǎo', 'hào'], ['hǎo', 'hào'], ['xué'], ['xí']]

const resultAll = polyphonic('好好学习', { type: 'all' });
/*
[
  [
    {
      final: 'ǎo',
      finalBody: 'ǎ',
      finalHead: '',
      finalTail: 'o',
      first: 'h',
      initial: 'h',
      isZh: true,
      num: 3,
      origin: '好',
      pinyin: 'hǎo',
    },
    {
      final: 'ào',
      finalBody: 'à',
      finalHead: '',
      finalTail: 'o',
      first: 'h',
      initial: 'h',
      isZh: true,
      num: 4,
      origin: '好',
      pinyin: 'hào',
    },
  ],
  [
    {
      final: 'ǎo',
      finalBody: 'ǎ',
      finalHead: '',
      finalTail: 'o',
      first: 'h',
      initial: 'h',
      isZh: true,
      num: 3,
      origin: '好',
      pinyin: 'hǎo',
    },
    {
      final: 'ào',
      finalBody: 'à',
      finalHead: '',
      finalTail: 'o',
      first: 'h',
      initial: 'h',
      isZh: true,
      num: 4,
      origin: '好',
      pinyin: 'hào',
    },
  ],
  [
    {
      final: 'üé',
      finalBody: 'é',
      finalHead: 'ü',
      finalTail: '',
      first: 'x',
      initial: 'x',
      isZh: true,
      num: 2,
      origin: '学',
      pinyin: 'xué',
    },
  ],
  [
    {
      final: 'í',
      finalBody: 'í',
      finalHead: '',
      finalTail: '',
      first: 'x',
      initial: 'x',
      isZh: true,
      num: 2,
      origin: '习',
      pinyin: 'xí',
    },
  ],
];
*/
import { polyphonic } from 'pinyin-pro';

const resultString = polyphonic('好好学习'); // ['hǎo hào', 'hǎo hào', 'xué', 'xí']

const resultArray = polyphonic('好好学习', { type: 'array' }); // [['hǎo', 'hào'], ['hǎo', 'hào'], ['xué'], ['xí']]

const resultAll = polyphonic('好好学习', { type: 'all' });
/*
[
  [
    {
      final: 'ǎo',
      finalBody: 'ǎ',
      finalHead: '',
      finalTail: 'o',
      first: 'h',
      initial: 'h',
      isZh: true,
      num: 3,
      origin: '好',
      pinyin: 'hǎo',
    },
    {
      final: 'ào',
      finalBody: 'à',
      finalHead: '',
      finalTail: 'o',
      first: 'h',
      initial: 'h',
      isZh: true,
      num: 4,
      origin: '好',
      pinyin: 'hào',
    },
  ],
  [
    {
      final: 'ǎo',
      finalBody: 'ǎ',
      finalHead: '',
      finalTail: 'o',
      first: 'h',
      initial: 'h',
      isZh: true,
      num: 3,
      origin: '好',
      pinyin: 'hǎo',
    },
    {
      final: 'ào',
      finalBody: 'à',
      finalHead: '',
      finalTail: 'o',
      first: 'h',
      initial: 'h',
      isZh: true,
      num: 4,
      origin: '好',
      pinyin: 'hào',
    },
  ],
  [
    {
      final: 'üé',
      finalBody: 'é',
      finalHead: 'ü',
      finalTail: '',
      first: 'x',
      initial: 'x',
      isZh: true,
      num: 2,
      origin: '学',
      pinyin: 'xué',
    },
  ],
  [
    {
      final: 'í',
      finalBody: 'í',
      finalHead: '',
      finalTail: '',
      first: 'x',
      initial: 'x',
      isZh: true,
      num: 2,
      origin: '习',
      pinyin: 'xí',
    },
  ],
];
*/

Tone

Use the toneType attribute to control the display of tones:

js
import { polyphonic } from 'pinyin-pro';

const resultNone = polyphonic('好好学习', { toneType: 'none' }); // ['hao', 'hao', 'xue', 'xi']

const resultNum = polyphonic('好好学习', { toneType: 'num' }); // ['hao3 hao4', 'hao3 hao4', 'xue2', 'xi2']
import { polyphonic } from 'pinyin-pro';

const resultNone = polyphonic('好好学习', { toneType: 'none' }); // ['hao', 'hao', 'xue', 'xi']

const resultNum = polyphonic('好好学习', { toneType: 'num' }); // ['hao3 hao4', 'hao3 hao4', 'xue2', 'xi2']

API

The parameters of polyphonic are mostly the same as pinyin.

Function

js
import { polyphonic } from 'pinyin-pro';

interface BasicOptions {
    type: 'string' | 'array' | 'all';
    toneType?: 'symbol' | 'num' | 'none';
    pattern?: 'pinyin' | 'initial' | 'final' | 'num' | 'first' | 'finalHead' | 'finalBody' | 'finalTail';
    nonZh?: 'spaced' | 'consecutive' | 'removed';
    v?: boolean;
}

interface AllData {
    origin: string;
    pinyin: string;
    initial: string;
    final: string;
    num: number;
    first: string;
    finalHead: string;
    finalBody: string;
    finalTail: string;
    isZh: boolean;
}

 // 返回转换后的信息
function polyphonic(text: string, options?: BasicOptions): string[] | string[][] | AllData[][]
import { polyphonic } from 'pinyin-pro';

interface BasicOptions {
    type: 'string' | 'array' | 'all';
    toneType?: 'symbol' | 'num' | 'none';
    pattern?: 'pinyin' | 'initial' | 'final' | 'num' | 'first' | 'finalHead' | 'finalBody' | 'finalTail';
    nonZh?: 'spaced' | 'consecutive' | 'removed';
    v?: boolean;
}

interface AllData {
    origin: string;
    pinyin: string;
    initial: string;
    final: string;
    num: number;
    first: string;
    finalHead: string;
    finalBody: string;
    finalTail: string;
    isZh: boolean;
}

 // 返回转换后的信息
function polyphonic(text: string, options?: BasicOptions): string[] | string[][] | AllData[][]

Parameters

AttributeTypeDescriptionOptional ValuesDetailDefault Value
typestringThe format of returned valuestringstring[]string
arraystring[][]
allAllData[][]
toneTypestringThe display of tonesymbolNormalsymbol
noneHide
numDisplay in numbers after Pinyin
patternstringThe content of returned valuepinyinFull Pinyinpinyin
initialinitial of Pinyin
finalfinal of Pinyin
numNumbers of tone
firstfirst character
finalHeadhead of final
finalBodybody of final
finalTailtail of final
nonZhstringNon Chinese Character ProcessingspacedUse spaces to separatespaced
consecutiveconsecutive
removedRemove them in returned value
vbooleanWhether using v to replace ü in returned valuefalsenot replacefalse
truereplace