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: 动态import可选依赖 #380

Merged
merged 4 commits into from
Nov 20, 2023
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
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

----

## 3.0.0-alpha.6 (2022-09-21)
## 3.0.0-alpha.7 (2023-11-20)

- feat: dynamic import optional dependencies.

## 3.0.0-alpha.6 (2023-11-12)

- feat: support [Intl.Segmenter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segmenter) for segment.
- dict: 雪茄
Expand Down
6 changes: 3 additions & 3 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
presets: [
[ '@babel/preset-env', {targets: {node: 'current'}} ],
[ '@babel/preset-typescript', {allExtensions: true}],
[ "@babel/preset-env", {targets: {node: "current"}} ],
[ "@babel/preset-typescript", {allExtensions: true}],
],
'compact': true,
"compact": true,
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pinyin",
"version": "3.0.0-alpha.6",
"version": "3.0.0-alpha.7",
"description": "汉语拼音转换工具。",
"main": "./lib/pinyin.js",
"module": "./esm/pinyin.js",
Expand Down
73 changes: 39 additions & 34 deletions src/segment.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,58 @@
import nodejieba from "nodejieba";
import { load, cut /*, tag */ } from "@node-rs/jieba";
// @ts-ignore
import { Segment, useDefault } from "segmentit";
import type { IPinyinSegment } from "./declare";

let nodeRsJiebaLoaded = false; // @node-rs/jieba 加载词典。
let segmentit: any; // segmentit 加载词典。

Check warning on line 4 in src/segment.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Unexpected any. Specify a different type

Check warning on line 4 in src/segment.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected any. Specify a different type
let hansIntlSegmenter: any; // Intl.Segmenter

Check warning on line 5 in src/segment.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Unexpected any. Specify a different type

Check warning on line 5 in src/segment.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected any. Specify a different type

/**
* TODO: 分词并带词性信息,需要调整 segment_pinyin 方法。
* 分词并标注词性。
*/
export function segment(hans: string, segment?: IPinyinSegment): string[] {
// @node-rs/jieba (Rust)
if (segment === "@node-rs/jieba") {
if (!nodeRsJiebaLoaded) {
nodeRsJiebaLoaded = true;
load();
try {
// @node-rs/jieba (Rust)
if (segment === "@node-rs/jieba") {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { load, cut /*, tag */ } = require("@node-rs/jieba");
if (!nodeRsJiebaLoaded) {
nodeRsJiebaLoaded = true;
load();
}
return cut(hans, false);
// return tag(hans);
}
return cut(hans, false);
// return tag(hans);
}

// segmentit (Node.js)
if (segment === "segmentit") {
if (!segmentit) {
segmentit = useDefault(new Segment());
// segmentit (Node.js)
if (segment === "segmentit") {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { Segment, useDefault } = require("segmentit");
if (!segmentit) {
segmentit = useDefault(new Segment());
}
return segmentit.doSegment(hans, {
simple: true,
});
}
return segmentit.doSegment(hans, {
simple: true,
});
}

// Intl.Segmenter
if (segment === "Intl.Segmenter") {
if (typeof Intl?.Segmenter === "function") {
if (!hansIntlSegmenter) {
hansIntlSegmenter = new Intl.Segmenter("zh-Hans-CN", {
granularity: "word",
});
// Intl.Segmenter
if (segment === "Intl.Segmenter") {
if (typeof Intl?.Segmenter === "function") {
if (!hansIntlSegmenter) {
hansIntlSegmenter = new Intl.Segmenter("zh-Hans-CN", {
granularity: "word",
});
}
return [...hansIntlSegmenter.segment(hans)].map((s) => s.segment);
}
return [...hansIntlSegmenter.segment(hans)].map((s) => s.segment);
}
}

// 默认使用 nodejieba (C++)
// return nodejieba.tag(hans);
// nodejieba 定义的类型返回值错误,先忽略。
// @ts-ignore
return nodejieba.cutSmall(hans, 4);
// eslint-disable-next-line @typescript-eslint/no-var-requires
const nodejieba = require("nodejieba");
// 默认使用 nodejieba (C++)
// return nodejieba.tag(hans);
// nodejieba 定义的类型返回值错误,先忽略。
return nodejieba.cutSmall(hans, 4) as unknown as string[];
} catch (ex) {
return [hans];
}
}
Loading