Skip to content

Commit

Permalink
Merge pull request #279 from zh-lx/feature-first-consistent
Browse files Browse the repository at this point in the history
feat: keep consistent for first
  • Loading branch information
zh-lx authored Nov 2, 2024
2 parents 77d8ae9 + 5179bc1 commit fa07de9
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 14 deletions.
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

0 comments on commit fa07de9

Please sign in to comment.