dsh-mobile-plugin 0.5.137 → 0.5.139
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/config/agent-presets/standard/skills/dsh-mobile-kb/references/pairing.md +2 -2
- package/config/agent-presets/standard/skills/dsh-mobile-kb/references/port-conflict.md +4 -4
- package/lib/impl.js +10 -6
- package/lib/mobile/permissions.js +10 -0
- package/lib/mobile/session-step.js +17 -11
- package/lib/mobile/voice/frames.js +198 -0
- package/lib/mobile/voice/gateway.js +746 -0
- package/lib/mobile/voice/lang.js +347 -0
- package/lib/mobile/voice/lexicon-store.js +189 -0
- package/lib/mobile/voice/lexicon.js +508 -0
- package/lib/mobile/voice/panel-api.js +88 -0
- package/lib/mobile/voice/postprocess.js +551 -0
- package/lib/mobile/voice/protocol.js +306 -0
- package/lib/mobile/voice/providers/cn/aliyun.js +401 -0
- package/lib/mobile/voice/providers/cn/tencent.js +402 -0
- package/lib/mobile/voice/providers/cn/volc.js +328 -0
- package/lib/mobile/voice/providers/cn/xfyun.js +290 -0
- package/lib/mobile/voice/providers/host.js +190 -0
- package/lib/mobile/voice/providers/intl/_common.js +346 -0
- package/lib/mobile/voice/providers/intl/assemblyai.js +353 -0
- package/lib/mobile/voice/providers/intl/azure.js +444 -0
- package/lib/mobile/voice/providers/intl/deepgram.js +366 -0
- package/lib/mobile/voice/providers/intl/openai.js +389 -0
- package/lib/mobile/voice/providers/intl/speechmatics.js +405 -0
- package/lib/mobile/voice/providers/local/ondevice.js +129 -0
- package/lib/mobile/voice/providers/registry.js +277 -0
- package/lib/mobile/voice/routes.js +278 -0
- package/lib/mobile/voice/runtime.js +84 -0
- package/lib/mobile/voice/settings.js +206 -0
- package/lib/mobile/voice/stream.js +415 -0
- package/lib/mobile/voice/voice-provider.js +268 -0
- package/lib/panel/index.html +22 -22
- package/lib/routes/control-plane.js +19 -0
- package/lib/routes/mobile-api.js +33 -1
- package/lib/sidecar-facade-worker.cjs +1 -1
- package/lib/zeronews/wiring.cjs +17 -6
- package/package.json +14 -15
|
@@ -0,0 +1,551 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 后处理流水线:provider 给的文本 → 能直接进草稿的文本。全部在本机跑,纯函数为主。
|
|
5
|
+
*
|
|
6
|
+
* 顺序固定(`docs/voice-input-plan.md` 5.4):标点补全 → 数字/单位 ITN → 口语清理 →
|
|
7
|
+
* 术语纠正 →(可选)轻量改写。每步都能关,默认全开:
|
|
8
|
+
*
|
|
9
|
+
* normalize(raw, { lang, hotwords, options }) // 开关放 options
|
|
10
|
+
* normalize(raw, { lang, hotwords, punctuation, itn, ... }) // 也认契约里平铺的写法
|
|
11
|
+
*
|
|
12
|
+
* 几个边界,写死在这里:
|
|
13
|
+
* - **标点只在 provider 没给标点时才补**:只要原文里有任何一个句读标点就认为它自带标点,
|
|
14
|
+
* 规则补出来的逗号一定不如人家的准;
|
|
15
|
+
* - **ITN 只转有把握的形态**:日期、金额、百分比、数量词,以及数字后面跟着单位的;
|
|
16
|
+
* 光秃秃的「一」「三」不动 —— 中文里「一起」「一样」比数量多;
|
|
17
|
+
* - **口语清理只清明确的赘词和口吃重复**:「看看」「谢谢」「慢慢」这类正常叠词不碰,
|
|
18
|
+
* 「那个文件」里的「那个」是实义词,只有它后面跟标点时才算语气词;
|
|
19
|
+
* - **术语纠正只改高置信**:用户改错学到的映射可信度最高;拉丁标识符只在「字母一致、
|
|
20
|
+
* 只差大小写/连字符/空格」时换成词库写法;中文近音替换要求词库词 ≥3 字、只差一个字、
|
|
21
|
+
* 且差的那对字在已知的易混表里 —— 宁可漏改,不改坏;
|
|
22
|
+
* - **轻量改写只留注入点**:这里不发任何网络请求,调用方传进来的函数跑失败就保留原文。
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
const { detectLang } = require('./lang.js');
|
|
26
|
+
|
|
27
|
+
// ---------------------------------------------------------------------------
|
|
28
|
+
// 数字与单位(ITN)
|
|
29
|
+
// ---------------------------------------------------------------------------
|
|
30
|
+
|
|
31
|
+
const CN_DIGITS = {
|
|
32
|
+
零: 0, 〇: 0, 一: 1, 二: 2, 两: 2, 三: 3, 四: 4, 五: 5, 六: 6, 七: 7, 八: 8, 九: 9,
|
|
33
|
+
};
|
|
34
|
+
const CN_SMALL_UNITS = { 十: 10, 百: 100, 千: 1000 };
|
|
35
|
+
const CN_BIG_UNITS = { 万: 1e4, 亿: 1e8 };
|
|
36
|
+
|
|
37
|
+
/** 中文数字串 → 数值;认不出返回 null(认不出就保留原文,不猜)。 */
|
|
38
|
+
function cnNumeralToNumber(raw) {
|
|
39
|
+
const chars = [...raw];
|
|
40
|
+
if (!chars.length) return null;
|
|
41
|
+
// 逐位读法:年份、号码(「二零二六」→ 2026)。只要全是数字字就按位读。
|
|
42
|
+
if (chars.every((ch) => ch in CN_DIGITS)) {
|
|
43
|
+
return Number(chars.map((ch) => CN_DIGITS[ch]).join(''));
|
|
44
|
+
}
|
|
45
|
+
let total = 0;
|
|
46
|
+
let section = 0;
|
|
47
|
+
let num = 0;
|
|
48
|
+
let seen = false;
|
|
49
|
+
for (const ch of chars) {
|
|
50
|
+
if (ch in CN_DIGITS) {
|
|
51
|
+
num = CN_DIGITS[ch];
|
|
52
|
+
seen = true;
|
|
53
|
+
} else if (ch in CN_SMALL_UNITS) {
|
|
54
|
+
section += (num || 1) * CN_SMALL_UNITS[ch];
|
|
55
|
+
num = 0;
|
|
56
|
+
seen = true;
|
|
57
|
+
} else if (ch in CN_BIG_UNITS) {
|
|
58
|
+
section = (section + num) * CN_BIG_UNITS[ch];
|
|
59
|
+
total += section;
|
|
60
|
+
section = 0;
|
|
61
|
+
num = 0;
|
|
62
|
+
seen = true;
|
|
63
|
+
} else {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return seen ? total + section + num : null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** 「三点五」→ 3.5;整数部分按中文数字读,小数部分逐位读。 */
|
|
71
|
+
function cnDecimalToNumber(integerPart, fractionPart) {
|
|
72
|
+
const whole = cnNumeralToNumber(integerPart);
|
|
73
|
+
if (whole === null) return null;
|
|
74
|
+
if (!/^[零〇一二三四五六七八九]+$/.test(fractionPart)) return null;
|
|
75
|
+
const fraction = [...fractionPart].map((ch) => CN_DIGITS[ch]).join('');
|
|
76
|
+
return Number(`${whole}.${fraction}`);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const CN_NUMERAL = '[零〇一二三四五六七八九十百千万亿两]+';
|
|
80
|
+
const CN_YEAR_DIGITS = '[零〇一二三四五六七八九]{2,4}';
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* 中文 ITN:日期、金额、百分比、数量词。
|
|
84
|
+
* 顺序有讲究:先长形态(百分之 / 小数 / 日期),再短形态(数量词),否则「三十」会被
|
|
85
|
+
* 数量词规则先吃掉一半。
|
|
86
|
+
*/
|
|
87
|
+
function applyChineseItn(text) {
|
|
88
|
+
let out = text;
|
|
89
|
+
|
|
90
|
+
// 百分比:百分之三十点五 → 30.5%
|
|
91
|
+
out = out.replace(new RegExp(`百分之(${CN_NUMERAL}(?:点[零〇一二三四五六七八九]+)?)`, 'g'), (match, body) => {
|
|
92
|
+
const [whole, fraction] = body.split('点');
|
|
93
|
+
const value = fraction === undefined ? cnNumeralToNumber(whole) : cnDecimalToNumber(whole, fraction);
|
|
94
|
+
return value === null ? match : `${value}%`;
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// 小数:三点五 → 3.5(「一点」单独出现没有小数位,不会被这条吃掉)
|
|
98
|
+
out = out.replace(new RegExp(`(${CN_NUMERAL})点([零〇一二三四五六七八九]+)`, 'g'), (match, whole, fraction) => {
|
|
99
|
+
const value = cnDecimalToNumber(whole, fraction);
|
|
100
|
+
return value === null ? match : String(value);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
// 年:二零二六年 → 2026年(逐位读)
|
|
104
|
+
out = out.replace(new RegExp(`(${CN_YEAR_DIGITS})年`, 'g'), (match, digits) => {
|
|
105
|
+
const value = Number([...digits].map((ch) => CN_DIGITS[ch]).join(''));
|
|
106
|
+
return Number.isFinite(value) ? `${value}年` : match;
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
// 月 / 日(号)
|
|
110
|
+
out = out.replace(/(十[一二]?|[一二三四五六七八九十])月/g, (match, body) => {
|
|
111
|
+
const value = cnNumeralToNumber(body);
|
|
112
|
+
return value === null ? match : `${value}月`;
|
|
113
|
+
});
|
|
114
|
+
out = out.replace(/(三十一|三十[一二三四五六七八九]?|二十[一二三四五六七八九]?|十[一二三四五六七八九]?|[一二三四五六七八九])(日|号)/g,
|
|
115
|
+
(match, body, unit) => {
|
|
116
|
+
const value = cnNumeralToNumber(body);
|
|
117
|
+
return value === null ? match : `${value}${unit}`;
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
// 金额:一百二十块 → 120块(单位照原样保留,「块」「元」都有口语场景)
|
|
121
|
+
out = out.replace(new RegExp(`(${CN_NUMERAL})(美元|美金|港币|欧元|日元|块钱|元|块)`, 'g'), (match, body, unit) => {
|
|
122
|
+
const value = cnNumeralToNumber(body);
|
|
123
|
+
return value === null ? match : `${value}${unit}`;
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
// 数量词:「三个」→ 3个;「一个月」不动(「一个」在口语里常是虚指)。
|
|
127
|
+
const measure = '个月|分钟|小时|个|次|条|篇|行|页|秒|天|周|倍|遍|台|名|位|套|份|张|只|件|年';
|
|
128
|
+
out = out.replace(new RegExp(`(${CN_NUMERAL})(${measure})`, 'g'), (match, body, unit) => {
|
|
129
|
+
if (body === '一') return match;
|
|
130
|
+
const value = cnNumeralToNumber(body);
|
|
131
|
+
return value === null ? match : `${value}${unit}`;
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
return out;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const EN_SMALL = {
|
|
138
|
+
zero: 0, one: 1, two: 2, three: 3, four: 4, five: 5, six: 6, seven: 7, eight: 8, nine: 9,
|
|
139
|
+
ten: 10, eleven: 11, twelve: 12, thirteen: 13, fourteen: 14, fifteen: 15, sixteen: 16,
|
|
140
|
+
seventeen: 17, eighteen: 18, nineteen: 19,
|
|
141
|
+
};
|
|
142
|
+
const EN_TENS = {
|
|
143
|
+
twenty: 20, thirty: 30, forty: 40, fifty: 50, sixty: 60, seventy: 70, eighty: 80, ninety: 90,
|
|
144
|
+
};
|
|
145
|
+
const EN_SCALE = { hundred: 100, thousand: 1000, million: 1000000 };
|
|
146
|
+
|
|
147
|
+
/** 英文数字词 → 数值。没有 hundred/thousand 时按「两位一组」拼(twenty twenty six → 2026)。 */
|
|
148
|
+
function enWordsToNumber(words) {
|
|
149
|
+
const list = words.filter((word) => word !== 'and');
|
|
150
|
+
if (!list.length) return null;
|
|
151
|
+
const hasScale = list.some((word) => word in EN_SCALE);
|
|
152
|
+
if (!hasScale) {
|
|
153
|
+
let digits = '';
|
|
154
|
+
let pendingTens = null;
|
|
155
|
+
for (const word of list) {
|
|
156
|
+
const value = EN_SMALL[word] !== undefined ? EN_SMALL[word] : EN_TENS[word];
|
|
157
|
+
if (value === undefined) return null;
|
|
158
|
+
if (value >= 20 && value % 10 === 0) {
|
|
159
|
+
if (pendingTens !== null) {
|
|
160
|
+
digits += String(pendingTens);
|
|
161
|
+
pendingTens = value;
|
|
162
|
+
} else {
|
|
163
|
+
pendingTens = value;
|
|
164
|
+
}
|
|
165
|
+
} else if (pendingTens !== null) {
|
|
166
|
+
const pair = pendingTens + value;
|
|
167
|
+
if (pair >= 100) return null;
|
|
168
|
+
digits += String(pair);
|
|
169
|
+
pendingTens = null;
|
|
170
|
+
} else {
|
|
171
|
+
digits += String(value);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
if (pendingTens !== null) digits += String(pendingTens);
|
|
175
|
+
return digits ? Number(digits) : null;
|
|
176
|
+
}
|
|
177
|
+
let total = 0;
|
|
178
|
+
let current = 0;
|
|
179
|
+
for (const word of list) {
|
|
180
|
+
if (word in EN_SMALL) current += EN_SMALL[word];
|
|
181
|
+
else if (word in EN_TENS) current += EN_TENS[word];
|
|
182
|
+
else if (word === 'hundred') current = (current || 1) * 100;
|
|
183
|
+
else if (word === 'thousand' || word === 'million') {
|
|
184
|
+
total += (current || 1) * EN_SCALE[word];
|
|
185
|
+
current = 0;
|
|
186
|
+
} else return null;
|
|
187
|
+
}
|
|
188
|
+
return total + current;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const EN_NUMBER_WORDS = [...Object.keys(EN_SMALL), ...Object.keys(EN_TENS), ...Object.keys(EN_SCALE), 'and'];
|
|
192
|
+
const EN_NUMBER_RUN = new RegExp(
|
|
193
|
+
`\\b((?:(?:${EN_NUMBER_WORDS.join('|')})\\b[\\s-]*)+)(?:\\s*(percent|dollars?|bucks?|euros?|pounds?|yuan|rmb|usd)\\b)?`,
|
|
194
|
+
'gi',
|
|
195
|
+
);
|
|
196
|
+
|
|
197
|
+
const EN_UNIT = {
|
|
198
|
+
percent: '%', dollar: '$', dollars: '$', buck: '$', bucks: '$', usd: '$',
|
|
199
|
+
euro: '€', euros: '€', pound: '£', pounds: '£', yuan: '元', rmb: '元',
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
/** 英文 ITN:数字词换成阿拉伯数字;带 percent / 货币单位的单个词也换。 */
|
|
203
|
+
function applyEnglishItn(text) {
|
|
204
|
+
return text.replace(EN_NUMBER_RUN, (match, run, unitRaw) => {
|
|
205
|
+
const words = (run.match(/[A-Za-z]+/g) || []).map((word) => word.toLowerCase());
|
|
206
|
+
if (!words.length || words.every((word) => word === 'and')) return match;
|
|
207
|
+
|
|
208
|
+
// 「and」只在 hundred/thousand/million 之后是同一个数的连接词(three hundred and twenty);
|
|
209
|
+
// 其余位置是「两个数并列」(twenty twenty six and three hundred),断开分别转。
|
|
210
|
+
const segments = [];
|
|
211
|
+
let current = [];
|
|
212
|
+
for (const word of words) {
|
|
213
|
+
if (word === 'and') {
|
|
214
|
+
const prev = current[current.length - 1];
|
|
215
|
+
if (prev && prev in EN_SCALE) continue;
|
|
216
|
+
segments.push(current);
|
|
217
|
+
current = [];
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
current.push(word);
|
|
221
|
+
}
|
|
222
|
+
segments.push(current);
|
|
223
|
+
|
|
224
|
+
const unit = unitRaw ? EN_UNIT[unitRaw.toLowerCase()] : '';
|
|
225
|
+
const last = segments.length - 1;
|
|
226
|
+
const values = segments.map((segment, index) => {
|
|
227
|
+
// 单个数字词只有带单位时才有把握转(「five dollars」→ $5,「one of them」不动)
|
|
228
|
+
if (!segment.length) return null;
|
|
229
|
+
if (segment.length < 2 && !(unit && index === last)) return null;
|
|
230
|
+
const value = enWordsToNumber(segment);
|
|
231
|
+
return value === null || !Number.isFinite(value) ? null : value;
|
|
232
|
+
});
|
|
233
|
+
if (values.some((value) => value === null)) return match;
|
|
234
|
+
|
|
235
|
+
// 匹配串把数字词后面的空格也吃进来了;没有单位时补回一个,别把「320 items」粘成「320items」。
|
|
236
|
+
const trailing = !unitRaw && /[\s-]$/.test(run) ? ' ' : '';
|
|
237
|
+
return values
|
|
238
|
+
.map((value, index) => {
|
|
239
|
+
if (index !== last || !unit) return String(value);
|
|
240
|
+
return unit === '%' ? `${value}%` : `${unit}${value}`;
|
|
241
|
+
})
|
|
242
|
+
.join(' and ') + trailing;
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** ITN 总入口:中文规则和英文规则互不重叠,一起跑。 */
|
|
247
|
+
function applyItn(text) {
|
|
248
|
+
return applyEnglishItn(applyChineseItn(text));
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// ---------------------------------------------------------------------------
|
|
252
|
+
// 标点补全
|
|
253
|
+
// ---------------------------------------------------------------------------
|
|
254
|
+
|
|
255
|
+
/** 有任何句读标点就认为 provider 自带标点,不再画蛇添足。 */
|
|
256
|
+
const ANY_PUNCT = /[。!?,、;:,.!?;:]/;
|
|
257
|
+
/** 口语里天然断句的连接词:插一个逗号,长句可读性立刻不一样。 */
|
|
258
|
+
const DISCOURSE = /(然后|接着|另外|不过|但是|所以|而且|因为|也就是|还有一点)/g;
|
|
259
|
+
const HAS_CJK = /[\u3040-\u30ff\uac00-\ud7af\u4e00-\u9fff]/;
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* 补标点:provider 没给标点时的兜底。
|
|
263
|
+
*
|
|
264
|
+
* 没有时间轴就不知道真正的停顿在哪,这里只做两件有把握的事:在口语连接词前断一次,
|
|
265
|
+
* 句末补一个句号(中文)或句点(英文,首字母大写)。
|
|
266
|
+
*
|
|
267
|
+
* @param {string} text
|
|
268
|
+
* @returns {string}
|
|
269
|
+
*/
|
|
270
|
+
function completePunctuation(text) {
|
|
271
|
+
const trimmed = text.trim();
|
|
272
|
+
if (!trimmed || ANY_PUNCT.test(trimmed)) return trimmed;
|
|
273
|
+
// 极短的一句话(「好的」「收到」)不插逗号,补个句号就够。
|
|
274
|
+
let out = trimmed.replace(DISCOURSE, (match, word, offset) => (offset === 0 ? match : `,${word}`));
|
|
275
|
+
const cjk = HAS_CJK.test(out);
|
|
276
|
+
if (!/[。!?.!?…]$/.test(out)) out += cjk ? '。' : '.';
|
|
277
|
+
if (!cjk) out = out.charAt(0).toUpperCase() + out.slice(1);
|
|
278
|
+
return out;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// ---------------------------------------------------------------------------
|
|
282
|
+
// 口语清理
|
|
283
|
+
// ---------------------------------------------------------------------------
|
|
284
|
+
|
|
285
|
+
/** 「不对,我是说……」这类口误重说:标记之后才是用户真正要说的。 */
|
|
286
|
+
const RESTATEMENTS = [
|
|
287
|
+
/(?:不对不对|不对|不是|不不|说错了|说错|抱歉|不好意思|我重新说|重新说|重说|改口)[,,、\s]*(?:我是想说|我意思是|我是想|我是说|我想说|应该说|也就是说|也就是|是说)/g,
|
|
288
|
+
/\b(?:no|sorry|actually|wait)[,\s]+(?:i mean|i meant|i should say)\b[,\s]*/gi,
|
|
289
|
+
];
|
|
290
|
+
|
|
291
|
+
/** 取最后一个重说标记之后的文本:用户可能改口好几次。 */
|
|
292
|
+
function cutRestatement(text) {
|
|
293
|
+
let out = text;
|
|
294
|
+
for (const pattern of RESTATEMENTS) {
|
|
295
|
+
pattern.lastIndex = 0;
|
|
296
|
+
const matches = [...out.matchAll(pattern)];
|
|
297
|
+
if (!matches.length) continue;
|
|
298
|
+
const last = matches[matches.length - 1];
|
|
299
|
+
out = out.slice(last.index + last[0].length);
|
|
300
|
+
}
|
|
301
|
+
return out.trim();
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** 相邻重复词(口吃):「我们我们」→「我们」;正常叠词(看看/谢谢/慢慢)不在表里,不动。 */
|
|
305
|
+
const STUTTER_WORDS = /(我们|你们|他们|这个|那个|然后|就是|所以|但是|因为|可以|应该|需要|项目|文件|问题|功能|模块)\1/g;
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* 口语清理:语气词、赘词、口吃重复、口误重说。
|
|
309
|
+
*
|
|
310
|
+
* 语气词只清「明确是语气词」的位置:句首、标点后、被标点夹住的那些;
|
|
311
|
+
* 「好啊」句尾的「啊」是语气不是废话,留着。金额的「额」、余额的「额」同理不碰。
|
|
312
|
+
*
|
|
313
|
+
* @param {string} text
|
|
314
|
+
* @returns {string}
|
|
315
|
+
*/
|
|
316
|
+
function cleanFillers(text) {
|
|
317
|
+
let out = cutRestatement(text);
|
|
318
|
+
|
|
319
|
+
// 嗯 / 呃 几乎只作语气词,直接清;啊 / 哦 / 唉 / 呀 只在句首或标点旁清。
|
|
320
|
+
out = out.replace(/(^|[,。!?、,.!?;;::\n\s])[啊哦噢唉呀]+[,、,.\s]*/gu, '$1');
|
|
321
|
+
out = out.replace(/[嗯呃]+[,、,.\s]*/gu, '');
|
|
322
|
+
// 「那个,」「这个,」「那么,」是赘词;「那个文件」里的「那个」保留。
|
|
323
|
+
out = out.replace(/(^|[,。!?、,.!?\s])(?:那个|这个|那么|然后)[,,、\s]+/gu, '$1');
|
|
324
|
+
// 口吃重复
|
|
325
|
+
out = out.replace(STUTTER_WORDS, '$1');
|
|
326
|
+
out = out.replace(/([我你他她它])\1+/g, '$1');
|
|
327
|
+
// 英文语气词
|
|
328
|
+
out = out.replace(/\b(?:um+|uh+|erm+|hmm+)\b[,\s]*/gi, '');
|
|
329
|
+
out = out.replace(/\byou know\b[,\s]*/gi, '');
|
|
330
|
+
|
|
331
|
+
// 清理后留下的空标点与多余空格
|
|
332
|
+
out = out.replace(/[ \t]{2,}/g, ' ');
|
|
333
|
+
out = out.replace(/\s+([,。!?、;:,.!?;:])/g, '$1');
|
|
334
|
+
out = out.replace(/([,、,;;])\1+/g, '$1');
|
|
335
|
+
return out.replace(/^[,,、。.;;::\s]+/u, '').trim();
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// ---------------------------------------------------------------------------
|
|
339
|
+
// 术语纠正
|
|
340
|
+
// ---------------------------------------------------------------------------
|
|
341
|
+
|
|
342
|
+
/** 语音识别里高频的中文近音字对(双向)。只用来给「词库里有的词」做一位纠错。 */
|
|
343
|
+
const CONFUSABLE_PAIRS = [
|
|
344
|
+
['关', '管'], ['网', '往'], ['端', '段'], ['断', '段'], ['是', '事'], ['实', '时'],
|
|
345
|
+
['试', '识'], ['查', '察'], ['版', '板'], ['账', '帐'], ['付', '副'], ['订', '定'],
|
|
346
|
+
['码', '马'], ['件', '键'], ['带', '戴'], ['做', '作'], ['那', '哪'], ['已', '以'],
|
|
347
|
+
];
|
|
348
|
+
const CONFUSABLE = new Set();
|
|
349
|
+
for (const [a, b] of CONFUSABLE_PAIRS) {
|
|
350
|
+
CONFUSABLE.add(a + b);
|
|
351
|
+
CONFUSABLE.add(b + a);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/** 拉丁标识符比对用:忽略大小写、连字符、下划线、空格。 */
|
|
355
|
+
function latinKey(value) {
|
|
356
|
+
return value.toLowerCase().replace(/[^a-z0-9]/g, '');
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* 术语纠正:只改高置信的三种情况。
|
|
361
|
+
* ① 用户改错学到的映射(「听错 → 改对」)—— 真值,最长优先替换;
|
|
362
|
+
* ② 拉丁标识符只差大小写/连字符/空格 —— 换成词库里的写法(`post process` → `postprocess`);
|
|
363
|
+
* ③ 中文词库词与原文只差一个易混字 —— 且词库词 ≥3 字,否则不动。
|
|
364
|
+
*
|
|
365
|
+
* @param {string} text
|
|
366
|
+
* @param {string[]} hotwords
|
|
367
|
+
* @param {Array<{heard:string, corrected:string}>} corrections
|
|
368
|
+
* @returns {string}
|
|
369
|
+
*/
|
|
370
|
+
function correctTerms(text, hotwords, corrections) {
|
|
371
|
+
let out = text;
|
|
372
|
+
|
|
373
|
+
// ① 用户改错映射:长句优先,避免短映射先把长映射切碎。
|
|
374
|
+
const pairs = (Array.isArray(corrections) ? corrections : [])
|
|
375
|
+
.filter((pair) => pair && typeof pair.heard === 'string' && typeof pair.corrected === 'string')
|
|
376
|
+
.filter((pair) => pair.heard && pair.corrected && pair.heard !== pair.corrected)
|
|
377
|
+
.slice()
|
|
378
|
+
.sort((a, b) => b.heard.length - a.heard.length);
|
|
379
|
+
for (const pair of pairs) {
|
|
380
|
+
if (!out.includes(pair.heard)) continue;
|
|
381
|
+
out = out.split(pair.heard).join(pair.corrected);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
// ② 拉丁标识符
|
|
385
|
+
const latinMap = new Map();
|
|
386
|
+
for (const hotword of Array.isArray(hotwords) ? hotwords : []) {
|
|
387
|
+
if (typeof hotword !== 'string' || !/^[A-Za-z][A-Za-z0-9_.\- ]*$/.test(hotword)) continue;
|
|
388
|
+
const key = latinKey(hotword);
|
|
389
|
+
if (key.length >= 3 && !latinMap.has(key)) latinMap.set(key, hotword.trim());
|
|
390
|
+
}
|
|
391
|
+
if (latinMap.size) {
|
|
392
|
+
out = out.replace(/[A-Za-z][A-Za-z0-9_.-]*(?:[ \t]+[A-Za-z][A-Za-z0-9_.-]*)?/g, (match) => {
|
|
393
|
+
const direct = latinMap.get(latinKey(match));
|
|
394
|
+
if (direct && direct !== match) return direct;
|
|
395
|
+
const parts = match.split(/[ \t]+/);
|
|
396
|
+
if (parts.length === 2) {
|
|
397
|
+
const joined = latinMap.get(latinKey(parts[0] + parts[1]));
|
|
398
|
+
if (joined) return joined;
|
|
399
|
+
}
|
|
400
|
+
return match;
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// ③ 中文近音一位纠错:只对 ≥3 字、整段都是汉字的窗口做,且差异必须在易混表里。
|
|
405
|
+
const cjkHotwords = (Array.isArray(hotwords) ? hotwords : [])
|
|
406
|
+
.filter((hotword) => typeof hotword === 'string' && /^[\u4e00-\u9fff]{3,}$/.test(hotword));
|
|
407
|
+
if (cjkHotwords.length) {
|
|
408
|
+
for (let i = 0; i < out.length;) {
|
|
409
|
+
let advanced = 0;
|
|
410
|
+
for (const hotword of cjkHotwords) {
|
|
411
|
+
const length = hotword.length;
|
|
412
|
+
if (i + length > out.length) continue;
|
|
413
|
+
const window = out.slice(i, i + length);
|
|
414
|
+
if (window === hotword) {
|
|
415
|
+
advanced = length; // 已经是正确写法,跳过这段,免得被更短的词插进来
|
|
416
|
+
break;
|
|
417
|
+
}
|
|
418
|
+
if (!/^[\u4e00-\u9fff]+$/.test(window)) continue;
|
|
419
|
+
let diffAt = -1;
|
|
420
|
+
let diffCount = 0;
|
|
421
|
+
for (let k = 0; k < length; k += 1) {
|
|
422
|
+
if (window[k] !== hotword[k]) {
|
|
423
|
+
diffCount += 1;
|
|
424
|
+
diffAt = k;
|
|
425
|
+
if (diffCount > 1) break;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
if (diffCount !== 1) continue;
|
|
429
|
+
if (!CONFUSABLE.has(window[diffAt] + hotword[diffAt])) continue;
|
|
430
|
+
out = out.slice(0, i) + hotword + out.slice(i + length);
|
|
431
|
+
advanced = length;
|
|
432
|
+
break;
|
|
433
|
+
}
|
|
434
|
+
i += advanced || 1;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
return out;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
// ---------------------------------------------------------------------------
|
|
442
|
+
// 主流水线
|
|
443
|
+
// ---------------------------------------------------------------------------
|
|
444
|
+
|
|
445
|
+
/** 收尾:多余空格、标点前空格、重复标点。 */
|
|
446
|
+
function tidy(text) {
|
|
447
|
+
return text
|
|
448
|
+
.replace(/\r\n?/g, '\n')
|
|
449
|
+
.replace(/[ \t]{2,}/g, ' ')
|
|
450
|
+
.replace(/\s+([,。!?、;:,.!?;:])/g, '$1')
|
|
451
|
+
.replace(/([,。!?、;:,.!?;:])\1+/g, '$1')
|
|
452
|
+
.trim();
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/** 调可选的轻量改写:同步回调,抛错或返回空就保留原文;promise 也当没改(管线是同步的)。 */
|
|
456
|
+
function applyRewriter(text, hook, context) {
|
|
457
|
+
if (typeof hook !== 'function') return text;
|
|
458
|
+
let result;
|
|
459
|
+
try {
|
|
460
|
+
result = hook(text, context);
|
|
461
|
+
} catch {
|
|
462
|
+
return text;
|
|
463
|
+
}
|
|
464
|
+
if (typeof result === 'string' && result.trim()) return result.trim();
|
|
465
|
+
return text;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/** 开关:`options` 里优先,其次认契约里平铺的写法;没写就是开。 */
|
|
469
|
+
function resolveFlag(options, params, ...names) {
|
|
470
|
+
for (const name of names) {
|
|
471
|
+
if (options[name] !== undefined) return options[name];
|
|
472
|
+
if (params[name] !== undefined) return params[name];
|
|
473
|
+
}
|
|
474
|
+
return undefined;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
/** 收集用户改错映射:`corrections` 数组、`userDict`(数组或映射)、词库对象都认。 */
|
|
478
|
+
function collectCorrections(params) {
|
|
479
|
+
const fromLexicon = params.hotwords && typeof params.hotwords === 'object' && !Array.isArray(params.hotwords)
|
|
480
|
+
&& Array.isArray(params.hotwords.corrections) ? params.hotwords.corrections : [];
|
|
481
|
+
const direct = Array.isArray(params.corrections) ? params.corrections : [];
|
|
482
|
+
const userDict = params.userDict;
|
|
483
|
+
const fromDict = [];
|
|
484
|
+
if (Array.isArray(userDict)) {
|
|
485
|
+
for (const entry of userDict) {
|
|
486
|
+
if (!entry || typeof entry !== 'object') continue;
|
|
487
|
+
const heard = entry.heard !== undefined ? entry.heard : entry.from;
|
|
488
|
+
const corrected = entry.corrected !== undefined ? entry.corrected : entry.to;
|
|
489
|
+
if (typeof heard === 'string' && typeof corrected === 'string') fromDict.push({ heard, corrected });
|
|
490
|
+
}
|
|
491
|
+
} else if (userDict && typeof userDict === 'object') {
|
|
492
|
+
for (const [heard, corrected] of Object.entries(userDict)) {
|
|
493
|
+
if (typeof corrected === 'string') fromDict.push({ heard, corrected });
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
return [...fromLexicon, ...direct, ...fromDict];
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* 后处理主入口。
|
|
501
|
+
*
|
|
502
|
+
* @param {string} raw provider 识别的原始文本
|
|
503
|
+
* @param {{lang?:string, hotwords?:string[]|{hotwords?:string[], corrections?:Array},
|
|
504
|
+
* corrections?:Array, userDict?:Array|object, options?:object,
|
|
505
|
+
* punctuation?:boolean, itn?:boolean, filler?:boolean, term?:boolean,
|
|
506
|
+
* llm?:Function|boolean, rewrite?:Function|boolean}} [params]
|
|
507
|
+
* @returns {{text:string, corrected:boolean}} `corrected` 表示这条文本被改动过(含标点、ITN 任一步)。
|
|
508
|
+
*/
|
|
509
|
+
function normalize(raw, params = {}) {
|
|
510
|
+
const options = params && typeof params.options === 'object' && params.options ? params.options : {};
|
|
511
|
+
const input = typeof raw === 'string' ? raw : '';
|
|
512
|
+
const base = input.replace(/\r\n?/g, '\n').trim();
|
|
513
|
+
if (!base) return { text: '', corrected: false };
|
|
514
|
+
|
|
515
|
+
const flags = {
|
|
516
|
+
punctuation: resolveFlag(options, params, 'punctuation') !== false,
|
|
517
|
+
itn: resolveFlag(options, params, 'itn') !== false,
|
|
518
|
+
filler: resolveFlag(options, params, 'filler') !== false,
|
|
519
|
+
term: resolveFlag(options, params, 'term', 'terms') !== false,
|
|
520
|
+
};
|
|
521
|
+
// 改写开关:契约叫 llm、任务口径叫 rewrite;任一处显式 false 都关掉,函数优先取 rewrite。
|
|
522
|
+
const llmFlag = resolveFlag(options, params, 'llm');
|
|
523
|
+
const rewriteFlag = resolveFlag(options, params, 'rewrite');
|
|
524
|
+
const rewriteHook = llmFlag === false || rewriteFlag === false
|
|
525
|
+
? null
|
|
526
|
+
: typeof rewriteFlag === 'function' ? rewriteFlag
|
|
527
|
+
: typeof llmFlag === 'function' ? llmFlag : null;
|
|
528
|
+
const hotwords = Array.isArray(params.hotwords)
|
|
529
|
+
? params.hotwords
|
|
530
|
+
: params.hotwords && Array.isArray(params.hotwords.hotwords) ? params.hotwords.hotwords : [];
|
|
531
|
+
const corrections = collectCorrections(params);
|
|
532
|
+
const lang = typeof params.lang === 'string' && params.lang && params.lang !== 'auto' ? params.lang : '';
|
|
533
|
+
|
|
534
|
+
let text = base;
|
|
535
|
+
if (flags.punctuation) text = completePunctuation(text);
|
|
536
|
+
if (flags.itn) text = applyItn(text);
|
|
537
|
+
if (flags.filler) text = cleanFillers(text);
|
|
538
|
+
if (flags.term) text = correctTerms(text, hotwords, corrections);
|
|
539
|
+
if (rewriteHook) {
|
|
540
|
+
text = applyRewriter(text, rewriteHook, {
|
|
541
|
+
lang: lang || detectLang(text).lang,
|
|
542
|
+
hotwords,
|
|
543
|
+
});
|
|
544
|
+
}
|
|
545
|
+
text = tidy(text);
|
|
546
|
+
return { text, corrected: text !== base };
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
module.exports = {
|
|
550
|
+
normalize,
|
|
551
|
+
};
|