Skip to content

Commit

Permalink
feat: pinyin bin support intl.segmenter
Browse files Browse the repository at this point in the history
  • Loading branch information
hotoo committed Sep 21, 2022
1 parent 63c5b9f commit 7913f79
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions bin/pinyin
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ commander.
option('-v, --version', 'output the version number').
option('-s, --style <style>', 'pinyin styles: [NORMAL,TONE,TONE2,INITIALS,FIRST_LETTER]').
option('-m, --mode <mode>', 'pinyin mode: [NORMAL,SURNAME]').
option('-S, --segment [segment]', 'segmentation word to phrases, support "nodejieba", "@node-rs/jieba", "segmentit"').
option('-S, --segment [segment]', 'segmentation word to phrases, support "Intl.Segmenter", "nodejieba", "@node-rs/jieba", "segmentit"').
option('-h, --heteronym', 'output heteronym pinyins').
option('-g, --group', 'output group by phrases').
option('-c, --compact', 'output the compact pinyin result').
Expand All @@ -20,7 +20,7 @@ if (commander.list) {
}

// --segment <segment> 是可选项,当后面带的不合法的 segment 时,当作文本处理。
const validSegmentList = ["nodejieba", "@node-rs/jieba", "segmentit", true, false, undefined];
const validSegmentList = ["Intl.Segmenter", "nodejieba", "@node-rs/jieba", "segmentit", true, false, undefined];
if (!validSegmentList.includes(commander.segment)) {
commander.args.splice(0, 0, commander.segment);
commander.segment = true;
Expand Down
2 changes: 1 addition & 1 deletion src/segment-web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function segment(hans: string, segment?: IPinyinSegment): string[] {

// Intl.Segmenter
if (segment === "Intl.Segmenter") {
if (Intl.Segmenter) {
if (typeof Intl?.Segmenter === "function") {
if (!hansIntlSegmenter) {
hansIntlSegmenter = new Intl.Segmenter("zh-Hans-CN", {
granularity: "word",
Expand Down
2 changes: 1 addition & 1 deletion src/segment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function segment(hans: string, segment?: IPinyinSegment): string[] {

// Intl.Segmenter
if (segment === "Intl.Segmenter") {
if (Intl.Segmenter) {
if (typeof Intl?.Segmenter === "function") {
if (!hansIntlSegmenter) {
hansIntlSegmenter = new Intl.Segmenter("zh-Hans-CN", {
granularity: "word",
Expand Down
2 changes: 1 addition & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export function convertUserOptions(options?: IPinyinOptions): IPinyinAllOptions
let segment: IPinyinSegment | undefined = undefined;
if (options?.segment) {
if (options?.segment === true) {
segment = "nodejieba";
segment = "Intl.Segmenter";
} else {
segment = options.segment;
}
Expand Down

0 comments on commit 7913f79

Please sign in to comment.