Skip to content

Commit

Permalink
Merge pull request #208 from zh-lx/feat-all-extend
Browse files Browse the repository at this point in the history
feat: pinyin all 模式新增 polyphonic 和 inZhRange 属性
  • Loading branch information
zh-lx authored Mar 29, 2024
2 parents 0b813bb + 003c9a0 commit e923e97
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 14 deletions.
37 changes: 26 additions & 11 deletions lib/core/pinyin/handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,28 +127,43 @@ const getPinyinWithoutTone: GetPinyinWithoutTone = (pinyin) => {
* @param {string} word
* @return {WordResult[]}
*/
type GetMultiplePinyin = (
type GetAllPinyin = (
word: string,
mode?: PinyinMode
) => SingleWordResult[];
const getMultiplePinyin: GetMultiplePinyin = (word, mode = 'normal') => {
) => string[];
export const getAllPinyin: GetAllPinyin = (word, mode = "normal") => {
const wordCode = word.charCodeAt(0);
const customMultpileDict = getCustomMultpileDict();
let pinyin = DICT1[wordCode] || '';
let pinyin = DICT1[wordCode] ? DICT1[wordCode].split(" ") : [];
if (customMultpileDict[wordCode]) {
pinyin = customMultpileDict[wordCode];
} else if (mode === 'surname') {
pinyin = customMultpileDict[wordCode]
? customMultpileDict[wordCode].split(" ")
: [];
} else if (mode === "surname") {
const surnamePinyin = Surnames[word];
if (surnamePinyin) {
pinyin = [
surnamePinyin,
pinyin.split(' ').filter(py => py !== surnamePinyin),
].join(' ');
pinyin = [surnamePinyin].concat(
pinyin.filter((py) => py !== surnamePinyin)
);
}
}
return pinyin;
};

/**
* @description: 获取单字符的多音拼音
* @param {string} word
* @return {WordResult[]}
*/
type GetMultiplePinyin = (
word: string,
mode?: PinyinMode
) => SingleWordResult[];
const getMultiplePinyin: GetMultiplePinyin = (word, mode = 'normal') => {
let pinyin = getAllPinyin(word, mode);

if (pinyin) {
return pinyin.split(' ').map((value) => ({
return pinyin.map((value) => ({
origin: word,
result: value,
isZh: true,
Expand Down
2 changes: 2 additions & 0 deletions lib/core/pinyin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ interface AllData {
finalBody: string;
finalTail: string;
isZh: boolean;
polyphonic: string[];
inZhRange: boolean;
}

interface OptionsReturnString extends BasicOptions {
Expand Down
14 changes: 12 additions & 2 deletions lib/core/pinyin/middlewares.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -193,6 +201,8 @@ export const middlewareType = (
finalTail: tail,
num: Number(getNumOfTone(item.originPinyin)),
isZh: item.isZh,
polyphonic,
inZhRange: isZhChar(item.origin),
};
});
}
Expand Down
3 changes: 3 additions & 0 deletions lib/core/polyphonic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
getFinalParts,
} from '@/core/pinyin/handle';
import { getCustomPolyphonicDict } from '../custom';
import { isZhChar } from '@/common/utils';

interface BasicOptions {
/**
Expand Down Expand Up @@ -70,6 +71,7 @@ interface AllData {
finalBody: string;
finalTail: string;
isZh: boolean;
inZhRange: boolean;
}

interface OptionsReturnString extends BasicOptions {
Expand Down Expand Up @@ -257,6 +259,7 @@ export const handleType = (
finalTail: tail,
num: Number(getNumOfTone(item.originPinyin)),
isZh: item.isZh,
inZhRange: isZhChar(item.origin),
};
});
}
Expand Down
26 changes: 26 additions & 0 deletions test/all.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ describe('all', () => {
finalTail: 'n',
num: 4,
isZh: true,
inZhRange: true,
polyphonic: ['hàn'],
},
{
origin: '语',
Expand All @@ -30,6 +32,8 @@ describe('all', () => {
finalTail: '',
num: 3,
isZh: true,
inZhRange: true,
polyphonic: ['yǔ', "yù"],
},
{
origin: '拼',
Expand All @@ -42,6 +46,8 @@ describe('all', () => {
finalTail: 'n',
num: 1,
isZh: true,
inZhRange: true,
polyphonic: ['pīn'],
},
{
origin: '音',
Expand All @@ -54,6 +60,8 @@ describe('all', () => {
finalTail: 'n',
num: 1,
isZh: true,
inZhRange: true,
polyphonic: ['yīn'],
},
]);
});
Expand All @@ -74,6 +82,8 @@ describe('all', () => {
finalTail: 'n',
num: 4,
isZh: true,
inZhRange: true,
polyphonic: ['hàn'],
},
{
origin: 'a',
Expand All @@ -86,6 +96,8 @@ describe('all', () => {
finalTail: '',
num: 0,
isZh: false,
inZhRange: false,
polyphonic: [],
},
{
origin: '𧒽',
Expand All @@ -98,6 +110,8 @@ describe('all', () => {
finalTail: '',
num: 0,
isZh: false,
inZhRange: false,
polyphonic: [],
},
{
origin: '音',
Expand All @@ -110,6 +124,8 @@ describe('all', () => {
finalTail: 'n',
num: 1,
isZh: true,
inZhRange: true,
polyphonic: ['yīn'],
},
]);
});
Expand All @@ -131,6 +147,8 @@ describe('all', () => {
finalTail: 'n',
num: 4,
isZh: true,
inZhRange: true,
polyphonic: ['hàn'],
},
{
origin: '音',
Expand All @@ -143,6 +161,8 @@ describe('all', () => {
finalTail: 'n',
num: 1,
isZh: true,
inZhRange: true,
polyphonic: ['yīn'],
},
]);
});
Expand All @@ -164,6 +184,8 @@ describe('all', () => {
finalTail: 'n',
num: 4,
isZh: true,
inZhRange: true,
polyphonic: ['hàn'],
},
{
origin: 'a𧒽',
Expand All @@ -176,6 +198,8 @@ describe('all', () => {
finalTail: '',
num: 0,
isZh: false,
inZhRange: false,
polyphonic: [],
},
{
origin: '音',
Expand All @@ -188,6 +212,8 @@ describe('all', () => {
finalTail: 'n',
num: 1,
isZh: true,
inZhRange: true,
polyphonic: ['yīn'],
},
]);
});
Expand Down
2 changes: 1 addition & 1 deletion test/multiple.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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同时使用', () => {
Expand Down
13 changes: 13 additions & 0 deletions test/polyphonic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('polyphonic', () => {
num: 3,
origin: '好',
pinyin: 'hǎo',
inZhRange: true,
},
{
final: 'ào',
Expand All @@ -44,6 +45,7 @@ describe('polyphonic', () => {
num: 4,
origin: '好',
pinyin: 'hào',
inZhRange: true,
},
],
[
Expand All @@ -58,6 +60,7 @@ describe('polyphonic', () => {
num: 3,
origin: '好',
pinyin: 'hǎo',
inZhRange: true,
},
{
final: 'ào',
Expand All @@ -70,6 +73,7 @@ describe('polyphonic', () => {
num: 4,
origin: '好',
pinyin: 'hào',
inZhRange: true,
},
],
[
Expand All @@ -84,6 +88,7 @@ describe('polyphonic', () => {
num: 2,
origin: '学',
pinyin: 'xué',
inZhRange: true,
},
],
[
Expand All @@ -98,6 +103,7 @@ describe('polyphonic', () => {
num: 2,
origin: '习',
pinyin: 'xí',
inZhRange: true,
},
],
]);
Expand Down Expand Up @@ -133,6 +139,7 @@ describe('polyphonic', () => {
num: 3,
origin: '好',
pinyin: 'hǎo',
inZhRange: true,
},
{
final: 'ào',
Expand All @@ -145,6 +152,7 @@ describe('polyphonic', () => {
num: 4,
origin: '好',
pinyin: 'hào',
inZhRange: true,
},
],
[
Expand All @@ -159,6 +167,7 @@ describe('polyphonic', () => {
num: 3,
origin: '好',
pinyin: 'hǎo',
inZhRange: true,
},
{
final: 'ào',
Expand All @@ -171,6 +180,7 @@ describe('polyphonic', () => {
num: 4,
origin: '好',
pinyin: 'hào',
inZhRange: true,
},
],
[
Expand All @@ -185,6 +195,7 @@ describe('polyphonic', () => {
num: 2,
origin: '学',
pinyin: 'xué',
inZhRange: true,
},
],
[
Expand All @@ -199,6 +210,7 @@ describe('polyphonic', () => {
num: 2,
origin: '习',
pinyin: 'xí',
inZhRange: true,
},
],
[
Expand All @@ -213,6 +225,7 @@ describe('polyphonic', () => {
num: 0,
origin: 's',
pinyin: '',
inZhRange: false,
},
],
]);
Expand Down
7 changes: 7 additions & 0 deletions types/core/pinyin/handle.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions types/core/pinyin/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ interface AllData {
finalBody: string;
finalTail: string;
isZh: boolean;
polyphonic: string[];
inZhRange: boolean;
}
interface OptionsReturnString extends BasicOptions {
/**
Expand Down
2 changes: 2 additions & 0 deletions types/core/pinyin/middlewares.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Loading

0 comments on commit e923e97

Please sign in to comment.