dsh-total-quality 2.0.55 → 2.0.57
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/CONTRIBUTING.md +51 -47
- package/LICENSE.md +37 -37
- package/README.en.md +32 -30
- package/README.md +90 -88
- package/agent-host.cjs +277 -277
- package/client.js +199 -92
- package/cordis.patch.yml +5 -5
- package/docs/enterprise-onboarding.md +10 -10
- package/docs/technical-reference.md +223 -223
- package/host.cjs +91 -124
- package/llm-bridge.cjs +234 -234
- package/locale/en.json +5 -5
- package/locale/zh.json +5 -5
- package/package.json +3 -2
- package/public/app.css +3 -0
- package/public/app.js +6 -2
- package/public/index.html +8 -9
- package/public/js/api.js +152 -152
- package/public/js/assistant-history.js +62 -62
- package/public/js/material-library.js +100 -100
- package/public/js/mobile-preview.js +119 -0
- package/public/js/pages-company-knowledge.js +9 -32
- package/public/js/pages-core.js +232 -232
- package/public/js/pages-growth.js +148 -148
- package/public/js/pages-qms.js +1 -1
- package/public/js/pages-qtools.js +410 -410
- package/public/js/pages-thanks.js +71 -71
- package/public/js/plugin-updates.js +53 -0
- package/public/js/quality-workflow.js +205 -205
- package/public/js/survey-dates.js +41 -41
- package/public/js/survey-knowledge.js +301 -301
- package/public/js/tool-wizards.js +621 -621
- package/public/js/ui.js +467 -467
- package/public/js/voice-capture-worklet.js +30 -30
- package/public/js/xiaozhi-voice.js +264 -264
- package/public/js/xiaozhi.js +468 -468
- package/public/platform-help.html +426 -426
- package/public/platform-site/assets/0468a68574112812.css +10703 -10703
- package/public/platform-site/assets/159e086d35adfa1c.css +2094 -2094
- package/public/platform-site/assets/169e3159cade0d9f.css +98 -98
- package/public/platform-site/assets/22b917c4a3d4ae3d.svg +14 -14
- package/public/platform-site/assets/476f9254ccf723e7.css +8265 -8265
- package/public/platform-site/assets/85a0a4fb4416ded9.css +120 -120
- package/public/platform-site/assets/a3b272383226f6d9.svg +135 -135
- package/public/platform-site/assets/fd16dc5f3cc9d97e.svg +104 -104
- package/public/platform-site/assets/qip-favicon.svg +5 -5
- package/public/platform-site/index.html +279 -279
- package/public/platform-site/offline.js +261 -261
- package/public/platform-site/qip-brand.css +566 -566
- package/public/platform-site/qip-module-guides.js +237 -237
- package/public/responsive.css +103 -101
- package/public/vendor/markdown-it-15.0.3.LICENSE +22 -22
- package/public/vendor/markdown-it-15.0.3.umd.min.js +13 -13
- package/public/workbench.css +7 -7
- package/public/xiaozhi.css +292 -292
- package/resources/qms-templates/019ffb58f24ed48d.docx +0 -0
- package/resources/qms-templates/04a69165fc15143a.docx +0 -0
- package/resources/qms-templates/09af69358c989a63.docx +0 -0
- package/resources/qms-templates/0e7ac77844e45311.docx +0 -0
- package/resources/qms-templates/12446a1b5e173c71.docx +0 -0
- package/resources/qms-templates/15ae40f67f9a3efc.docx +0 -0
- package/resources/qms-templates/18a69bbebf2ece7f.docx +0 -0
- package/resources/qms-templates/1978faa989134f02.docx +0 -0
- package/resources/qms-templates/1a30e121de752f7b.docx +0 -0
- package/resources/qms-templates/1b88907969ea7d4c.docx +0 -0
- package/resources/qms-templates/1e92b808722bf90a.docx +0 -0
- package/resources/qms-templates/2091cad340dc0e7f.docx +0 -0
- package/resources/qms-templates/271ae1002c896ed7.docx +0 -0
- package/resources/qms-templates/280ce53e5d3b5fcf.docx +0 -0
- package/resources/qms-templates/28ca8e4ede0adf6d.docx +0 -0
- package/resources/qms-templates/2ac6675f3cb1f993.docx +0 -0
- package/resources/qms-templates/2c3b799389909d8b.docx +0 -0
- package/resources/qms-templates/2c51038ba2cc3264.docx +0 -0
- package/resources/qms-templates/30c888851d16d764.docx +0 -0
- package/resources/qms-templates/3459fc4daaa2743d.docx +0 -0
- package/resources/qms-templates/3751dafc335e0e80.docx +0 -0
- package/resources/qms-templates/38798cd4298cd2db.docx +0 -0
- package/resources/qms-templates/38f551f9d738ab0a.docx +0 -0
- package/resources/qms-templates/3c85a23ea17018b4.docx +0 -0
- package/resources/qms-templates/3cdb7d14caf1b852.docx +0 -0
- package/resources/qms-templates/3f23239bb6b3e1db.docx +0 -0
- package/resources/qms-templates/42a5b9efeeaa8007.docx +0 -0
- package/resources/qms-templates/47352aafe9b8dac1.docx +0 -0
- package/resources/qms-templates/495359b1e3cbb8fe.docx +0 -0
- package/resources/qms-templates/4ca6920464785166.docx +0 -0
- package/resources/qms-templates/4ce862582a7791be.docx +0 -0
- package/resources/qms-templates/54c9a02a10991b9e.docx +0 -0
- package/resources/qms-templates/5aeb8fddf5f3693f.docx +0 -0
- package/resources/qms-templates/5d7e5576b0e73be7.docx +0 -0
- package/resources/qms-templates/5db3a40edac37791.docx +0 -0
- package/resources/qms-templates/5ffa21eb78eb6f77.docx +0 -0
- package/resources/qms-templates/60d0fbcb709b7161.docx +0 -0
- package/resources/qms-templates/63e1d3a166756095.docx +0 -0
- package/resources/qms-templates/65c724c080d32f10.docx +0 -0
- package/resources/qms-templates/674bd7826c6cbac2.docx +0 -0
- package/resources/qms-templates/677e851acca1b879.docx +0 -0
- package/resources/qms-templates/6bf15feb1a0d2a8d.docx +0 -0
- package/resources/qms-templates/6c3889c7d0918db9.docx +0 -0
- package/resources/qms-templates/6c4c349eeb0a96a8.docx +0 -0
- package/resources/qms-templates/6c5befbd33f759c1.docx +0 -0
- package/resources/qms-templates/6df8367901347508.docx +0 -0
- package/resources/qms-templates/71db51997037692b.docx +0 -0
- package/resources/qms-templates/78a8ef8c76510c99.docx +0 -0
- package/resources/qms-templates/8397bfd1c0a1b013.docx +0 -0
- package/resources/qms-templates/84d750cf858b60a1.docx +0 -0
- package/resources/qms-templates/880de58a4b334533.docx +0 -0
- package/resources/qms-templates/88c37d1b87d9b78c.docx +0 -0
- package/resources/qms-templates/88e9cd42f1849462.docx +0 -0
- package/resources/qms-templates/8956597403be35b6.docx +0 -0
- package/resources/qms-templates/94675c810a233e1f.docx +0 -0
- package/resources/qms-templates/94dff59dd7ebec6e.docx +0 -0
- package/resources/qms-templates/9670b9cd02141594.docx +0 -0
- package/resources/qms-templates/98c373ebd7b5163d.docx +0 -0
- package/resources/qms-templates/a4078cf2378a9388.docx +0 -0
- package/resources/qms-templates/a5b110c7ef917139.docx +0 -0
- package/resources/qms-templates/aec75820e45774be.docx +0 -0
- package/resources/qms-templates/b31c085e281d230c.docx +0 -0
- package/resources/qms-templates/b7c1c3c72f8ffeef.docx +0 -0
- package/resources/qms-templates/b81b82eaa5c8f422.docx +0 -0
- package/resources/qms-templates/b92a2fa31f75f137.docx +0 -0
- package/resources/qms-templates/b96c54ae9cfffc54.docx +0 -0
- package/resources/qms-templates/bc47cfd626c010e7.docx +0 -0
- package/resources/qms-templates/be60792734afca69.docx +0 -0
- package/resources/qms-templates/c0018d8ae9ad19e1.docx +0 -0
- package/resources/qms-templates/c171027c964c8be3.docx +0 -0
- package/resources/qms-templates/c4b93d71f5d98b4b.docx +0 -0
- package/resources/qms-templates/c531da29fcb3090f.docx +0 -0
- package/resources/qms-templates/c88eddd43358d015.docx +0 -0
- package/resources/qms-templates/cb978da542f0d892.docx +0 -0
- package/resources/qms-templates/ccde4a94532a8d36.docx +0 -0
- package/resources/qms-templates/cf3fb1e20ae6f24d.docx +0 -0
- package/resources/qms-templates/d231b4982fc8113e.docx +0 -0
- package/resources/qms-templates/d3c55aa5ed14055c.docx +0 -0
- package/resources/qms-templates/db45c44a00a13349.docx +0 -0
- package/resources/qms-templates/db849a41210f54e0.docx +0 -0
- package/resources/qms-templates/dba9ce363d83ddc8.docx +0 -0
- package/resources/qms-templates/dca3ea4c95eb2225.docx +0 -0
- package/resources/qms-templates/dfa4220d2b6b8131.docx +0 -0
- package/resources/qms-templates/dfa51222c777734f.docx +0 -0
- package/resources/qms-templates/e07ea511507e14f5.docx +0 -0
- package/resources/qms-templates/e0a2e00c03458dfa.docx +0 -0
- package/resources/qms-templates/e0e802f46b6764d1.docx +0 -0
- package/resources/qms-templates/e53fc85717f2852b.docx +0 -0
- package/resources/qms-templates/e9dd82d5e54520e5.docx +0 -0
- package/resources/qms-templates/ea7bfc22c06d82a9.docx +0 -0
- package/resources/qms-templates/ecf0db9f37294536.docx +0 -0
- package/resources/qms-templates/f05fcfc87e82ff39.docx +0 -0
- package/resources/qms-templates/fb9e56a17c4a4f97.docx +0 -0
- package/resources/qms-templates/manifest.json +932 -0
- package/src/data/seed-sme.js +317 -317
- package/src/data/seed.js +879 -879
- package/src/lib/backup-identity.js +57 -57
- package/src/lib/chat-answer.js +225 -225
- package/src/lib/chat-context.js +99 -99
- package/src/lib/company-library.js +28 -19
- package/src/lib/data-import.js +217 -217
- package/src/lib/data-sources.js +118 -118
- package/src/lib/demo-data.js +27 -1
- package/src/lib/employee-intake.js +84 -84
- package/src/lib/growth.js +53 -53
- package/src/lib/intake-packets.js +119 -119
- package/src/lib/issue-intake.js +88 -88
- package/src/lib/kb-revision.js +214 -214
- package/src/lib/llm.js +279 -279
- package/src/lib/model-error.js +14 -14
- package/src/lib/privacy.js +26 -26
- package/src/lib/qms-drafting.js +194 -194
- package/src/lib/qms-jobs.js +11 -7
- package/src/lib/qms-manual-review.js +185 -185
- package/src/lib/qms-procedure-review.js +109 -109
- package/src/lib/qms-templates.js +358 -326
- package/src/lib/qms.js +369 -369
- package/src/lib/quality-priority.js +100 -100
- package/src/lib/quality-state.js +92 -92
- package/src/lib/quality-workflow.js +65 -65
- package/src/lib/retrieval.js +159 -159
- package/src/lib/spc.js +127 -127
- package/src/lib/voice-hotwords.txt +22 -22
- package/src/lib/voice-text.js +13 -13
- package/src/lib/voice-worker.js +72 -72
- package/src/lib/voice.js +330 -330
- package/src/lib/workbench-notices.js +49 -49
- package/src/lib/workbench.js +103 -106
- package/src/package.json +3 -3
- package/src/tools/analysis.js +306 -306
- package/src/tools/data.js +596 -596
- package/src/tools/docs.js +430 -430
- package/src/tools/registry.js +57 -57
- package/tools-dsh.cjs +153 -153
- package//345/256/211/350/243/205/350/257/264/346/230/216.md +33 -25
- package//346/234/254/346/234/272/347/213/254/347/253/213/345/256/211/350/243/205/350/257/264/346/230/216.md +17 -17
- package//350/207/252/346/243/200.cjs +88 -88
- package/public/js/remote-knowledge.js +0 -165
- package/public/js/workbench-notices.js +0 -67
- package/public/remote-knowledge-help.html +0 -35
- package/public/remote-knowledge.css +0 -40
- package/src/lib/remote-knowledge.js +0 -284
package/src/lib/retrieval.js
CHANGED
|
@@ -1,159 +1,159 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
/**
|
|
3
|
-
* 中文语义检索(零依赖 BM25 + 字符 bigram)
|
|
4
|
-
* ------------------------------------------------------------------
|
|
5
|
-
* 不需要外部向量服务即可工作:
|
|
6
|
-
* · 中文按「单字 + 双字组合」切分,英文/数字按词切分;
|
|
7
|
-
* · BM25 打分(k1=1.5, b=0.75),字段加权(标题 > 标签 > 正文);
|
|
8
|
-
* · 同时提供 bigram 余弦相似度,用于问题去重与相似案例匹配。
|
|
9
|
-
* 若企业后续接入向量库(如 bge + Milvus),只需替换 search(),上层不变。
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
const STOP = new Set(['的', '了', '和', '与', '及', '或', '在', '是', '有', '为', '对', '上', '下', '中', '个', '如何', '怎么', '什么', '请问', '帮我', '一下', '以及', '并且', '这个', '那个', '我们', '他们']);
|
|
13
|
-
|
|
14
|
-
/** 分词:中文单字 + bigram,英文数字整词,保留单位符号 */
|
|
15
|
-
function tokenize(text) {
|
|
16
|
-
const s = String(text || '').toLowerCase();
|
|
17
|
-
const tokens = [];
|
|
18
|
-
const re = /[a-z0-9]+(?:[.\-_/][a-z0-9]+)*|[\u4e00-\u9fa5]+|[a-z]+/g;
|
|
19
|
-
let m;
|
|
20
|
-
while ((m = re.exec(s))) {
|
|
21
|
-
const t = m[0];
|
|
22
|
-
if (/^[\u4e00-\u9fa5]+$/.test(t)) {
|
|
23
|
-
for (let i = 0; i < t.length; i++) {
|
|
24
|
-
const ch = t[i];
|
|
25
|
-
if (!STOP.has(ch)) tokens.push(ch);
|
|
26
|
-
if (i < t.length - 1) {
|
|
27
|
-
const bg = t.slice(i, i + 2);
|
|
28
|
-
if (!STOP.has(bg)) tokens.push(bg);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
} else {
|
|
32
|
-
if (!STOP.has(t)) tokens.push(t);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
return tokens;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function termFreq(tokens, terms) {
|
|
39
|
-
const tf = new Map();
|
|
40
|
-
for (const t of tokens) {
|
|
41
|
-
if (!terms || terms.has(t)) tf.set(t, (tf.get(t) || 0) + 1);
|
|
42
|
-
}
|
|
43
|
-
return tf;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* 构建 BM25 索引
|
|
48
|
-
* @param {Array<object>} docs
|
|
49
|
-
* @param {Array<{field:string, weight:number}>} fields
|
|
50
|
-
* @param {{query?:string}} options 查询已知时,只保留该查询需要的词频
|
|
51
|
-
*/
|
|
52
|
-
function buildIndex(docs, fields, options = {}) {
|
|
53
|
-
const terms = options.query === undefined ? null : new Set(tokenize(options.query));
|
|
54
|
-
const df = new Map();
|
|
55
|
-
const items = [];
|
|
56
|
-
let totalLen = 0;
|
|
57
|
-
for (let i = 0; i < docs.length; i++) {
|
|
58
|
-
const doc = docs[i];
|
|
59
|
-
const tf = new Map();
|
|
60
|
-
let len = 0;
|
|
61
|
-
for (const f of fields) {
|
|
62
|
-
const raw = resolveField(doc, f.field);
|
|
63
|
-
const vals = Array.isArray(raw) ? raw : [raw];
|
|
64
|
-
const toks = tokenize(vals.filter(Boolean).join(' '));
|
|
65
|
-
len += toks.length * (f.weight || 1);
|
|
66
|
-
const local = termFreq(toks, terms);
|
|
67
|
-
for (const [t, c] of local) tf.set(t, (tf.get(t) || 0) + c * (f.weight || 1));
|
|
68
|
-
}
|
|
69
|
-
const seen = new Set(tf.keys());
|
|
70
|
-
for (const t of seen) df.set(t, (df.get(t) || 0) + 1);
|
|
71
|
-
items.push({ doc, tf, len });
|
|
72
|
-
totalLen += len;
|
|
73
|
-
}
|
|
74
|
-
return { docs, items, df, avgLen: items.length ? totalLen / items.length : 1, n: items.length, fields };
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function resolveField(obj, path) {
|
|
78
|
-
return path.split('.').reduce((o, k) => (o === undefined || o === null ? undefined : o[k]), obj);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* BM25 检索
|
|
83
|
-
* @returns {Array<{doc:object, score:number, matched:string[]}>}
|
|
84
|
-
*/
|
|
85
|
-
function search(index, query, topK = 8, opts = {}) {
|
|
86
|
-
const k1 = 1.5, b = 0.75;
|
|
87
|
-
const qTokens = [...new Set(tokenize(query))];
|
|
88
|
-
if (!qTokens.length) return [];
|
|
89
|
-
const scored = [];
|
|
90
|
-
for (const item of index.items) {
|
|
91
|
-
let score = 0;
|
|
92
|
-
const matched = [];
|
|
93
|
-
for (const t of qTokens) {
|
|
94
|
-
const f = item.tf.get(t);
|
|
95
|
-
if (!f) continue;
|
|
96
|
-
const dfv = index.df.get(t) || 0.5;
|
|
97
|
-
const idf = Math.log(1 + (index.n - dfv + 0.5) / (dfv + 0.5));
|
|
98
|
-
const denom = f + k1 * (1 - b + b * (item.len / index.avgLen));
|
|
99
|
-
score += idf * (f * (k1 + 1)) / denom;
|
|
100
|
-
matched.push(t);
|
|
101
|
-
}
|
|
102
|
-
if (score <= 0) continue;
|
|
103
|
-
// 覆盖率加成:命中不同查询词越多越相关
|
|
104
|
-
score *= 1 + 0.15 * Math.min(matched.length, 6);
|
|
105
|
-
if (opts.filter && !opts.filter(item.doc)) continue;
|
|
106
|
-
scored.push({ doc: item.doc, score: Number(score.toFixed(4)), matched });
|
|
107
|
-
}
|
|
108
|
-
scored.sort((a, b2) => b2.score - a.score);
|
|
109
|
-
return scored.slice(0, topK);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* 字符 bigram 余弦相似度,用于问题去重与相似案例匹配(0~1)
|
|
114
|
-
* 做法:先归一化(去空格/标点/全角半角、统一小写),再在「整个字符串」上取字符 bigram。
|
|
115
|
-
* 这样"左 A 柱线束干涉"与"左A柱线束支架干涉"仍能匹配上,不受空格与字母混排影响。
|
|
116
|
-
*/
|
|
117
|
-
function similarity(a, b) {
|
|
118
|
-
const va = vec(charBigrams(a)), vb = vec(charBigrams(b));
|
|
119
|
-
let dot = 0, na = 0, nb = 0;
|
|
120
|
-
for (const v of va.values()) na += v * v;
|
|
121
|
-
for (const v of vb.values()) nb += v * v;
|
|
122
|
-
for (const [k, v] of va) { const w = vb.get(k); if (w) dot += v * w; }
|
|
123
|
-
if (!na || !nb) return 0;
|
|
124
|
-
return Number((dot / (Math.sqrt(na) * Math.sqrt(nb))).toFixed(4));
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/** 归一化 + 字符 bigram(含单字,兼容极短文本) */
|
|
128
|
-
function charBigrams(input) {
|
|
129
|
-
const s = String(input || '')
|
|
130
|
-
.toLowerCase()
|
|
131
|
-
.replace(/[\s\u3000]+/g, '')
|
|
132
|
-
.replace(/[,。、;:!?()【】《》"'`~!@#$%^&*()_+\-=\[\]{}|\\;:'",.<>/?·…—]/g, '');
|
|
133
|
-
const out = [];
|
|
134
|
-
if (s.length === 1) return [s];
|
|
135
|
-
for (let i = 0; i < s.length - 1; i++) out.push(s.slice(i, i + 2));
|
|
136
|
-
return out;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
function vec(tokens) {
|
|
140
|
-
const m = new Map();
|
|
141
|
-
for (const t of tokens) m.set(t, (m.get(t) || 0) + 1);
|
|
142
|
-
return m;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/** 关键词高亮片段(用于前端展示命中原文) */
|
|
146
|
-
function snippet(text, query, len = 120) {
|
|
147
|
-
const s = String(text || '');
|
|
148
|
-
const qs = [...new Set(tokenize(query))].filter((t) => t.length >= 1);
|
|
149
|
-
let best = -1;
|
|
150
|
-
for (const q of qs) {
|
|
151
|
-
const i = s.indexOf(q);
|
|
152
|
-
if (i >= 0 && (best < 0 || i < best)) best = i;
|
|
153
|
-
}
|
|
154
|
-
if (best < 0) return s.slice(0, len) + (s.length > len ? '…' : '');
|
|
155
|
-
const start = Math.max(0, best - Math.floor(len / 3));
|
|
156
|
-
return (start > 0 ? '…' : '') + s.slice(start, start + len) + (start + len < s.length ? '…' : '');
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
module.exports = { tokenize, buildIndex, search, similarity, charBigrams, snippet };
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* 中文语义检索(零依赖 BM25 + 字符 bigram)
|
|
4
|
+
* ------------------------------------------------------------------
|
|
5
|
+
* 不需要外部向量服务即可工作:
|
|
6
|
+
* · 中文按「单字 + 双字组合」切分,英文/数字按词切分;
|
|
7
|
+
* · BM25 打分(k1=1.5, b=0.75),字段加权(标题 > 标签 > 正文);
|
|
8
|
+
* · 同时提供 bigram 余弦相似度,用于问题去重与相似案例匹配。
|
|
9
|
+
* 若企业后续接入向量库(如 bge + Milvus),只需替换 search(),上层不变。
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const STOP = new Set(['的', '了', '和', '与', '及', '或', '在', '是', '有', '为', '对', '上', '下', '中', '个', '如何', '怎么', '什么', '请问', '帮我', '一下', '以及', '并且', '这个', '那个', '我们', '他们']);
|
|
13
|
+
|
|
14
|
+
/** 分词:中文单字 + bigram,英文数字整词,保留单位符号 */
|
|
15
|
+
function tokenize(text) {
|
|
16
|
+
const s = String(text || '').toLowerCase();
|
|
17
|
+
const tokens = [];
|
|
18
|
+
const re = /[a-z0-9]+(?:[.\-_/][a-z0-9]+)*|[\u4e00-\u9fa5]+|[a-z]+/g;
|
|
19
|
+
let m;
|
|
20
|
+
while ((m = re.exec(s))) {
|
|
21
|
+
const t = m[0];
|
|
22
|
+
if (/^[\u4e00-\u9fa5]+$/.test(t)) {
|
|
23
|
+
for (let i = 0; i < t.length; i++) {
|
|
24
|
+
const ch = t[i];
|
|
25
|
+
if (!STOP.has(ch)) tokens.push(ch);
|
|
26
|
+
if (i < t.length - 1) {
|
|
27
|
+
const bg = t.slice(i, i + 2);
|
|
28
|
+
if (!STOP.has(bg)) tokens.push(bg);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
} else {
|
|
32
|
+
if (!STOP.has(t)) tokens.push(t);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return tokens;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function termFreq(tokens, terms) {
|
|
39
|
+
const tf = new Map();
|
|
40
|
+
for (const t of tokens) {
|
|
41
|
+
if (!terms || terms.has(t)) tf.set(t, (tf.get(t) || 0) + 1);
|
|
42
|
+
}
|
|
43
|
+
return tf;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 构建 BM25 索引
|
|
48
|
+
* @param {Array<object>} docs
|
|
49
|
+
* @param {Array<{field:string, weight:number}>} fields
|
|
50
|
+
* @param {{query?:string}} options 查询已知时,只保留该查询需要的词频
|
|
51
|
+
*/
|
|
52
|
+
function buildIndex(docs, fields, options = {}) {
|
|
53
|
+
const terms = options.query === undefined ? null : new Set(tokenize(options.query));
|
|
54
|
+
const df = new Map();
|
|
55
|
+
const items = [];
|
|
56
|
+
let totalLen = 0;
|
|
57
|
+
for (let i = 0; i < docs.length; i++) {
|
|
58
|
+
const doc = docs[i];
|
|
59
|
+
const tf = new Map();
|
|
60
|
+
let len = 0;
|
|
61
|
+
for (const f of fields) {
|
|
62
|
+
const raw = resolveField(doc, f.field);
|
|
63
|
+
const vals = Array.isArray(raw) ? raw : [raw];
|
|
64
|
+
const toks = tokenize(vals.filter(Boolean).join(' '));
|
|
65
|
+
len += toks.length * (f.weight || 1);
|
|
66
|
+
const local = termFreq(toks, terms);
|
|
67
|
+
for (const [t, c] of local) tf.set(t, (tf.get(t) || 0) + c * (f.weight || 1));
|
|
68
|
+
}
|
|
69
|
+
const seen = new Set(tf.keys());
|
|
70
|
+
for (const t of seen) df.set(t, (df.get(t) || 0) + 1);
|
|
71
|
+
items.push({ doc, tf, len });
|
|
72
|
+
totalLen += len;
|
|
73
|
+
}
|
|
74
|
+
return { docs, items, df, avgLen: items.length ? totalLen / items.length : 1, n: items.length, fields };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function resolveField(obj, path) {
|
|
78
|
+
return path.split('.').reduce((o, k) => (o === undefined || o === null ? undefined : o[k]), obj);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* BM25 检索
|
|
83
|
+
* @returns {Array<{doc:object, score:number, matched:string[]}>}
|
|
84
|
+
*/
|
|
85
|
+
function search(index, query, topK = 8, opts = {}) {
|
|
86
|
+
const k1 = 1.5, b = 0.75;
|
|
87
|
+
const qTokens = [...new Set(tokenize(query))];
|
|
88
|
+
if (!qTokens.length) return [];
|
|
89
|
+
const scored = [];
|
|
90
|
+
for (const item of index.items) {
|
|
91
|
+
let score = 0;
|
|
92
|
+
const matched = [];
|
|
93
|
+
for (const t of qTokens) {
|
|
94
|
+
const f = item.tf.get(t);
|
|
95
|
+
if (!f) continue;
|
|
96
|
+
const dfv = index.df.get(t) || 0.5;
|
|
97
|
+
const idf = Math.log(1 + (index.n - dfv + 0.5) / (dfv + 0.5));
|
|
98
|
+
const denom = f + k1 * (1 - b + b * (item.len / index.avgLen));
|
|
99
|
+
score += idf * (f * (k1 + 1)) / denom;
|
|
100
|
+
matched.push(t);
|
|
101
|
+
}
|
|
102
|
+
if (score <= 0) continue;
|
|
103
|
+
// 覆盖率加成:命中不同查询词越多越相关
|
|
104
|
+
score *= 1 + 0.15 * Math.min(matched.length, 6);
|
|
105
|
+
if (opts.filter && !opts.filter(item.doc)) continue;
|
|
106
|
+
scored.push({ doc: item.doc, score: Number(score.toFixed(4)), matched });
|
|
107
|
+
}
|
|
108
|
+
scored.sort((a, b2) => b2.score - a.score);
|
|
109
|
+
return scored.slice(0, topK);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* 字符 bigram 余弦相似度,用于问题去重与相似案例匹配(0~1)
|
|
114
|
+
* 做法:先归一化(去空格/标点/全角半角、统一小写),再在「整个字符串」上取字符 bigram。
|
|
115
|
+
* 这样"左 A 柱线束干涉"与"左A柱线束支架干涉"仍能匹配上,不受空格与字母混排影响。
|
|
116
|
+
*/
|
|
117
|
+
function similarity(a, b) {
|
|
118
|
+
const va = vec(charBigrams(a)), vb = vec(charBigrams(b));
|
|
119
|
+
let dot = 0, na = 0, nb = 0;
|
|
120
|
+
for (const v of va.values()) na += v * v;
|
|
121
|
+
for (const v of vb.values()) nb += v * v;
|
|
122
|
+
for (const [k, v] of va) { const w = vb.get(k); if (w) dot += v * w; }
|
|
123
|
+
if (!na || !nb) return 0;
|
|
124
|
+
return Number((dot / (Math.sqrt(na) * Math.sqrt(nb))).toFixed(4));
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** 归一化 + 字符 bigram(含单字,兼容极短文本) */
|
|
128
|
+
function charBigrams(input) {
|
|
129
|
+
const s = String(input || '')
|
|
130
|
+
.toLowerCase()
|
|
131
|
+
.replace(/[\s\u3000]+/g, '')
|
|
132
|
+
.replace(/[,。、;:!?()【】《》"'`~!@#$%^&*()_+\-=\[\]{}|\\;:'",.<>/?·…—]/g, '');
|
|
133
|
+
const out = [];
|
|
134
|
+
if (s.length === 1) return [s];
|
|
135
|
+
for (let i = 0; i < s.length - 1; i++) out.push(s.slice(i, i + 2));
|
|
136
|
+
return out;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function vec(tokens) {
|
|
140
|
+
const m = new Map();
|
|
141
|
+
for (const t of tokens) m.set(t, (m.get(t) || 0) + 1);
|
|
142
|
+
return m;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** 关键词高亮片段(用于前端展示命中原文) */
|
|
146
|
+
function snippet(text, query, len = 120) {
|
|
147
|
+
const s = String(text || '');
|
|
148
|
+
const qs = [...new Set(tokenize(query))].filter((t) => t.length >= 1);
|
|
149
|
+
let best = -1;
|
|
150
|
+
for (const q of qs) {
|
|
151
|
+
const i = s.indexOf(q);
|
|
152
|
+
if (i >= 0 && (best < 0 || i < best)) best = i;
|
|
153
|
+
}
|
|
154
|
+
if (best < 0) return s.slice(0, len) + (s.length > len ? '…' : '');
|
|
155
|
+
const start = Math.max(0, best - Math.floor(len / 3));
|
|
156
|
+
return (start > 0 ? '…' : '') + s.slice(start, start + len) + (start + len < s.length ? '…' : '');
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
module.exports = { tokenize, buildIndex, search, similarity, charBigrams, snippet };
|
package/src/lib/spc.js
CHANGED
|
@@ -1,127 +1,127 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const stats = require('./stats');
|
|
4
|
-
const DAY = 86400000;
|
|
5
|
-
|
|
6
|
-
function windowOf(days = 7, asOf) {
|
|
7
|
-
const duration = Number(days);
|
|
8
|
-
const end = asOf === undefined ? Date.now() + 1 : Date.parse(asOf);
|
|
9
|
-
if (!Number.isFinite(duration) || duration <= 0 || duration > 36500 || !Number.isFinite(end)) {
|
|
10
|
-
throw Object.assign(new Error('统计时间范围无效'), { status: 400 });
|
|
11
|
-
}
|
|
12
|
-
return { start: end - duration * DAY, end };
|
|
13
|
-
}
|
|
14
|
-
function inWindow(value, { start, end }) {
|
|
15
|
-
const instant = Date.parse(value);
|
|
16
|
-
return instant >= start && instant < end;
|
|
17
|
-
}
|
|
18
|
-
const valid = (value) => value !== null && value !== undefined && value !== '' && Number.isFinite(Number(value));
|
|
19
|
-
const round = (n, digits = 4) => Number.isFinite(n) ? Number(n.toFixed(digits)) : null;
|
|
20
|
-
|
|
21
|
-
function samples(rows) {
|
|
22
|
-
return rows.filter((row) => valid(row.value)).map((row) => ({ ...row, value: Number(row.value) }));
|
|
23
|
-
}
|
|
24
|
-
function groupRows(rows) {
|
|
25
|
-
const grouped = new Map();
|
|
26
|
-
for (const row of rows) {
|
|
27
|
-
if (row.subgroup === null || row.subgroup === undefined || row.subgroup === '') continue;
|
|
28
|
-
const key = JSON.stringify([row.source || '', row.importBatchId || '', row.batch || '', String(row.subgroup)]);
|
|
29
|
-
if (!grouped.has(key)) grouped.set(key, []);
|
|
30
|
-
grouped.get(key).push(row);
|
|
31
|
-
}
|
|
32
|
-
return [...grouped.values()].sort((a, b) => Date.parse(a[0].ts || 0) - Date.parse(b[0].ts || 0));
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function capability(rows, char) {
|
|
36
|
-
const clean = samples(rows);
|
|
37
|
-
const values = clean.map((row) => row.value);
|
|
38
|
-
const u = valid(char.usl) ? Number(char.usl) : null;
|
|
39
|
-
const l = valid(char.lsl) ? Number(char.lsl) : null;
|
|
40
|
-
const groups = groupRows(clean).filter((group) => group.length >= 2).map((group) => group.map((row) => row.value));
|
|
41
|
-
let withinSd;
|
|
42
|
-
let basis;
|
|
43
|
-
if (groups.length >= 2 && groups.every((g) => g.length === groups[0].length)) {
|
|
44
|
-
withinSd = stats.chartXbarR(groups).sigmaWithin;
|
|
45
|
-
basis = '实际等长子组 R̄/d₂';
|
|
46
|
-
} else if (groups.length >= 2) {
|
|
47
|
-
const dof = groups.reduce((n, group) => n + group.length - 1, 0);
|
|
48
|
-
withinSd = Math.sqrt(groups.reduce((sum, group) => sum + stats.variance(group) * (group.length - 1), 0) / dof);
|
|
49
|
-
basis = '实际不等长子组合并标准差';
|
|
50
|
-
} else if (!groups.length && Number(char.subgroupSize || 1) <= 1 && values.length >= 2) {
|
|
51
|
-
withinSd = stats.chartIMR(values).sigmaWithin;
|
|
52
|
-
basis = '单值序列 MR̄/1.128';
|
|
53
|
-
} else {
|
|
54
|
-
withinSd = 0;
|
|
55
|
-
basis = '缺少足够的实际子组';
|
|
56
|
-
}
|
|
57
|
-
let reason = '';
|
|
58
|
-
if (values.length < 2) reason = '样本不足';
|
|
59
|
-
else if (u === null && l === null) reason = '未填写规格限';
|
|
60
|
-
else if (u !== null && l !== null && u <= l) reason = '上规格限必须大于下规格限';
|
|
61
|
-
const result = stats.capability(values, reason ? null : u, reason ? null : l, { withinSd, target: char.nominal });
|
|
62
|
-
Object.assign(result, { withinSd: basis === '缺少足够的实际子组' ? null : withinSd, sigmaBasis: basis });
|
|
63
|
-
if (!(withinSd > 0)) Object.assign(result, { cp: null, cpu: null, cpl: null, cpk: null, cpm: null });
|
|
64
|
-
if (reason || !(result.sd > 0)) Object.assign(result, { pp: null, ppu: null, ppl: null, ppk: null, ppmTotal: null, ppmAbove: null, ppmBelow: null, sigmaLevel: null, yieldPct: null });
|
|
65
|
-
result.note = reason || (result.sd === 0 ? '测量值恒定' : withinSd === 0 ? basis === '缺少足够的实际子组' ? basis : '组内无波动' : null);
|
|
66
|
-
return result;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function chartFor(rows, requested) {
|
|
70
|
-
const clean = samples(rows);
|
|
71
|
-
if (clean.length < 3) return { status: '样本不足', error: '至少需要 3 个有效测量点', points: [], hits: [] };
|
|
72
|
-
const groups = groupRows(clean);
|
|
73
|
-
const grouped = ['Xbar-R', 'Xbar-S'].includes(requested);
|
|
74
|
-
const uniform = groups.length >= 2 && groups.every((g) => g.length === groups[0].length && g.length >= 2) &&
|
|
75
|
-
groups.reduce((total, g) => total + g.length, 0) === clean.length;
|
|
76
|
-
const useGroups = grouped && uniform;
|
|
77
|
-
const values = clean.map((row) => row.value);
|
|
78
|
-
const chart = useGroups ? (requested === 'Xbar-S' ? stats.chartXbarS : stats.chartXbarR)(groups.map((g) => g.map((row) => row.value))) :
|
|
79
|
-
requested === 'EWMA' ? stats.chartEWMA(values) : stats.chartIMR(values);
|
|
80
|
-
const principal = chart.xbar || chart;
|
|
81
|
-
const ewma = chart.chart === 'EWMA';
|
|
82
|
-
const points = principal.points.map((point, index) => {
|
|
83
|
-
const members = useGroups ? groups[index] : [clean[index]];
|
|
84
|
-
const first = members[0], last = members.at(-1);
|
|
85
|
-
return {
|
|
86
|
-
index, pointId: useGroups ? 'group:' + String(first.id) : String(first.id), subgroup: useGroups ? first.subgroup : null,
|
|
87
|
-
start: first.ts, end: last.ts, ids: members.map((member) => member.id),
|
|
88
|
-
shift: last.shift, value: ewma ? point.z : point.value, outOfControl: point.outOfControl,
|
|
89
|
-
ucl: point.ucl, lcl: point.lcl,
|
|
90
|
-
};
|
|
91
|
-
});
|
|
92
|
-
const sigma = (principal.ucl - principal.center) / 3;
|
|
93
|
-
const limits = { center: principal.center, ucl: principal.ucl, lcl: principal.lcl, sigma };
|
|
94
|
-
const breaches = points.filter((point) => point.outOfControl).map((point) => point.index);
|
|
95
|
-
const hits = ewma || !(sigma > 0)
|
|
96
|
-
? breaches.length ? [{ rule: 1, name: '超出控制限', severity: 'high', desc: ewma ? '加权移动平均超出该点控制限' : '点位偏离零宽控制限', indexes: breaches }] : []
|
|
97
|
-
: stats.spcRules(points.map((point) => point.value), limits);
|
|
98
|
-
const dispersion = chart.r || chart.s || (chart.mr && {
|
|
99
|
-
center:chart.mrCenter, ucl:chart.mrUcl, lcl:chart.mrLcl,
|
|
100
|
-
points:chart.mr.map((value,i)=>({i:i+1,value,outOfControl:value>chart.mrUcl || value<chart.mrLcl})),
|
|
101
|
-
});
|
|
102
|
-
const secondary = dispersion && {
|
|
103
|
-
name:chart.r ? 'R 极差图' : chart.s ? 'S 标准差图' : 'MR 移动极差图',
|
|
104
|
-
center:dispersion.center, ucl:dispersion.ucl, lcl:dispersion.lcl,
|
|
105
|
-
points:dispersion.points.map(p=>({...points[p.i],value:p.value,outOfControl:p.outOfControl,
|
|
106
|
-
ids:chart.mr ? [...points[p.i-1].ids,...points[p.i].ids] : points[p.i].ids})),
|
|
107
|
-
};
|
|
108
|
-
const dispersionHits = secondary?.points.filter(p=>p.outOfControl) || [];
|
|
109
|
-
if (dispersionHits.length) hits.push({
|
|
110
|
-
rule:1,name:secondary.name+'超出控制限',severity:'high',series:'dispersion',
|
|
111
|
-
desc:secondary.name+'显示组内波动超出该图控制限。',indexes:dispersionHits.map(p=>p.index),
|
|
112
|
-
});
|
|
113
|
-
const status = hits.length ? '失控' : sigma > 0 ? '未检出异常' : '未计算';
|
|
114
|
-
return {
|
|
115
|
-
chart, limits, points, secondary, hits, status,
|
|
116
|
-
note: ewma ? 'EWMA 使用每点的动态控制限,只判超限。' : grouped && !useGroups ?
|
|
117
|
-
'窗口内子组不完整或大小不一致,使用单值移动极差图。' : sigma <= 0 ? '波动为零,无法按八大准则评估稳定性。' : '',
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
function syntheticRows(values, groups) {
|
|
122
|
-
if (Array.isArray(groups) && groups.length) return groups.flatMap((group, index) =>
|
|
123
|
-
Array.isArray(group) ? group.map((value, i) => ({ id: index + ':' + i, subgroup: index, value })) : []);
|
|
124
|
-
return (values || []).map((value, i) => ({ id: String(i), value }));
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
module.exports = { windowOf, inWindow, valid, round, samples, groupRows, capability, chartFor, syntheticRows };
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const stats = require('./stats');
|
|
4
|
+
const DAY = 86400000;
|
|
5
|
+
|
|
6
|
+
function windowOf(days = 7, asOf) {
|
|
7
|
+
const duration = Number(days);
|
|
8
|
+
const end = asOf === undefined ? Date.now() + 1 : Date.parse(asOf);
|
|
9
|
+
if (!Number.isFinite(duration) || duration <= 0 || duration > 36500 || !Number.isFinite(end)) {
|
|
10
|
+
throw Object.assign(new Error('统计时间范围无效'), { status: 400 });
|
|
11
|
+
}
|
|
12
|
+
return { start: end - duration * DAY, end };
|
|
13
|
+
}
|
|
14
|
+
function inWindow(value, { start, end }) {
|
|
15
|
+
const instant = Date.parse(value);
|
|
16
|
+
return instant >= start && instant < end;
|
|
17
|
+
}
|
|
18
|
+
const valid = (value) => value !== null && value !== undefined && value !== '' && Number.isFinite(Number(value));
|
|
19
|
+
const round = (n, digits = 4) => Number.isFinite(n) ? Number(n.toFixed(digits)) : null;
|
|
20
|
+
|
|
21
|
+
function samples(rows) {
|
|
22
|
+
return rows.filter((row) => valid(row.value)).map((row) => ({ ...row, value: Number(row.value) }));
|
|
23
|
+
}
|
|
24
|
+
function groupRows(rows) {
|
|
25
|
+
const grouped = new Map();
|
|
26
|
+
for (const row of rows) {
|
|
27
|
+
if (row.subgroup === null || row.subgroup === undefined || row.subgroup === '') continue;
|
|
28
|
+
const key = JSON.stringify([row.source || '', row.importBatchId || '', row.batch || '', String(row.subgroup)]);
|
|
29
|
+
if (!grouped.has(key)) grouped.set(key, []);
|
|
30
|
+
grouped.get(key).push(row);
|
|
31
|
+
}
|
|
32
|
+
return [...grouped.values()].sort((a, b) => Date.parse(a[0].ts || 0) - Date.parse(b[0].ts || 0));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function capability(rows, char) {
|
|
36
|
+
const clean = samples(rows);
|
|
37
|
+
const values = clean.map((row) => row.value);
|
|
38
|
+
const u = valid(char.usl) ? Number(char.usl) : null;
|
|
39
|
+
const l = valid(char.lsl) ? Number(char.lsl) : null;
|
|
40
|
+
const groups = groupRows(clean).filter((group) => group.length >= 2).map((group) => group.map((row) => row.value));
|
|
41
|
+
let withinSd;
|
|
42
|
+
let basis;
|
|
43
|
+
if (groups.length >= 2 && groups.every((g) => g.length === groups[0].length)) {
|
|
44
|
+
withinSd = stats.chartXbarR(groups).sigmaWithin;
|
|
45
|
+
basis = '实际等长子组 R̄/d₂';
|
|
46
|
+
} else if (groups.length >= 2) {
|
|
47
|
+
const dof = groups.reduce((n, group) => n + group.length - 1, 0);
|
|
48
|
+
withinSd = Math.sqrt(groups.reduce((sum, group) => sum + stats.variance(group) * (group.length - 1), 0) / dof);
|
|
49
|
+
basis = '实际不等长子组合并标准差';
|
|
50
|
+
} else if (!groups.length && Number(char.subgroupSize || 1) <= 1 && values.length >= 2) {
|
|
51
|
+
withinSd = stats.chartIMR(values).sigmaWithin;
|
|
52
|
+
basis = '单值序列 MR̄/1.128';
|
|
53
|
+
} else {
|
|
54
|
+
withinSd = 0;
|
|
55
|
+
basis = '缺少足够的实际子组';
|
|
56
|
+
}
|
|
57
|
+
let reason = '';
|
|
58
|
+
if (values.length < 2) reason = '样本不足';
|
|
59
|
+
else if (u === null && l === null) reason = '未填写规格限';
|
|
60
|
+
else if (u !== null && l !== null && u <= l) reason = '上规格限必须大于下规格限';
|
|
61
|
+
const result = stats.capability(values, reason ? null : u, reason ? null : l, { withinSd, target: char.nominal });
|
|
62
|
+
Object.assign(result, { withinSd: basis === '缺少足够的实际子组' ? null : withinSd, sigmaBasis: basis });
|
|
63
|
+
if (!(withinSd > 0)) Object.assign(result, { cp: null, cpu: null, cpl: null, cpk: null, cpm: null });
|
|
64
|
+
if (reason || !(result.sd > 0)) Object.assign(result, { pp: null, ppu: null, ppl: null, ppk: null, ppmTotal: null, ppmAbove: null, ppmBelow: null, sigmaLevel: null, yieldPct: null });
|
|
65
|
+
result.note = reason || (result.sd === 0 ? '测量值恒定' : withinSd === 0 ? basis === '缺少足够的实际子组' ? basis : '组内无波动' : null);
|
|
66
|
+
return result;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function chartFor(rows, requested) {
|
|
70
|
+
const clean = samples(rows);
|
|
71
|
+
if (clean.length < 3) return { status: '样本不足', error: '至少需要 3 个有效测量点', points: [], hits: [] };
|
|
72
|
+
const groups = groupRows(clean);
|
|
73
|
+
const grouped = ['Xbar-R', 'Xbar-S'].includes(requested);
|
|
74
|
+
const uniform = groups.length >= 2 && groups.every((g) => g.length === groups[0].length && g.length >= 2) &&
|
|
75
|
+
groups.reduce((total, g) => total + g.length, 0) === clean.length;
|
|
76
|
+
const useGroups = grouped && uniform;
|
|
77
|
+
const values = clean.map((row) => row.value);
|
|
78
|
+
const chart = useGroups ? (requested === 'Xbar-S' ? stats.chartXbarS : stats.chartXbarR)(groups.map((g) => g.map((row) => row.value))) :
|
|
79
|
+
requested === 'EWMA' ? stats.chartEWMA(values) : stats.chartIMR(values);
|
|
80
|
+
const principal = chart.xbar || chart;
|
|
81
|
+
const ewma = chart.chart === 'EWMA';
|
|
82
|
+
const points = principal.points.map((point, index) => {
|
|
83
|
+
const members = useGroups ? groups[index] : [clean[index]];
|
|
84
|
+
const first = members[0], last = members.at(-1);
|
|
85
|
+
return {
|
|
86
|
+
index, pointId: useGroups ? 'group:' + String(first.id) : String(first.id), subgroup: useGroups ? first.subgroup : null,
|
|
87
|
+
start: first.ts, end: last.ts, ids: members.map((member) => member.id),
|
|
88
|
+
shift: last.shift, value: ewma ? point.z : point.value, outOfControl: point.outOfControl,
|
|
89
|
+
ucl: point.ucl, lcl: point.lcl,
|
|
90
|
+
};
|
|
91
|
+
});
|
|
92
|
+
const sigma = (principal.ucl - principal.center) / 3;
|
|
93
|
+
const limits = { center: principal.center, ucl: principal.ucl, lcl: principal.lcl, sigma };
|
|
94
|
+
const breaches = points.filter((point) => point.outOfControl).map((point) => point.index);
|
|
95
|
+
const hits = ewma || !(sigma > 0)
|
|
96
|
+
? breaches.length ? [{ rule: 1, name: '超出控制限', severity: 'high', desc: ewma ? '加权移动平均超出该点控制限' : '点位偏离零宽控制限', indexes: breaches }] : []
|
|
97
|
+
: stats.spcRules(points.map((point) => point.value), limits);
|
|
98
|
+
const dispersion = chart.r || chart.s || (chart.mr && {
|
|
99
|
+
center:chart.mrCenter, ucl:chart.mrUcl, lcl:chart.mrLcl,
|
|
100
|
+
points:chart.mr.map((value,i)=>({i:i+1,value,outOfControl:value>chart.mrUcl || value<chart.mrLcl})),
|
|
101
|
+
});
|
|
102
|
+
const secondary = dispersion && {
|
|
103
|
+
name:chart.r ? 'R 极差图' : chart.s ? 'S 标准差图' : 'MR 移动极差图',
|
|
104
|
+
center:dispersion.center, ucl:dispersion.ucl, lcl:dispersion.lcl,
|
|
105
|
+
points:dispersion.points.map(p=>({...points[p.i],value:p.value,outOfControl:p.outOfControl,
|
|
106
|
+
ids:chart.mr ? [...points[p.i-1].ids,...points[p.i].ids] : points[p.i].ids})),
|
|
107
|
+
};
|
|
108
|
+
const dispersionHits = secondary?.points.filter(p=>p.outOfControl) || [];
|
|
109
|
+
if (dispersionHits.length) hits.push({
|
|
110
|
+
rule:1,name:secondary.name+'超出控制限',severity:'high',series:'dispersion',
|
|
111
|
+
desc:secondary.name+'显示组内波动超出该图控制限。',indexes:dispersionHits.map(p=>p.index),
|
|
112
|
+
});
|
|
113
|
+
const status = hits.length ? '失控' : sigma > 0 ? '未检出异常' : '未计算';
|
|
114
|
+
return {
|
|
115
|
+
chart, limits, points, secondary, hits, status,
|
|
116
|
+
note: ewma ? 'EWMA 使用每点的动态控制限,只判超限。' : grouped && !useGroups ?
|
|
117
|
+
'窗口内子组不完整或大小不一致,使用单值移动极差图。' : sigma <= 0 ? '波动为零,无法按八大准则评估稳定性。' : '',
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function syntheticRows(values, groups) {
|
|
122
|
+
if (Array.isArray(groups) && groups.length) return groups.flatMap((group, index) =>
|
|
123
|
+
Array.isArray(group) ? group.map((value, i) => ({ id: index + ':' + i, subgroup: index, value })) : []);
|
|
124
|
+
return (values || []).map((value, i) => ({ id: String(i), value }));
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
module.exports = { windowOf, inWindow, valid, round, samples, groupRows, capability, chartFor, syntheticRows };
|
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
质 量 异 常
|
|
2
|
-
首 件 检 验
|
|
3
|
-
电 动 工 具
|
|
4
|
-
生 产 批 次
|
|
5
|
-
量 具
|
|
6
|
-
来 料 检 验
|
|
7
|
-
根 因 分 析
|
|
8
|
-
供 应 商
|
|
9
|
-
班 前 校 验
|
|
10
|
-
过 程 能 力
|
|
11
|
-
校 准 记 录
|
|
12
|
-
公 差
|
|
13
|
-
不 合 格 品
|
|
14
|
-
控 制 计 划
|
|
15
|
-
牛 米
|
|
16
|
-
检 定
|
|
17
|
-
工 装
|
|
18
|
-
上 限
|
|
19
|
-
下 限
|
|
20
|
-
停 线
|
|
21
|
-
不 良 率
|
|
22
|
-
测 量 系 统
|
|
1
|
+
质 量 异 常
|
|
2
|
+
首 件 检 验
|
|
3
|
+
电 动 工 具
|
|
4
|
+
生 产 批 次
|
|
5
|
+
量 具
|
|
6
|
+
来 料 检 验
|
|
7
|
+
根 因 分 析
|
|
8
|
+
供 应 商
|
|
9
|
+
班 前 校 验
|
|
10
|
+
过 程 能 力
|
|
11
|
+
校 准 记 录
|
|
12
|
+
公 差
|
|
13
|
+
不 合 格 品
|
|
14
|
+
控 制 计 划
|
|
15
|
+
牛 米
|
|
16
|
+
检 定
|
|
17
|
+
工 装
|
|
18
|
+
上 限
|
|
19
|
+
下 限
|
|
20
|
+
停 线
|
|
21
|
+
不 良 率
|
|
22
|
+
测 量 系 统
|
package/src/lib/voice-text.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
// Restrict homophone edits to nearby manufacturing context. Quantities, names,
|
|
4
|
-
// identifiers and standalone words are left as the recognizer heard them.
|
|
5
|
-
function formatVoiceText(text) {
|
|
6
|
-
return String(text || '').trim()
|
|
7
|
-
.replace(/(请|帮我|先|要)坐(?=根因分析)/g,'$1做')
|
|
8
|
-
.replace(/工装家具(?=[^,。!?\n]{0,12}(?:过程能力|工序|校准|精度))/g,'工装夹具')
|
|
9
|
-
.replace(/梁据(?=的?(?:校准|检定)(?:记录|报告|证书))/g,'量具')
|
|
10
|
-
.replace(/((?:扭矩|扳手)[^,。!?\n]{0,8})弯前校验/g,'$1班前校验');
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
module.exports = { formatVoiceText };
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Restrict homophone edits to nearby manufacturing context. Quantities, names,
|
|
4
|
+
// identifiers and standalone words are left as the recognizer heard them.
|
|
5
|
+
function formatVoiceText(text) {
|
|
6
|
+
return String(text || '').trim()
|
|
7
|
+
.replace(/(请|帮我|先|要)坐(?=根因分析)/g,'$1做')
|
|
8
|
+
.replace(/工装家具(?=[^,。!?\n]{0,12}(?:过程能力|工序|校准|精度))/g,'工装夹具')
|
|
9
|
+
.replace(/梁据(?=的?(?:校准|检定)(?:记录|报告|证书))/g,'量具')
|
|
10
|
+
.replace(/((?:扭矩|扳手)[^,。!?\n]{0,8})弯前校验/g,'$1班前校验');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
module.exports = { formatVoiceText };
|