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: keep consistent for first #279

Merged
merged 4 commits into from
Nov 2, 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
10 changes: 4 additions & 6 deletions lib/core/pinyin/handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import { getCustomMultpileDict } from "@/core/custom";
import { SingleWordResult } from "../../common/type";
import type { SurnameMode } from "../../common/type";
import { acTree, MatchPattern, TokenizationAlgorithm } from "../../common/segmentit";
import {
Priority,
} from "@/common/constant";
import { Priority } from "@/common/constant";
import { splitString } from "@/common/utils";

/**
Expand Down Expand Up @@ -275,12 +273,12 @@ const getPinyinWithNum: GetPinyinWithNum = (pinyin, originPinyin) => {
* @param {string} pinyin
* @return {string}
*/
type GetFirstLetter = (pinyin: string) => string;
const getFirstLetter: GetFirstLetter = (pinyin) => {
type GetFirstLetter = (pinyin: string, isZh: boolean) => string;
const getFirstLetter: GetFirstLetter = (pinyin: string, isZh: boolean) => {
const first_letter_arr: string[] = [];
const pinyin_arr = pinyin.split(" ");
pinyin_arr.forEach((pinyin) => {
first_letter_arr.push(pinyin[0]);
first_letter_arr.push(isZh ? pinyin[0] : pinyin);
});
return first_letter_arr.join(" ");
};
Expand Down
5 changes: 2 additions & 3 deletions lib/core/pinyin/middlewares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ export const middlewarePattern = (
break;
case "first":
list.forEach((item) => {
// todo: first 暂时不作为拼音一部分,不进行 isZh 识别
item.result = getFirstLetter(item.result);
item.result = getFirstLetter(item.result, item.isZh);
});
break;
case "finalHead":
Expand Down Expand Up @@ -199,7 +198,7 @@ export const middlewareType = (
pinyin,
initial,
final,
first: item.isZh ? getFirstLetter(item.result) : "",
first: getFirstLetter(item.result, item.isZh),
finalHead: head,
finalBody: body,
finalTail: tail,
Expand Down
2 changes: 1 addition & 1 deletion lib/core/polyphonic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ export const handleType = (
pinyin,
initial,
final,
first: item.isZh ? getFirstLetter(item.result) : '',
first: getFirstLetter(item.result, item.isZh),
finalHead: head,
finalBody: body,
finalTail: tail,
Expand Down
6 changes: 3 additions & 3 deletions test/all.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('all', () => {
pinyin: '',
initial: '',
final: '',
first: '',
first: 'a',
finalHead: '',
finalBody: '',
finalTail: '',
Expand All @@ -114,7 +114,7 @@ describe('all', () => {
pinyin: '',
initial: '',
final: '',
first: '',
first: '𧒽',
finalHead: '',
finalBody: '',
finalTail: '',
Expand Down Expand Up @@ -207,7 +207,7 @@ describe('all', () => {
pinyin: '',
initial: '',
final: '',
first: '',
first: 'a𧒽',
finalHead: '',
finalBody: '',
finalTail: '',
Expand Down
2 changes: 1 addition & 1 deletion test/polyphonic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ describe('polyphonic', () => {
finalBody: '',
finalHead: '',
finalTail: '',
first: '',
first: 's',
initial: '',
isZh: false,
num: 0,
Expand Down
Loading