mirror of
https://github.com/zh-lx/pinyin-pro.git
synced 2026-03-13 09:51:38 +08:00
feat: pinyin all 模式新增 polyphonic 和 inZhRange 属性
This commit is contained in:
@@ -122,6 +122,34 @@ const getPinyinWithoutTone: GetPinyinWithoutTone = (pinyin) => {
|
||||
.replace(/ḿ|m̀/g, 'm');
|
||||
};
|
||||
|
||||
/**
|
||||
* @description: 获取单字符的多音拼音
|
||||
* @param {string} word
|
||||
* @return {WordResult[]}
|
||||
*/
|
||||
type GetAllPinyin = (
|
||||
word: string,
|
||||
mode?: PinyinMode
|
||||
) => string[];
|
||||
export const getAllPinyin: GetAllPinyin = (word, mode = "normal") => {
|
||||
const wordCode = word.charCodeAt(0);
|
||||
const customMultpileDict = getCustomMultpileDict();
|
||||
let pinyin = DICT1[wordCode] ? DICT1[wordCode].split(" ") : [];
|
||||
if (customMultpileDict[wordCode]) {
|
||||
pinyin = customMultpileDict[wordCode]
|
||||
? customMultpileDict[wordCode].split(" ")
|
||||
: [];
|
||||
} else if (mode === "surname") {
|
||||
const surnamePinyin = Surnames[word];
|
||||
if (surnamePinyin) {
|
||||
pinyin = [surnamePinyin].concat(
|
||||
pinyin.filter((py) => py !== surnamePinyin)
|
||||
);
|
||||
}
|
||||
}
|
||||
return pinyin;
|
||||
};
|
||||
|
||||
/**
|
||||
* @description: 获取单字符的多音拼音
|
||||
* @param {string} word
|
||||
@@ -132,23 +160,10 @@ type GetMultiplePinyin = (
|
||||
mode?: PinyinMode
|
||||
) => SingleWordResult[];
|
||||
const getMultiplePinyin: GetMultiplePinyin = (word, mode = 'normal') => {
|
||||
const wordCode = word.charCodeAt(0);
|
||||
const customMultpileDict = getCustomMultpileDict();
|
||||
let pinyin = DICT1[wordCode] || '';
|
||||
if (customMultpileDict[wordCode]) {
|
||||
pinyin = customMultpileDict[wordCode];
|
||||
} else if (mode === 'surname') {
|
||||
const surnamePinyin = Surnames[word];
|
||||
if (surnamePinyin) {
|
||||
pinyin = [
|
||||
surnamePinyin,
|
||||
pinyin.split(' ').filter(py => py !== surnamePinyin),
|
||||
].join(' ');
|
||||
}
|
||||
}
|
||||
let pinyin = getAllPinyin(word, mode);
|
||||
|
||||
if (pinyin) {
|
||||
return pinyin.split(' ').map((value) => ({
|
||||
return pinyin.map((value) => ({
|
||||
origin: word,
|
||||
result: value,
|
||||
isZh: true,
|
||||
|
||||
@@ -90,6 +90,8 @@ interface AllData {
|
||||
finalBody: string;
|
||||
finalTail: string;
|
||||
isZh: boolean;
|
||||
polyphonic: string[];
|
||||
inZhRange: boolean;
|
||||
}
|
||||
|
||||
interface OptionsReturnString extends BasicOptions {
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { getStringLength } from '@/common/utils';
|
||||
import { getStringLength, isZhChar } from '@/common/utils';
|
||||
import type { SingleWordResult } from '../../common/type';
|
||||
import {
|
||||
DoubleUnicodePrefixReg,
|
||||
DoubleUnicodeSuffixReg,
|
||||
} from '@/common/constant';
|
||||
import { getMultiplePinyin } from './handle';
|
||||
import { getAllPinyin, getMultiplePinyin } from './handle';
|
||||
import { CompleteOptions } from './index';
|
||||
import {
|
||||
getNumOfTone,
|
||||
@@ -182,6 +182,14 @@ export const middlewareType = (
|
||||
const pinyin = item.isZh ? item.result : '';
|
||||
const { initial, final } = getInitialAndFinal(pinyin);
|
||||
const { head, body, tail } = getFinalParts(pinyin);
|
||||
let polyphonic: string[] = [];
|
||||
if (pinyin) {
|
||||
polyphonic = [pinyin].concat(
|
||||
getAllPinyin(item.origin, options.mode).filter(
|
||||
(item) => item !== pinyin
|
||||
)
|
||||
);
|
||||
}
|
||||
return {
|
||||
origin: item.origin,
|
||||
pinyin,
|
||||
@@ -193,6 +201,8 @@ export const middlewareType = (
|
||||
finalTail: tail,
|
||||
num: Number(getNumOfTone(item.originPinyin)),
|
||||
isZh: item.isZh,
|
||||
polyphonic,
|
||||
inZhRange: isZhChar(item.origin),
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ import {
|
||||
getFinalParts,
|
||||
} from '@/core/pinyin/handle';
|
||||
import { getCustomPolyphonicDict } from '../custom';
|
||||
import { isZhChar } from '@/common/utils';
|
||||
|
||||
interface BasicOptions {
|
||||
/**
|
||||
@@ -70,6 +71,7 @@ interface AllData {
|
||||
finalBody: string;
|
||||
finalTail: string;
|
||||
isZh: boolean;
|
||||
inZhRange: boolean;
|
||||
}
|
||||
|
||||
interface OptionsReturnString extends BasicOptions {
|
||||
@@ -257,6 +259,7 @@ export const handleType = (
|
||||
finalTail: tail,
|
||||
num: Number(getNumOfTone(item.originPinyin)),
|
||||
isZh: item.isZh,
|
||||
inZhRange: isZhChar(item.origin),
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
@@ -18,6 +18,8 @@ describe('all', () => {
|
||||
finalTail: 'n',
|
||||
num: 4,
|
||||
isZh: true,
|
||||
inZhRange: true,
|
||||
polyphonic: ['hàn'],
|
||||
},
|
||||
{
|
||||
origin: '语',
|
||||
@@ -30,6 +32,8 @@ describe('all', () => {
|
||||
finalTail: '',
|
||||
num: 3,
|
||||
isZh: true,
|
||||
inZhRange: true,
|
||||
polyphonic: ['yǔ', "yù"],
|
||||
},
|
||||
{
|
||||
origin: '拼',
|
||||
@@ -42,6 +46,8 @@ describe('all', () => {
|
||||
finalTail: 'n',
|
||||
num: 1,
|
||||
isZh: true,
|
||||
inZhRange: true,
|
||||
polyphonic: ['pīn'],
|
||||
},
|
||||
{
|
||||
origin: '音',
|
||||
@@ -54,6 +60,8 @@ describe('all', () => {
|
||||
finalTail: 'n',
|
||||
num: 1,
|
||||
isZh: true,
|
||||
inZhRange: true,
|
||||
polyphonic: ['yīn'],
|
||||
},
|
||||
]);
|
||||
});
|
||||
@@ -74,6 +82,8 @@ describe('all', () => {
|
||||
finalTail: 'n',
|
||||
num: 4,
|
||||
isZh: true,
|
||||
inZhRange: true,
|
||||
polyphonic: ['hàn'],
|
||||
},
|
||||
{
|
||||
origin: 'a',
|
||||
@@ -86,6 +96,8 @@ describe('all', () => {
|
||||
finalTail: '',
|
||||
num: 0,
|
||||
isZh: false,
|
||||
inZhRange: false,
|
||||
polyphonic: [],
|
||||
},
|
||||
{
|
||||
origin: '𧒽',
|
||||
@@ -98,6 +110,8 @@ describe('all', () => {
|
||||
finalTail: '',
|
||||
num: 0,
|
||||
isZh: false,
|
||||
inZhRange: false,
|
||||
polyphonic: [],
|
||||
},
|
||||
{
|
||||
origin: '音',
|
||||
@@ -110,6 +124,8 @@ describe('all', () => {
|
||||
finalTail: 'n',
|
||||
num: 1,
|
||||
isZh: true,
|
||||
inZhRange: true,
|
||||
polyphonic: ['yīn'],
|
||||
},
|
||||
]);
|
||||
});
|
||||
@@ -131,6 +147,8 @@ describe('all', () => {
|
||||
finalTail: 'n',
|
||||
num: 4,
|
||||
isZh: true,
|
||||
inZhRange: true,
|
||||
polyphonic: ['hàn'],
|
||||
},
|
||||
{
|
||||
origin: '音',
|
||||
@@ -143,6 +161,8 @@ describe('all', () => {
|
||||
finalTail: 'n',
|
||||
num: 1,
|
||||
isZh: true,
|
||||
inZhRange: true,
|
||||
polyphonic: ['yīn'],
|
||||
},
|
||||
]);
|
||||
});
|
||||
@@ -164,6 +184,8 @@ describe('all', () => {
|
||||
finalTail: 'n',
|
||||
num: 4,
|
||||
isZh: true,
|
||||
inZhRange: true,
|
||||
polyphonic: ['hàn'],
|
||||
},
|
||||
{
|
||||
origin: 'a𧒽',
|
||||
@@ -176,6 +198,8 @@ describe('all', () => {
|
||||
finalTail: '',
|
||||
num: 0,
|
||||
isZh: false,
|
||||
inZhRange: false,
|
||||
polyphonic: [],
|
||||
},
|
||||
{
|
||||
origin: '音',
|
||||
@@ -188,6 +212,8 @@ describe('all', () => {
|
||||
finalTail: 'n',
|
||||
num: 1,
|
||||
isZh: true,
|
||||
inZhRange: true,
|
||||
polyphonic: ['yīn'],
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
@@ -29,7 +29,7 @@ describe('multiple', () => {
|
||||
|
||||
it('[multiple]非字符串', () => {
|
||||
const result = pinyin('a', { multiple: true, type: 'array' });
|
||||
expect(result).to.deep.equal(['a']);
|
||||
expect(result).to.deep.equal([]);
|
||||
});
|
||||
|
||||
it('[multiple]multiple+surname同时使用', () => {
|
||||
|
||||
@@ -32,6 +32,7 @@ describe('polyphonic', () => {
|
||||
num: 3,
|
||||
origin: '好',
|
||||
pinyin: 'hǎo',
|
||||
inZhRange: true,
|
||||
},
|
||||
{
|
||||
final: 'ào',
|
||||
@@ -44,6 +45,7 @@ describe('polyphonic', () => {
|
||||
num: 4,
|
||||
origin: '好',
|
||||
pinyin: 'hào',
|
||||
inZhRange: true,
|
||||
},
|
||||
],
|
||||
[
|
||||
@@ -58,6 +60,7 @@ describe('polyphonic', () => {
|
||||
num: 3,
|
||||
origin: '好',
|
||||
pinyin: 'hǎo',
|
||||
inZhRange: true,
|
||||
},
|
||||
{
|
||||
final: 'ào',
|
||||
@@ -70,6 +73,7 @@ describe('polyphonic', () => {
|
||||
num: 4,
|
||||
origin: '好',
|
||||
pinyin: 'hào',
|
||||
inZhRange: true,
|
||||
},
|
||||
],
|
||||
[
|
||||
@@ -84,6 +88,7 @@ describe('polyphonic', () => {
|
||||
num: 2,
|
||||
origin: '学',
|
||||
pinyin: 'xué',
|
||||
inZhRange: true,
|
||||
},
|
||||
],
|
||||
[
|
||||
@@ -98,6 +103,7 @@ describe('polyphonic', () => {
|
||||
num: 2,
|
||||
origin: '习',
|
||||
pinyin: 'xí',
|
||||
inZhRange: true,
|
||||
},
|
||||
],
|
||||
]);
|
||||
@@ -133,6 +139,7 @@ describe('polyphonic', () => {
|
||||
num: 3,
|
||||
origin: '好',
|
||||
pinyin: 'hǎo',
|
||||
inZhRange: true,
|
||||
},
|
||||
{
|
||||
final: 'ào',
|
||||
@@ -145,6 +152,7 @@ describe('polyphonic', () => {
|
||||
num: 4,
|
||||
origin: '好',
|
||||
pinyin: 'hào',
|
||||
inZhRange: true,
|
||||
},
|
||||
],
|
||||
[
|
||||
@@ -159,6 +167,7 @@ describe('polyphonic', () => {
|
||||
num: 3,
|
||||
origin: '好',
|
||||
pinyin: 'hǎo',
|
||||
inZhRange: true,
|
||||
},
|
||||
{
|
||||
final: 'ào',
|
||||
@@ -171,6 +180,7 @@ describe('polyphonic', () => {
|
||||
num: 4,
|
||||
origin: '好',
|
||||
pinyin: 'hào',
|
||||
inZhRange: true,
|
||||
},
|
||||
],
|
||||
[
|
||||
@@ -185,6 +195,7 @@ describe('polyphonic', () => {
|
||||
num: 2,
|
||||
origin: '学',
|
||||
pinyin: 'xué',
|
||||
inZhRange: true,
|
||||
},
|
||||
],
|
||||
[
|
||||
@@ -199,6 +210,7 @@ describe('polyphonic', () => {
|
||||
num: 2,
|
||||
origin: '习',
|
||||
pinyin: 'xí',
|
||||
inZhRange: true,
|
||||
},
|
||||
],
|
||||
[
|
||||
@@ -213,6 +225,7 @@ describe('polyphonic', () => {
|
||||
num: 0,
|
||||
origin: 's',
|
||||
pinyin: '',
|
||||
inZhRange: false,
|
||||
},
|
||||
],
|
||||
]);
|
||||
|
||||
7
types/core/pinyin/handle.d.ts
vendored
7
types/core/pinyin/handle.d.ts
vendored
@@ -14,6 +14,13 @@ export declare const getPinyin: (word: string, list: SingleWordResult[], mode: '
|
||||
*/
|
||||
type GetPinyinWithoutTone = (pinyin: string) => string;
|
||||
declare const getPinyinWithoutTone: GetPinyinWithoutTone;
|
||||
/**
|
||||
* @description: 获取单字符的多音拼音
|
||||
* @param {string} word
|
||||
* @return {WordResult[]}
|
||||
*/
|
||||
type GetAllPinyin = (word: string, mode?: PinyinMode) => string[];
|
||||
export declare const getAllPinyin: GetAllPinyin;
|
||||
/**
|
||||
* @description: 获取单字符的多音拼音
|
||||
* @param {string} word
|
||||
|
||||
2
types/core/pinyin/index.d.ts
vendored
2
types/core/pinyin/index.d.ts
vendored
@@ -67,6 +67,8 @@ interface AllData {
|
||||
finalBody: string;
|
||||
finalTail: string;
|
||||
isZh: boolean;
|
||||
polyphonic: string[];
|
||||
inZhRange: boolean;
|
||||
}
|
||||
interface OptionsReturnString extends BasicOptions {
|
||||
/**
|
||||
|
||||
2
types/core/pinyin/middlewares.d.ts
vendored
2
types/core/pinyin/middlewares.d.ts
vendored
@@ -17,6 +17,8 @@ export declare const middlewareType: (list: SingleWordResult[], options: Complet
|
||||
finalTail: string;
|
||||
num: number;
|
||||
isZh: boolean;
|
||||
polyphonic: string[];
|
||||
inZhRange: boolean;
|
||||
}[];
|
||||
export declare const middlewareDoubleUnicode: (list: SingleWordResult[]) => SingleWordResult[];
|
||||
export declare const middlewareToneSandhi: (list: SingleWordResult[], toneSandhi: boolean) => SingleWordResult[];
|
||||
|
||||
2
types/core/polyphonic/index.d.ts
vendored
2
types/core/polyphonic/index.d.ts
vendored
@@ -44,6 +44,7 @@ interface AllData {
|
||||
finalBody: string;
|
||||
finalTail: string;
|
||||
isZh: boolean;
|
||||
inZhRange: boolean;
|
||||
}
|
||||
interface OptionsReturnString extends BasicOptions {
|
||||
/**
|
||||
@@ -113,5 +114,6 @@ export declare const handleType: (list: SingleWordResult[], options: CompleteOpt
|
||||
finalTail: string;
|
||||
num: number;
|
||||
isZh: boolean;
|
||||
inZhRange: boolean;
|
||||
}[];
|
||||
export { polyphonic };
|
||||
|
||||
Reference in New Issue
Block a user