|
1 | 1 | // Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
2 | 2 |
|
3 | 3 | import type { SegmenterFallbackReason } from '@objectstack/spec/contracts'; |
| 4 | +import { PLATFORM_WORDS } from './platform-words.js'; |
4 | 5 |
|
5 | 6 | /** The part of a dictionary segmenter the service calls: a cut with the HMM off. */ |
6 | 7 | export interface DictionarySegmenter { |
@@ -28,6 +29,7 @@ export type JiebaCutMode = 'cut' | 'cutForSearch'; |
28 | 29 | export const JIEBA_CUT_MODE: JiebaCutMode = 'cut'; |
29 | 30 |
|
30 | 31 | interface JiebaInstance { |
| 32 | + loadDict(dict: Uint8Array): void; |
31 | 33 | cut(sentence: string, hmm?: boolean): string[]; |
32 | 34 | cutForSearch(sentence: string, hmm?: boolean): string[]; |
33 | 35 | } |
@@ -62,24 +64,60 @@ function cjsExports<T>(mod: unknown): T { |
62 | 64 | } |
63 | 65 |
|
64 | 66 | /** |
65 | | - * Load `@node-rs/jieba` with its default dictionary, once: one read-only |
66 | | - * instance for the process, with the HMM off (it guesses words a dictionary |
67 | | - * does not hold, and invents some, such as 能付 and 黑后). |
| 67 | + * The frequency each platform word is added to jieba's dictionary with: high |
| 68 | + * enough that jieba keeps the word whole where the question holds it (一年有几天 |
| 69 | + * 年假 → 年假), low enough that a likelier split across it still wins (去年假期 → |
| 70 | + * 去年 | 假期, never 去 | 年假 | 期). Measured on both kinds of sentence: 10 |
| 71 | + * splits 年假, and every value from 100 to 10000 cuts both as above. |
| 72 | + */ |
| 73 | +export const PLATFORM_WORD_FREQUENCY = 1000; |
| 74 | + |
| 75 | +/** |
| 76 | + * The one load of jieba in this process, shared by every loader and every |
| 77 | + * service built on it: one read-only instance, with its default dictionary and |
| 78 | + * the platform words. A load that failed is not retried either. |
| 79 | + */ |
| 80 | +let processJieba: Promise<{ ok: true; jieba: JiebaInstance } | { ok: false; reason: SegmenterFallbackReason; error: string }> | undefined; |
| 81 | + |
| 82 | +async function loadProcessJieba(): Promise<{ ok: true; jieba: JiebaInstance } | { ok: false; reason: SegmenterFallbackReason; error: string }> { |
| 83 | + try { |
| 84 | + const { Jieba } = cjsExports<JiebaModule>(await import('@node-rs/jieba')); |
| 85 | + const { dict } = cjsExports<JiebaDictModule>(await import('@node-rs/jieba/dict.js')); |
| 86 | + const jieba = Jieba.withDict(dict); |
| 87 | + jieba.loadDict(new TextEncoder().encode(PLATFORM_WORDS.map((w) => `${w} ${PLATFORM_WORD_FREQUENCY}`).join('\n'))); |
| 88 | + return { ok: true, jieba }; |
| 89 | + } catch (e) { |
| 90 | + return { |
| 91 | + ok: false, |
| 92 | + reason: isNotInstalled(e) ? 'not_installed' : 'load_failed', |
| 93 | + error: e instanceof Error ? e.message : String(e), |
| 94 | + }; |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +/** The segmenter of each cut mode, built once over the process's one jieba. */ |
| 99 | +const segmenters = new Map<JiebaCutMode, Promise<SegmenterLoad>>(); |
| 100 | + |
| 101 | +/** |
| 102 | + * Load `@node-rs/jieba` with its default dictionary and the platform words |
| 103 | + * ({@link PLATFORM_WORDS}), once per process: one read-only instance, with the |
| 104 | + * HMM off (it guesses words a dictionary does not hold, and invents some, such |
| 105 | + * as 能付 and 黑后). Every call of every loader answers the same segmenter for |
| 106 | + * a cut mode, and the two cut modes share the one instance. |
68 | 107 | */ |
69 | 108 | export function jiebaLoader(mode: JiebaCutMode = JIEBA_CUT_MODE): SegmenterLoader { |
70 | | - return async () => { |
71 | | - try { |
72 | | - const { Jieba } = cjsExports<JiebaModule>(await import('@node-rs/jieba')); |
73 | | - const { dict } = cjsExports<JiebaDictModule>(await import('@node-rs/jieba/dict.js')); |
74 | | - const jieba = Jieba.withDict(dict); |
75 | | - const cut = mode === 'cut' ? (text: string) => jieba.cut(text, false) : (text: string) => jieba.cutForSearch(text, false); |
76 | | - return { ok: true, segmenter: { name: 'jieba', cut } }; |
77 | | - } catch (e) { |
78 | | - return { |
79 | | - ok: false, |
80 | | - reason: isNotInstalled(e) ? 'not_installed' : 'load_failed', |
81 | | - error: e instanceof Error ? e.message : String(e), |
82 | | - }; |
| 109 | + return () => { |
| 110 | + let load = segmenters.get(mode); |
| 111 | + if (!load) { |
| 112 | + processJieba ??= loadProcessJieba(); |
| 113 | + load = processJieba.then((loaded): SegmenterLoad => { |
| 114 | + if (!loaded.ok) return loaded; |
| 115 | + const { jieba } = loaded; |
| 116 | + const cut = mode === 'cut' ? (text: string) => jieba.cut(text, false) : (text: string) => jieba.cutForSearch(text, false); |
| 117 | + return { ok: true, segmenter: Object.freeze({ name: 'jieba', cut }) }; |
| 118 | + }); |
| 119 | + segmenters.set(mode, load); |
83 | 120 | } |
| 121 | + return load; |
84 | 122 | }; |
85 | 123 | } |
0 commit comments