Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: zh-lx/pinyin-pro
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: main
Choose a base ref
...
head repository: GitbookIO/pinyin-pro
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 4 commits
  • 26 files changed
  • 2 contributors

Commits on Jan 6, 2025

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    b4e722d View commit details
  2. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    5a9307e View commit details
  3. Copy the full SHA
    040d643 View commit details
  4. 3.26.0-gitbook4

    emmerich committed Jan 6, 2025
    Copy the full SHA
    8c71613 View commit details
213 changes: 2 additions & 211 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,212 +1,3 @@
<a href="https://github.com/zh-lx/pinyin-pro"><img src="https://cdn.jsdelivr.net/gh/zh-lx/static-img/pinyin-pro/logo.svg" alt="logo" width="500" /></a>
### pinyin-pro

[![NPM version](https://img.shields.io/npm/v/pinyin-pro.svg)](https://www.npmjs.com/package/pinyin-pro)
[![GITHUB star](https://img.shields.io/github/stars/zh-lx/pinyin-pro.svg)](https://github.com/zh-lx/pinyin-pro)
[![build-passing](https://img.shields.io/github/actions/workflow/status/zh-lx/pinyin-pro/ci.yaml)](https://github.com/zh-lx/pinyin-pro/actions)
[![NPM Downloads](https://img.shields.io/npm/dm/pinyin-pro.svg)](https://npmcharts.netlify.app/compare/pinyin-pro?minimal=true)
[![Coverage Status](https://img.shields.io/codecov/c/github/zh-lx/pinyin-pro)](https://app.codecov.io/gh/zh-lx/pinyin-pro)
[![DeepScan grade](https://deepscan.io/api/teams/20303/projects/26161/branches/829070/badge/grade.svg)](https://deepscan.io/dashboard#view=project&tid=20303&pid=26161&bid=829070)
[![MIT-license](https://img.shields.io/npm/l/pinyin-pro.svg)](https://opensource.org/licenses/MIT)
[![npm bundle size](https://img.shields.io/bundlephobia/minzip/pinyin-pro)](https://bundlephobia.com/package/pinyin-pro)
[![GITHUB-language](https://img.shields.io/github/languages/top/zh-lx/pinyin-pro.svg)](https://github.com/zh-lx/pinyin-pro)

### 📖 介绍

`pinyin-pro` 是一个专业的 js 汉字拼音转换库,功能丰富、准确率高、性能优异。

[中文文档](https://pinyin-pro.cn) | [English Docs](https://pinyin-pro.cn/en) | [在线运行](https://pinyin-pro.cn/run/run)

### 🎨 特色功能

- 支持拼音/声母/韵母/首字母/音调/全部信息
- 支持人名姓氏模式
- 支持文本和拼音匹配
- 支持自定义拼音
- 支持获取带拼音汉字的 HTML 字符串
- 支持获取汉字的所有拼音
- 支持拼音输入转换
- 极致的性能和极高的拼音识别准确率

### 🔨 安装

npm 安装

```html
npm install pinyin-pro
```

浏览器引入

```html
<script src="https://unpkg.com/pinyin-pro"></script>
```

### 💡 使用示例

全部功能的使用说明文档请查看[在线文档](https://pinyin-pro.cn/use/pinyin.html)

- 获取拼音,更多功能请查看[pinyin API](https://pinyin-pro.cn/use/pinyin.html)

```js
import { pinyin } from "pinyin-pro";

// 获取字符串格式拼音
pinyin("汉语拼音"); // 'hàn yǔ pīn yīn'

// 获取数组格式拼音
pinyin("汉语拼音", { type: "array" }); // ["hàn", "yǔ", "pīn", "yīn"]

// 获取不带音调格式拼音
pinyin("汉语拼音", { toneType: "none" }); // "han yu pin yin"

// 获取不带音调数组格式拼音
pinyin("汉语拼音", { toneType: "none", type: "array" }); // ["han", "yu", "pin", "yin"]

// 音调以数字形式显示
pinyin("汉语拼音", { toneType: "num" }); // "han4 yu3 pin1 yin1"

// 自动识别多音字
pinyin("睡着了"); // "shuì zháo le"
```

- 文本和拼音匹配,更多匹配规则请查看[match API](https://pinyin-pro.cn/use/match.html)

```js
import { match } from "pinyin-pro";

// 支持首字母匹配
match("中文拼音", "zwp"); // [0, 1, 2]

// 支持全拼匹配
match("中文拼音", "zhongwenpin"); // [0, 1, 2]

// 支持混合匹配
match("中文拼音", "zhongwp"); // [0, 1, 2]
```

- 拼音格式转换,更多功能请查看[convert API](https://pinyin-pro.cn/use/convert.html)

```js
import { convert } from "pinyin-pro";

// 数组转符号
convert("pin1 yin1"); // 'pīn yīn'

// 符号转数字
convert("pīn yīn", { format: "symbolToNum" }); // 'pin1 yin1'

// 消除符号
convert("pīn yīn", { format: "toneNone" }); // 'pin yin'
```

- 获取带汉字拼音的 HTML 字符串,更多配置请查看[html API](https://pinyin-pro.cn/use/html.html)

```js
import { html } from "pinyin-pro";

// 带拼音汉字的 HTML 字符串
html("汉语拼音");
/*
<span class="py-result-item">
<ruby>
<span class="py-chinese-item">汉</span>
<rp>(</rp>
<rt class="py-pinyin-item">hàn</rt>
<rp>)</rp>
</ruby>
</span>
<span class="py-result-item">
<ruby>
<span class="py-chinese-item">语</span>
<rp>(</rp>
<rt class="py-pinyin-item">yǔ</rt>
<rp>)</rp>
</ruby>
</span>
*/
```

上述结果浏览器中预览效果如下:
<span class="py-result-item">
<ruby>
<span class="py-chinese-item">汉</span>
<rp>(</rp>
<rt class="py-pinyin-item">hàn</rt>
<rp>)</rp>
</ruby>
</span>
<span class="py-result-item">
<ruby>
<span class="py-chinese-item">语</span>
<rp>(</rp>
<rt class="py-pinyin-item">yǔ</rt>
<rp>)</rp>
</ruby>
</span>

### 🏆 竞品对比

以下是 `pinyin-pro``pinyin``@napi-rs/pinyin` 包对于汉字转换的速度及准确率对比,可以看到 `pinyin-pro` 在各方面都全面领先。

- 准确率测试数据: [accuracy](https://github.com/zh-lx/pinyin-pro/blob/main/benchmark/accuracy.js)
- 性能测试数据:[speed](https://github.com/zh-lx/pinyin-pro/blob/main/benchmark/speed.js)
<table>
<tr>
<th colspan="2">对比项</th>
<th>pinyin</th>
<th>@napi-rs/pinyin</th>
<th>pinyin-pro</th>
</tr>
<tr>
<td rowspan="2" colspan="2">准确率</td>
<td>😕 Node 版: 94.097%</td>
<td rowspan="2">😕 94.097%</td>
<td rowspan="2">🤩 99.846%</td>
</tr>
<tr>
<td>😕 Web 版: 91.170% </td>
</tr>
<tr>
<td rowspan="3">性能</td>
<td>5k字转换耗时</td>
<td>🐢 749.111ms</td>
<td>🚲 200.877ms</td>
<td>🚀 5.958ms</td>
</tr>
<tr>
<td>1w字转换耗时</td>
<td>🐢 795.904ms</td>
<td>🚲 206.5ms</td>
<td>🚀 15.260ms</td>
</tr>
<tr>
<td>100w字转换耗时</td>
<td>⛔ 内存溢出转换失败</td>
<td>🚀 638.888ms</td>
<td>🚀 820.131ms</td>
</tr>
<tr>
<td rowspan="2">兼容性</td>
<td>Web 环境</td>
<td>✔️ 支持</td>
<td>❌ 不支持</td>
<td>✔️ 支持</td>
</tr>
<tr>
<td>Node 环境</td>
<td>✔️ 支持</td>
<td>✔️ 支持</td>
<td>✔️ 支持</td>
</tr>
</table>

### 📠 反馈

使用遇到问题或者需要功能支持欢迎提 issue。

技术交流欢迎加 pinyin-pro 用户群 或者微信:

<div style="display: flex;">
<img src="https://user-images.githubusercontent.com/73059627/226233976-5dbb9daa-6620-4d16-a2b0-359055dcafe1.png" width="200" >
<img src="https://user-images.githubusercontent.com/73059627/226233691-848b2a40-f1a9-414e-a80f-3fc6c6209eb1.png" width="200" >
</div>
[zh-lx](https://github.com/zh-lx) maintains [a fantastic library](https://github.com/zh-lx/pinyin-pro) for converting traditional Chinese characters to pinyin. GitBook uses this library for our users who write in hànzì, to turn their docs into URLs. We've forked pinyin-pro simply to optimize performance slightly. GitBook runs pinyin-pro inside Cloudflare Workers, where start-up time is very important. So we made this fork to make optimizations around when the languages are loaded into memory. We try to keep this up-to-date with upstream.
46 changes: 29 additions & 17 deletions lib/common/segmentit/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { PatternNumberDict } from "@/data/special";
import { Pattern2 } from "@/data/dict2";
import { Pattern3 } from "@/data/dict3";
import { Pattern4 } from "@/data/dict4";
import { Pattern5 } from "@/data/dict5";
import { PatternSurname } from "@/data/surname";
import { fillPatternNumberDict } from "@/data/special";
import { fillPattern2 } from "@/data/dict2";
import { fillPattern3 } from "@/data/dict3";
import { fillPattern4 } from "@/data/dict4";
import { fillPattern5 } from "@/data/dict5";
import { fillPatternSurname } from "@/data/surname";
import { maxProbability } from "./max-probability";
import { minTokenization } from "./min-tokenization";
import { reverseMaxMatch } from "./reverse-max-match";
@@ -228,14 +228,26 @@ export class AC {
}
}

// 常规匹配
export const PatternsNormal = [
...Pattern5,
...Pattern4,
...Pattern3,
...Pattern2,
...PatternNumberDict,
...PatternSurname,
];
export const acTree = new AC();
acTree.build(PatternsNormal);

let patternsNormal: Pattern[] | undefined;
let acTree: AC | undefined;

export function getACTree(): AC {
if (!patternsNormal) {
// 常规匹配
patternsNormal = [];
fillPattern5(patternsNormal);
fillPattern4(patternsNormal);
fillPattern3(patternsNormal);
fillPattern2(patternsNormal);
fillPatternNumberDict(patternsNormal);
fillPatternSurname(patternsNormal);
}

if (!acTree) {
acTree = new AC();
acTree.build(patternsNormal);
}

return acTree;
}
12 changes: 6 additions & 6 deletions lib/core/custom/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { acTree } from '@/common/segmentit';
import { getACTree } from '@/common/segmentit';
import { Probability, Priority } from '@/common/constant';
import { splitString, stringLength } from '@/common/utils';
import { FastDictFactory } from '../../common/utils';
import DICT1 from '@/data/dict1';
import { getDict1 } from '@/data/dict1';
let customDict: { [key: string]: string } = {};
const customMultipleDict = new FastDictFactory();
const customPolyphonicDict = new FastDictFactory();
@@ -47,7 +47,7 @@ export function customPinyin(
priority: Priority.Custom,
dict: CustomDictName,
}));
acTree.build(customPatterns);
getACTree().build(customPatterns);
// add words for multiple and polyphonic
if (options?.multiple) {
addCustomConfigToDict(config, customMultipleDict, options.multiple);
@@ -66,12 +66,12 @@ function addCustomConfigToDict(
const pinyins = config[word];
splitString(word).forEach((char, index) => {
const pinyin = pinyins.split(' ')[index] || '';
if (handleType === 'replace' || (handleType === 'add' && !dict.get(char) && !DICT1.get(char))) {
if (handleType === 'replace' || (handleType === 'add' && !dict.get(char) && !getDict1().get(char))) {
// 直接覆盖原词典
dict.set(char, pinyin);
} else {
// 补充至原词典
dict.set(char, dict.get(char) || DICT1.get(char));
dict.set(char, dict.get(char) || getDict1().get(char));
if (!dict.get(char).split(' ').includes(pinyin)) {
dict.set(char, `${dict.get(char)} ${pinyin}`.trim());
}
@@ -93,7 +93,7 @@ export function clearCustomDict(dict: CustomDictType | CustomDictType[]) {
Object.keys(customDict).forEach(function (word) {
delete customDict[word];
});
acTree.removeDict(CustomDictName);
getACTree().removeDict(CustomDictName);
}
if (dict === 'multiple' || dict.indexOf('multiple') !== -1) {
customMultipleDict.clear();
22 changes: 11 additions & 11 deletions lib/core/dict/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Priority, Probability } from "@/common/constant";
import { Pattern, acTree } from "@/common/segmentit";
import { Pattern, getACTree } from "@/common/segmentit";
import { stringLength } from "@/common/utils";
import DICT1 from "@/data/dict1";
import { getDict1 } from "@/data/dict1";

const DefaultName = Symbol("default");

@@ -62,11 +62,11 @@ export function addDict(dict: DICT | {}, options?: string | DictOptions) {
});
}
}
acTree.build(patterns);
getACTree().build(patterns);
}

export function removeDict(dictName?: string) {
acTree.removeDict(dictName || DefaultName);
getACTree().removeDict(dictName || DefaultName);
removeOriginDict(dictName || DefaultName);
}

@@ -81,24 +81,24 @@ function addToOriginDict(
}
const originDict = originDictMap.get(dict)!;
if (!originDict[char]) {
originDict[char] = DICT1.get(char) as string;
originDict[char] = getDict1().get(char) as string;
}
if (handle === "add") {
const existedPinyin = DICT1.get(char);
const existedPinyin = getDict1().get(char);
if (existedPinyin && !existedPinyin.split(' ').includes(pinyin)) {
DICT1.set(char, `${existedPinyin} ${pinyin}`);
} else if (!DICT1.get(char)) {
DICT1.set(char, pinyin);
getDict1().set(char, `${existedPinyin} ${pinyin}`);
} else if (!getDict1().get(char)) {
getDict1().set(char, pinyin);
}
} else if (handle === "replace") {
DICT1.set(char, pinyin);
getDict1().set(char, pinyin);
}
}

function removeOriginDict(dict: string | Symbol) {
const originDict = originDictMap.get(dict) || {};
for (let char in originDict) {
DICT1.set(char, originDict[char]);
getDict1().set(char, originDict[char]);
delete originDict[char];
}
}
11 changes: 6 additions & 5 deletions lib/core/pinyin/handle.ts
Original file line number Diff line number Diff line change
@@ -7,11 +7,11 @@ import {
processSepecialPinyin,
} from "@/data/special";
import Surnames from "@/data/surname";
import DICT1 from "@/data/dict1";
import { getDict1 } from "@/data/dict1";
import { getCustomMultpileDict } from "@/core/custom";
import { SingleWordResult } from "../../common/type";
import type { SurnameMode } from "../../common/type";
import { acTree, MatchPattern, TokenizationAlgorithm } from "../../common/segmentit";
import { getACTree, MatchPattern, TokenizationAlgorithm } from "../../common/segmentit";
import { Priority } from "@/common/constant";
import { splitString } from "@/common/utils";

@@ -22,7 +22,7 @@ import { splitString } from "@/common/utils";
*/
type GetSingleWordPinyin = (char: string) => string;
export const getSingleWordPinyin: GetSingleWordPinyin = (char) => {
const pinyin = DICT1.get(char);
const pinyin = getDict1().get(char);
// 若查到, 则返回第一个拼音; 若未查到, 返回原字符
return pinyin ? pinyin.split(" ")[0] : char;
};
@@ -33,7 +33,7 @@ export const getPinyin = (
surname: SurnameMode,
segmentit: TokenizationAlgorithm
): { list: SingleWordResult[], matches: MatchPattern[] } => {
const matches = acTree.search(word, surname, segmentit);
const matches = getACTree().search(word, surname, segmentit);
let matchIndex = 0;
const zhChars = splitString(word);
for (let i = 0; i < zhChars.length; ) {
@@ -110,7 +110,8 @@ const getPinyinWithoutTone: GetPinyinWithoutTone = (pinyin) => {
type GetAllPinyin = (char: string, surname?: SurnameMode) => string[];
export const getAllPinyin: GetAllPinyin = (char, surname = "off") => {
const customMultpileDict = getCustomMultpileDict();
let pinyin = DICT1.get(char) ? DICT1.get(char).split(" ") : [];
const charPinyin = getDict1().get(char);
let pinyin = charPinyin ? charPinyin.split(" ") : [];
if (customMultpileDict.get(char)) {
pinyin = customMultpileDict.get(char).split(" ");
} else if (surname !== "off") {
4 changes: 2 additions & 2 deletions lib/core/pinyin/middlewares.ts
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ import {
getPinyinWithoutTone,
getPinyinWithNum,
} from "./handle";
import DICT1 from "@/data/dict1";
import { getDict1 } from "@/data/dict1";

// 验证输入是否为字符串
export const validateType = (word: unknown) => {
@@ -205,7 +205,7 @@ export const middlewareType = (
num: Number(getNumOfTone(item.originPinyin)),
isZh: item.isZh,
polyphonic,
inZhRange: !!DICT1.get(item.origin),
inZhRange: !!getDict1().get(item.origin),
result: item.result,
};
});
6 changes: 3 additions & 3 deletions lib/core/polyphonic/index.ts
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ import {
middlewareToneType,
middlewareV,
} from '@/core/pinyin/middlewares';
import DICT1 from '@/data/dict1';
import { getDict1 } from '@/data/dict1';
import {
getNumOfTone,
getInitialAndFinal,
@@ -205,7 +205,7 @@ function polyphonic(
const getPolyphonicList = (text: string): SingleWordResult[] => {
return splitString(text).map((char) => {
const customPolyphonicDict = getCustomPolyphonicDict();
const pinyin = customPolyphonicDict.get(char) || DICT1.get(char) || char;
const pinyin = customPolyphonicDict.get(char) || getDict1().get(char) || char;
return {
origin: char,
result: pinyin,
@@ -255,7 +255,7 @@ export const handleType = (
finalTail: tail,
num: Number(getNumOfTone(item.originPinyin)),
isZh: item.isZh,
inZhRange: !!DICT1.get(item.origin),
inZhRange: !!getDict1().get(item.origin),
};
});
}
20 changes: 14 additions & 6 deletions lib/data/dict1.ts
Original file line number Diff line number Diff line change
@@ -18662,14 +18662,22 @@ const map: { [key: string]: string[] } = {
'yáo xiào': ['滧'],
};

const DICT1 = new FastDictFactory();
let DICT1: FastDictFactory | undefined = undefined;

Object.keys(map).forEach((key) => {
const chars = map[key];
for (let char of chars) {
DICT1.set(char, key);
export function getDict1(): FastDictFactory {
if (!DICT1) {
const dict = new FastDictFactory();
Object.keys(map).forEach((key) => {
const chars = map[key];
for (let char of chars) {
dict.set(char, key);
}
});
DICT1 = dict;
}
});

return DICT1;
}

export default DICT1;

20 changes: 12 additions & 8 deletions lib/data/dict2.ts
Original file line number Diff line number Diff line change
@@ -2161,11 +2161,15 @@ const DICT2: { [prop: string]: string } = {

};
export default DICT2;
export const Pattern2: Pattern[] = Object.keys(DICT2).map((key) => ({
zh: key,
pinyin: DICT2[key],
probability: Probability.DICT,
length: 2,
priority: Priority.Normal,
dict: Symbol('dict2'),
}));
export function fillPattern2(arr: Pattern[]): void {
Object.keys(DICT2).forEach((key) => {
arr.push({
zh: key,
pinyin: DICT2[key],
probability: Probability.DICT,
length: 2,
priority: Priority.Normal,
dict: Symbol('dict2'),
});
});
}
20 changes: 12 additions & 8 deletions lib/data/dict3.ts
Original file line number Diff line number Diff line change
@@ -332,11 +332,15 @@ const DICT3: { [prop: string]: string } = {
曲别针: 'qū bié zhēn',
};
export default DICT3;
export const Pattern3: Pattern[] = Object.keys(DICT3).map((key) => ({
zh: key,
pinyin: DICT3[key],
probability: Probability.DICT,
length: 3,
priority: Priority.Normal,
dict: Symbol('dict3'),
}));
export function fillPattern3(arr: Pattern[]): void {
Object.keys(DICT3).forEach((key) => {
arr.push({
zh: key,
pinyin: DICT3[key],
probability: Probability.DICT,
length: 3,
priority: Priority.Normal,
dict: Symbol('dict3'),
});
});
}
20 changes: 12 additions & 8 deletions lib/data/dict4.ts
Original file line number Diff line number Diff line change
@@ -1598,11 +1598,15 @@ const DICT4: { [prop: string]: string } = {
行行不行: 'háng háng bù xíng'
}
export default DICT4;
export const Pattern4: Pattern[] = Object.keys(DICT4).map((key) => ({
zh: key,
pinyin: DICT4[key],
probability: Probability.DICT,
length: 4,
priority: Priority.Normal,
dict: Symbol('dict4'),
}));
export function fillPattern4(arr: Pattern[]): void {
Object.keys(DICT4).forEach((key) => {
arr.push({
zh: key,
pinyin: DICT4[key],
probability: Probability.DICT,
length: 4,
priority: Priority.Normal,
dict: Symbol('dict4'),
});
});
}
21 changes: 13 additions & 8 deletions lib/data/dict5.ts
Original file line number Diff line number Diff line change
@@ -18,11 +18,16 @@ const DICT5: { [prop: string]: string } = {
竹筒倒豆子: 'zhú tǒng dào dòu zi',
};
export default DICT5;
export const Pattern5: Pattern[] = Object.keys(DICT5).map((key) => ({
zh: key,
pinyin: DICT5[key],
probability: Probability.DICT,
length: 5,
priority: Priority.Normal,
dict: Symbol('dict5'),
}));

export function fillPattern5(arr: Pattern[]): void {
Object.keys(DICT5).forEach((key) => {
arr.push({
zh: key,
pinyin: DICT5[key],
probability: Probability.DICT,
length: 5,
priority: Priority.Normal,
dict: Symbol('dict5'),
});
});
}
33 changes: 18 additions & 15 deletions lib/data/special.ts
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ import {
getNumOfTone,
getPinyinWithoutTone,
} from '@/core/pinyin/handle';
import DICT1 from './dict1';
import { getDict1 } from './dict1';
import { stringLength } from '@/common/utils';

export const InitialList = [
@@ -162,17 +162,20 @@ function genNumberDict() {

return dict;
}
const NumberDict = genNumberDict();
export const PatternNumberDict: Pattern[] = Object.keys(NumberDict).map(
(key) => ({
zh: key,
pinyin: NumberDict[key],
probability: Probability.Rule,
length: stringLength(key),
priority: Priority.Normal,
dict: Symbol('rule'),
})
);

export function fillPatternNumberDict(arr: Pattern[]): void {
const NumberDict = genNumberDict();
Object.keys(NumberDict).forEach((key) => {
arr.push({
zh: key,
pinyin: NumberDict[key],
probability: Probability.Rule,
length: stringLength(key),
priority: Priority.Normal,
dict: Symbol('rule'),
});
});
}

/**
* @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
@@ -226,18 +229,18 @@ export function processToneSandhi(cur: string, pre: string, next: string) {

// 处理「了」字的变调
export function processToneSandhiLiao(cur: string, pre: string) {
if (cur === '了' && (!pre || !DICT1.get(pre))) {
if (cur === '了' && (!pre || !getDict1().get(pre))) {
return 'liǎo';
}
}

// 处理叠字符[々]
function processReduplicationChar(cur: string, pre: string) {
if (cur === '々') {
if (!pre || !DICT1.get(pre)) {
if (!pre || !getDict1().get(pre)) {
return 'tóng';
} else {
return DICT1.get(pre).split(' ')[0];
return getDict1().get(pre).split(' ')[0];
}
}
}
21 changes: 13 additions & 8 deletions lib/data/surname.ts
Original file line number Diff line number Diff line change
@@ -498,11 +498,16 @@ const Surnames: { [key: string]: string } = {
};

export default Surnames;
export const PatternSurname: Pattern[] = Object.keys(Surnames).map((key) => ({
zh: key,
pinyin: Surnames[key],
probability: Probability.Surname + stringLength(key),
length: stringLength(key),
priority: Priority.Surname,
dict: Symbol('surname'),
}));
export function fillPatternSurname(arr: Pattern[]): void {
Object.keys(Surnames).forEach((key) => {
const len = stringLength(key)
arr.push({
zh: key,
pinyin: Surnames[key],
probability: Probability.Surname + len,
length: len,
priority: Priority.Surname,
dict: Symbol('surname'),
});
});
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "pinyin-pro",
"version": "3.25.0",
"description": "准确率和性能最优异的汉字转拼音库。获取中文拼音、韵母、声母、声调、首字母,支持拼音匹配",
"name": "@gitbook/pinyin-pro",
"version": "3.26.0-gitbook4",
"description": "GitBook's fork of pinyin-pro, a fantastic open-source package for turning Chinese characters to pinyin.",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"typings": "./types/index.d.ts",
3 changes: 1 addition & 2 deletions types/common/ac.d.ts
Original file line number Diff line number Diff line change
@@ -29,6 +29,5 @@ export declare class AC {
filterWithMaxProbability(patterns: MatchPattern[], isSurname?: boolean): MatchPattern[];
filter(patterns: MatchPattern[], isSurname?: boolean): MatchPattern[];
}
export declare const PatternsNormal: Pattern[];
export declare const acTree: AC;
export declare function getACTree(): AC;
export {};
3 changes: 1 addition & 2 deletions types/common/segmentit/index.d.ts
Original file line number Diff line number Diff line change
@@ -44,6 +44,5 @@ export declare class AC {
match(text: string, surname: SurnameMode): MatchPattern[];
search(text: string, surname: SurnameMode, algorithm?: TokenizationAlgorithm): MatchPattern[];
}
export declare const PatternsNormal: Pattern[];
export declare const acTree: AC;
export declare function getACTree(): AC;
export {};
2 changes: 1 addition & 1 deletion types/core/pinyin/handle.d.ts
Original file line number Diff line number Diff line change
@@ -74,6 +74,6 @@ declare const getPinyinWithNum: GetPinyinWithNum;
* @param {string} pinyin
* @return {string}
*/
type GetFirstLetter = (pinyin: string) => string;
type GetFirstLetter = (pinyin: string, isZh: boolean) => string;
declare const getFirstLetter: GetFirstLetter;
export { getPinyinWithoutTone, getInitialAndFinal, getMultiplePinyin, getNumOfTone, getPinyinWithNum, getFirstLetter, getFinalParts, };
2 changes: 1 addition & 1 deletion types/data/dict-number.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import type { Pattern } from "../common/ac";
export declare const PatternNumberDict: Pattern[];
export declare function fillPattern2(arr: Pattern[]): void;
3 changes: 2 additions & 1 deletion types/data/dict1.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { FastDictFactory } from "../common/utils";
declare const DICT1: FastDictFactory;
declare let DICT1: FastDictFactory | undefined;
export declare function getDict1(): FastDictFactory;
export default DICT1;
2 changes: 1 addition & 1 deletion types/data/dict2.d.ts
Original file line number Diff line number Diff line change
@@ -3,4 +3,4 @@ declare const DICT2: {
[prop: string]: string;
};
export default DICT2;
export declare const Pattern2: Pattern[];
export declare function fillPattern2(arr: Pattern[]): void;
2 changes: 1 addition & 1 deletion types/data/dict3.d.ts
Original file line number Diff line number Diff line change
@@ -3,4 +3,4 @@ declare const DICT3: {
[prop: string]: string;
};
export default DICT3;
export declare const Pattern3: Pattern[];
export declare function fillPattern3(arr: Pattern[]): void;
2 changes: 1 addition & 1 deletion types/data/dict4.d.ts
Original file line number Diff line number Diff line change
@@ -3,4 +3,4 @@ declare const DICT4: {
[prop: string]: string;
};
export default DICT4;
export declare const Pattern4: Pattern[];
export declare function fillPattern4(arr: Pattern[]): void;
2 changes: 1 addition & 1 deletion types/data/dict5.d.ts
Original file line number Diff line number Diff line change
@@ -3,4 +3,4 @@ declare const DICT5: {
[prop: string]: string;
};
export default DICT5;
export declare const Pattern5: Pattern[];
export declare function fillPattern5(arr: Pattern[]): void;
2 changes: 1 addition & 1 deletion types/data/special.d.ts
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@ export declare const SpecialFinalMap: {
u: string;
};
export declare const doubleFinalList: string[];
export declare const PatternNumberDict: Pattern[];
export declare function fillPatternNumberDict(arr: Pattern[]): void;
export declare const toneSandhiList: string[];
export declare function processToneSandhi(cur: string, pre: string, next: string): string | undefined;
export declare function processToneSandhiLiao(cur: string, pre: string): "liǎo" | undefined;
2 changes: 1 addition & 1 deletion types/data/surname.d.ts
Original file line number Diff line number Diff line change
@@ -3,4 +3,4 @@ declare const Surnames: {
[key: string]: string;
};
export default Surnames;
export declare const PatternSurname: Pattern[];
export declare function fillPatternSurname(arr: Pattern[]): void;