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
Attribute | Type | Description | Optional Values | Detail | Default Value |
---|---|---|---|---|---|
type | string | The format of returned value | string | string[] | string |
array | string[][] | ||||
all | AllData[][] | ||||
toneType | string | The display of tone | symbol | Normal | symbol |
none | Hide | ||||
num | Display in numbers after Pinyin | ||||
pattern | string | The content of returned value | pinyin | Full Pinyin | pinyin |
initial | initial of Pinyin | ||||
final | final of Pinyin | ||||
num | Numbers of tone | ||||
first | first character | ||||
finalHead | head of final | ||||
finalBody | body of final | ||||
finalTail | tail of final | ||||
nonZh | string | Non Chinese Character Processing | spaced | Use spaces to separate | spaced |
consecutive | consecutive | ||||
removed | Remove them in returned value | ||||
v | boolean | Whether using v to replace ü in returned value | false | not replace | false |
true | replace |