Skip to content

polyphonic v3.15.0+

使用 polyphonic api 来获取多音字的全部读音。

TIP

大多数情况下你只需要使用 pinyin api,它会自动识别文本中多音字的正确读音。polyphonic 是用来获取每个字的所有读音的而不是多音字在句子中的正确读音。

示例

常规使用

使用 type 属性控制返回值的格式:

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í',
    },
  ],
];
*/

不带音调

使用 toneType 属性控制音调的展示形式:

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']

语法及参数

polyphonic 的参数大部分和 pinyin 是相同的。

语法

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[][]

参数

属性类型描述可选值说明默认值
typestring返回的格式stringstring 数组string
arraystring 二维数组
allAllData 类型二维数组
toneTypestring音调展示的形式symbol正常拼音音调symbol
none不展示
num跟在拼音后以数字展示
patternstring展示的内容pinyin拼音全拼pinyin
initial声母
final韵母
num音调对应数字
first首字母
finalHead韵头
finalBody韵腹
finalTail韵尾
nonZhstring非中文字符处理spaced空格间隔开spaced
consecutive连续
removed从返回值中移除
vboolean是否使用v替换返回值中的üfalse不替换false
true替换