Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 增加变调开关 #185

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/core/html/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ interface HtmlOptions {
customClassMap?: {
[classname: string]: string[];
};
/**
* @description 是否对"一"和"不"开启变调。默认开启。参考:https://zh.wiktionary.org/wiki/Appendix:%E2%80%9C%E4%B8%80%E2%80%9D%E5%8F%8A%E2%80%9C%E4%B8%8D%E2%80%9D%E7%9A%84%E5%8F%98%E8%B0%83
* @value true:开启
* @value false:不开启
*/
inflection?: boolean;
}

const DefaultHtmlOptions: HtmlOptions = {
Expand All @@ -41,6 +47,7 @@ const DefaultHtmlOptions: HtmlOptions = {
wrapNonChinese: false,
toneType: 'symbol',
customClassMap: {},
inflection: true,
};

/**
Expand All @@ -57,6 +64,7 @@ export const html = (text: string, options?: HtmlOptions) => {
const pinyinArray = pinyin(text, {
type: 'all',
toneType: completeOptions.toneType,
inflection: options?.inflection,
});
const result = pinyinArray.map((item) => {
let additionalClass = '';
Expand Down
11 changes: 11 additions & 0 deletions lib/core/pinyin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
middlewareV,
middlewareType,
middlewareDoubleUnicode,
middlewareInflection,
} from './middlewares';

interface BasicOptions {
Expand Down Expand Up @@ -70,6 +71,12 @@ interface BasicOptions {
* @value true:返回值中 ü 转换成 v
*/
v?: boolean;
/**
* @description 是否对"一"和"不"开启变调。默认开启。参考:https://zh.wiktionary.org/wiki/Appendix:%E2%80%9C%E4%B8%80%E2%80%9D%E5%8F%8A%E2%80%9C%E4%B8%8D%E2%80%9D%E7%9A%84%E5%8F%98%E8%B0%83
* @value true:开启
* @value false:不开启
*/
inflection?: boolean;
}

interface AllData {
Expand Down Expand Up @@ -143,6 +150,7 @@ const DEFAULT_OPTIONS: CompleteOptions = {
nonZh: 'spaced',
v: false,
separator: ' ',
inflection: true,
};

/**
Expand Down Expand Up @@ -207,6 +215,9 @@ function pinyin(

list = getPinyin(word, list, options.mode || 'normal');

// 一和不变调处理
list = middlewareInflection(list, options.inflection as boolean);

// 双 unicode 编码字符处理
list = middlewareDoubleUnicode(list);

Expand Down
17 changes: 17 additions & 0 deletions lib/core/pinyin/middlewares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,20 @@ export const middlewareDoubleUnicode = (
});
return list;
};

// 是否开启变调
export const middlewareInflection = (
list: SingleWordResult[],
inflection: boolean,
): SingleWordResult[] => {
if (inflection === false) {
list.forEach(item => {
if (item.origin === '一') {
item.result = item.originPinyin = 'yī';
} else if (item.origin === '不') {
item.result = item.originPinyin = 'bù';
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个会影响性能吗?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

关闭变调时会增加一遍 O(n) 的遍历,之前整体的性能大概在 10 * O(n) 左右,影响不大,只要时间复杂度整体是 O(n) 都可以接受

})
}
return list;
};
7 changes: 6 additions & 1 deletion test/special.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('number', () => {

it('[number]数字发音 一行人', () => {
const result = pinyin('一行人');
expect(result).to.be.equal(' xíng rén');
expect(result).to.be.equal(' xíng rén');
});

it('[number]数字发音 二百零一行', () => {
Expand Down Expand Up @@ -64,4 +64,9 @@ describe('special change tone', () => {
const result = pinyin('一会儿');
expect(result).to.be.equal('yí huì er');
});

it('[special change tone]一会儿', () => {
const result = pinyin('一会儿', { inflection: false });
expect(result).to.be.equal('yī huì er');
});
});
6 changes: 6 additions & 0 deletions types/core/html/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ interface HtmlOptions {
customClassMap?: {
[classname: string]: string[];
};
/**
* @description 是否对"一"和"不"开启变调。默认开启。参考:https://zh.wiktionary.org/wiki/Appendix:%E2%80%9C%E4%B8%80%E2%80%9D%E5%8F%8A%E2%80%9C%E4%B8%8D%E2%80%9D%E7%9A%84%E5%8F%98%E8%B0%83
* @value true:开启
* @value false:不开启
*/
inflection?: boolean;
}
/**
* @description: 获取带拼音汉字的 html 字符串
Expand Down
6 changes: 6 additions & 0 deletions types/core/pinyin/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ interface BasicOptions {
* @value true:返回值中 ü 转换成 v
*/
v?: boolean;
/**
* @description 是否对"一"和"不"开启变调。默认开启。参考:https://zh.wiktionary.org/wiki/Appendix:%E2%80%9C%E4%B8%80%E2%80%9D%E5%8F%8A%E2%80%9C%E4%B8%8D%E2%80%9D%E7%9A%84%E5%8F%98%E8%B0%83
* @value true:开启
* @value false:不开启
*/
inflection?: boolean;
}
interface AllData {
origin: string;
Expand Down
1 change: 1 addition & 0 deletions types/core/pinyin/middlewares.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ export declare const middlewareType: (list: SingleWordResult[], options: Complet
isZh: boolean;
}[];
export declare const middlewareDoubleUnicode: (list: SingleWordResult[]) => SingleWordResult[];
export declare const middlewareInflection: (list: SingleWordResult[], inflection: boolean) => SingleWordResult[];
Loading