dsh-knit 0.4.0
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/LICENSE +21 -0
- package/README.en.md +178 -0
- package/README.md +167 -0
- package/assets/knit-icon-24-mono-4px.svg +1 -0
- package/cordis.patch.yml +8 -0
- package/package.json +63 -0
- package/src/client/client.js +1170 -0
- package/src/host/index.js +653 -0
- package/src/host/relevance.js +236 -0
- package/test/client.test.mjs +500 -0
- package/test/entry.test.mjs +232 -0
- package/test/harness.mjs +235 -0
- package/test/host.test.mjs +249 -0
- package/test/i18n.test.mjs +344 -0
- package/test/icon.test.mjs +153 -0
- package/test/tier1.test.mjs +443 -0
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Knit · 相关性引擎(纯本地,零模型、零网络)
|
|
3
|
+
*
|
|
4
|
+
* 输入:当前对话最近几条消息的文本 + 每个文档的可搜索文本
|
|
5
|
+
* 输出:每个文档一个 0-100 的相关度,以及一个「当前话题」标签
|
|
6
|
+
*
|
|
7
|
+
* 算法(TF-IDF 的简化版,但不做全局 df 统计以保持 O(docs×keywords)):
|
|
8
|
+
* 1. 从对话里抽关键词:ASCII 词 + 中文 2/3-gram,按 出现次数×长度² 排序,
|
|
9
|
+
* 贪心去重叠,取前 N 个。越新的消息权重越高。
|
|
10
|
+
* 2. 每个文档算加权命中分:标题×4 + 摘要×2 + 正文头部×1,命中数封顶 6 次。
|
|
11
|
+
* 3. 加 10% 的时间新鲜度奖励(相关性为主,新鲜度只做微调)。
|
|
12
|
+
* 4. 以最高分为 100 归一化。
|
|
13
|
+
*
|
|
14
|
+
* 全部是确定性字符串运算,没有模型调用,没有网络请求。
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/* ── 停用词 ─────────────────────────────────────────── */
|
|
18
|
+
|
|
19
|
+
/** 英文停用词(小写)。 */
|
|
20
|
+
const EN_STOP = new Set([
|
|
21
|
+
'the', 'and', 'for', 'are', 'but', 'not', 'you', 'all', 'can', 'her', 'was', 'one', 'our',
|
|
22
|
+
'out', 'day', 'get', 'has', 'him', 'his', 'how', 'its', 'new', 'now', 'old', 'see', 'two',
|
|
23
|
+
'way', 'who', 'boy', 'did', 'use', 'that', 'this', 'with', 'have', 'from', 'they', 'will',
|
|
24
|
+
'would', 'there', 'their', 'what', 'about', 'which', 'when', 'make', 'like', 'time', 'just',
|
|
25
|
+
'know', 'take', 'into', 'your', 'some', 'them', 'than', 'then', 'only', 'come', 'over',
|
|
26
|
+
'also', 'back', 'after', 'first', 'well', 'even', 'want', 'because', 'these', 'give', 'most',
|
|
27
|
+
'been', 'were', 'does', 'here', 'more', 'very', 'should', 'could', 'need', 'using', 'used',
|
|
28
|
+
'does', 'each', 'such', 'same', 'other', 'been', 'being', 'both', 'any', 'may', 'own',
|
|
29
|
+
'com', 'www', 'http', 'https', 'html', 'json', 'node', 'true', 'false', 'null', 'undefined',
|
|
30
|
+
'var', 'let', 'const', 'function', 'return', 'import', 'export', 'default', 'async', 'await',
|
|
31
|
+
])
|
|
32
|
+
|
|
33
|
+
/** 中文单字停用词:一个 gram 的字符全在这里面就丢掉。 */
|
|
34
|
+
const CN_STOP_CHARS = new Set([
|
|
35
|
+
'的', '了', '是', '在', '我', '你', '他', '她', '它', '们', '这', '那', '有', '和', '与',
|
|
36
|
+
'就', '都', '也', '不', '没', '很', '会', '能', '要', '把', '被', '让', '给', '从', '到',
|
|
37
|
+
'对', '为', '以', '及', '或', '但', '而', '之', '其', '于', '并', '等', '着', '过', '们',
|
|
38
|
+
'个', '上', '下', '中', '里', '外', '前', '后', '时', '话', '说', '做', '用', '好', '多',
|
|
39
|
+
'少', '大', '小', '来', '去', '出', '入', '一', '二', '三', '四', '五', '六', '七', '八',
|
|
40
|
+
'九', '十', '呢', '吗', '吧', '啊', '哦', '嗯', '呀', '么', '些', '嘛', '啦',
|
|
41
|
+
])
|
|
42
|
+
|
|
43
|
+
/** 中文 2-gram 停用词(常见虚词组合,单字停用表盖不住)。 */
|
|
44
|
+
const CN_STOP_BIGRAMS = new Set([
|
|
45
|
+
'我们', '你们', '他们', '她们', '这个', '那个', '这些', '那些', '什么', '怎么', '可以',
|
|
46
|
+
'需要', '一个', '就是', '不是', '没有', '已经', '还是', '因为', '所以', '但是', '如果',
|
|
47
|
+
'这样', '那样', '现在', '时候', '一下', '一直', '而且', '或者', '以及', '不过', '然后',
|
|
48
|
+
'比较', '非常', '真的', '应该', '可能', '只是', '其实', '这里', '那里', '自己', '大家',
|
|
49
|
+
'一些', '有点', '有点', '有点', '以后', '以前', '之后', '之前', '的话', '来说', '方面',
|
|
50
|
+
])
|
|
51
|
+
|
|
52
|
+
/* ── 关键词抽取 ─────────────────────────────────────── */
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 给一个 n-gram 打分用的字符集检查。
|
|
56
|
+
* @param {string} gram - 待检查的片段
|
|
57
|
+
* @returns {boolean} 是否全是停用字符
|
|
58
|
+
*/
|
|
59
|
+
function allStopChars(gram) {
|
|
60
|
+
for (const ch of gram) {
|
|
61
|
+
if (!CN_STOP_CHARS.has(ch)) return false
|
|
62
|
+
}
|
|
63
|
+
return true
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* 从一段文本里累计 n-gram。
|
|
68
|
+
*
|
|
69
|
+
* **原始次数与加权分数分开记**:权重只影响排序,不影响「够不够格」。
|
|
70
|
+
* 否则一条消息权重 3,里面出现一次的词也会被当成出现 3 次,噪音全进来了。
|
|
71
|
+
*
|
|
72
|
+
* @param {string} text - 文本
|
|
73
|
+
* @param {number} weight - 这段话的权重倍数
|
|
74
|
+
* @param {Map<string, number>} raw - 原始出现次数
|
|
75
|
+
* @param {Map<string, number>} weighted - 加权分数
|
|
76
|
+
* @returns {void}
|
|
77
|
+
*/
|
|
78
|
+
function accumulate(text, weight, raw, weighted) {
|
|
79
|
+
if (!text) return
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* 记一次命中。
|
|
83
|
+
* @param {string} term - 词
|
|
84
|
+
* @returns {void}
|
|
85
|
+
*/
|
|
86
|
+
const bump = (term) => {
|
|
87
|
+
raw.set(term, (raw.get(term) || 0) + 1)
|
|
88
|
+
weighted.set(term, (weighted.get(term) || 0) + weight)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// ASCII 词 / 标识符(chokidar、TypeScript、ctx.sidebarRightTabs…)
|
|
92
|
+
for (const m of text.matchAll(/[A-Za-z][A-Za-z0-9_]{2,29}/g)) {
|
|
93
|
+
const token = m[0].toLowerCase()
|
|
94
|
+
if (EN_STOP.has(token)) continue
|
|
95
|
+
bump(token)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// 中文 2-gram / 3-gram
|
|
99
|
+
const cjkRuns = text.replace(/[^\u4e00-\u9fff]+/g, ' ').split(' ')
|
|
100
|
+
for (const run of cjkRuns) {
|
|
101
|
+
if (run.length < 2) continue
|
|
102
|
+
for (let n = 2; n <= 3; n += 1) {
|
|
103
|
+
for (let i = 0; i + n <= run.length; i += 1) {
|
|
104
|
+
const gram = run.slice(i, i + n)
|
|
105
|
+
if (n === 2 && CN_STOP_BIGRAMS.has(gram)) continue
|
|
106
|
+
if (allStopChars(gram)) continue
|
|
107
|
+
bump(gram)
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* 从最近几段对话文本里抽关键词。
|
|
115
|
+
*
|
|
116
|
+
* `messages` 按**由新到旧**排列;越新的权重越高(3 / 2 / 1 / 1 …)。
|
|
117
|
+
*
|
|
118
|
+
* 门槛分两档:
|
|
119
|
+
* - ASCII 词出现 1 次就要(`chokidar`、`mtime` 这种精确词很值钱)
|
|
120
|
+
* - 中文 n-gram 要「出现过 2 次」或「出现在最新那条消息里」——
|
|
121
|
+
* 后者权重为 3,是当下正在聊什么的最强信号,不该因为只出现一次被丢掉;
|
|
122
|
+
* 而只在旧消息里露过一次的中文片段基本是噪音,滤掉。
|
|
123
|
+
*
|
|
124
|
+
* @param {readonly string[]} messages - 对话文本,最新在前
|
|
125
|
+
* @param {number} limit - 最多返回多少个关键词
|
|
126
|
+
* @returns {Array<{term: string, weight: number}>} 关键词及其权重,权重降序
|
|
127
|
+
*/
|
|
128
|
+
export function extractKeywords(messages, limit = 30) {
|
|
129
|
+
const raw = new Map()
|
|
130
|
+
const weighted = new Map()
|
|
131
|
+
|
|
132
|
+
messages.forEach((text, index) => {
|
|
133
|
+
const weight = index === 0 ? 3 : index === 1 ? 2 : 1
|
|
134
|
+
accumulate(text, weight, raw, weighted)
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
const ranked = [...weighted.entries()]
|
|
138
|
+
.map(([term, weight]) => ({ term, weight, seen: raw.get(term) || 0 }))
|
|
139
|
+
.filter((entry) => {
|
|
140
|
+
if (/^[a-z]/.test(entry.term)) return entry.seen >= 1
|
|
141
|
+
return entry.seen >= 2 || entry.weight >= 3
|
|
142
|
+
})
|
|
143
|
+
.map((entry) => ({ ...entry, score: entry.weight * entry.term.length * entry.term.length }))
|
|
144
|
+
.sort((a, b) => b.score - a.score)
|
|
145
|
+
|
|
146
|
+
const picked = []
|
|
147
|
+
for (const entry of ranked) {
|
|
148
|
+
if (picked.length >= limit) break
|
|
149
|
+
// 去重叠:已经选了「相关性排序」,就不再要「相关性」和「排序」
|
|
150
|
+
if (picked.some((p) => p.term.includes(entry.term) || entry.term.includes(p.term))) continue
|
|
151
|
+
picked.push(entry)
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return picked.map((entry) => ({ term: entry.term, weight: entry.score }))
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/* ── 文档打分 ───────────────────────────────────────── */
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* 数一个词在文本里出现几次(封顶,避免长文档靠堆词刷分)。
|
|
161
|
+
* @param {string} hay - 小写后的可搜索文本
|
|
162
|
+
* @param {string} needle - 关键词(小写)
|
|
163
|
+
* @returns {number} 命中次数,最多 6
|
|
164
|
+
*/
|
|
165
|
+
function countOccurrences(hay, needle) {
|
|
166
|
+
if (!hay || !needle) return 0
|
|
167
|
+
let hits = 0
|
|
168
|
+
let from = 0
|
|
169
|
+
for (;;) {
|
|
170
|
+
const at = hay.indexOf(needle, from)
|
|
171
|
+
if (at === -1 || hits >= 6) break
|
|
172
|
+
hits += 1
|
|
173
|
+
from = at + needle.length
|
|
174
|
+
}
|
|
175
|
+
return hits
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** 新鲜度奖励的时间常数(7 天)。 */
|
|
179
|
+
const FRESHNESS_WINDOW_MS = 7 * 24 * 60 * 60 * 1000
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* 给一批文档算相关度并按相关度重排。
|
|
183
|
+
*
|
|
184
|
+
* 每个文档需要 `haystack: { title, summary, body }`(都已小写)。
|
|
185
|
+
*
|
|
186
|
+
* @param {Array<object>} docs - 文档记录(含 haystack 与 mtimeMs)
|
|
187
|
+
* @param {readonly {term: string, weight: number}[]} keywords - 关键词表
|
|
188
|
+
* @param {number} now - 当前时间(epoch ms)
|
|
189
|
+
* @returns {{docs: Array<object>, maxRaw: number}} 带 `score` 的文档(相关度降序)
|
|
190
|
+
*/
|
|
191
|
+
export function rankByRelevance(docs, keywords, now) {
|
|
192
|
+
if (keywords.length === 0) {
|
|
193
|
+
return { docs: docs.map((doc) => ({ ...doc, score: 0 })), maxRaw: 0 }
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const raws = docs.map((doc) => {
|
|
197
|
+
const hay = doc.haystack || { title: '', summary: '', body: '' }
|
|
198
|
+
let raw = 0
|
|
199
|
+
for (const { term, weight } of keywords) {
|
|
200
|
+
const hits = countOccurrences(hay.title, term) * 4
|
|
201
|
+
+ countOccurrences(hay.summary, term) * 2
|
|
202
|
+
+ countOccurrences(hay.body, term)
|
|
203
|
+
if (hits > 0) raw += weight * hits
|
|
204
|
+
}
|
|
205
|
+
// 时间新鲜度只做 10% 的微调,主排序仍然是相关性
|
|
206
|
+
const age = Math.max(0, now - (doc.mtimeMs || 0))
|
|
207
|
+
const freshness = 1 - Math.min(1, age / FRESHNESS_WINDOW_MS)
|
|
208
|
+
return raw * (1 + 0.1 * freshness)
|
|
209
|
+
})
|
|
210
|
+
|
|
211
|
+
const maxRaw = Math.max(...raws, 0)
|
|
212
|
+
|
|
213
|
+
const scored = docs.map((doc, index) => ({
|
|
214
|
+
...doc,
|
|
215
|
+
raw: raws[index],
|
|
216
|
+
score: maxRaw > 0 ? Math.round((raws[index] / maxRaw) * 100) : 0,
|
|
217
|
+
}))
|
|
218
|
+
|
|
219
|
+
scored.sort((a, b) => b.raw - a.raw || b.mtimeMs - a.mtimeMs)
|
|
220
|
+
|
|
221
|
+
return { docs: scored, maxRaw }
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* 从关键词表里挑一个给人看的「当前话题」标签。
|
|
226
|
+
* @param {readonly {term: string, weight: number}[]} keywords - 关键词表
|
|
227
|
+
* @param {number} count - 取几个
|
|
228
|
+
* @returns {string} 用「、」连接的标签,没有则空串
|
|
229
|
+
*/
|
|
230
|
+
export function topicLabel(keywords, count = 3) {
|
|
231
|
+
const terms = keywords
|
|
232
|
+
.slice(0, count)
|
|
233
|
+
.map((entry) => entry.term)
|
|
234
|
+
.filter((term) => term.length >= 2)
|
|
235
|
+
return terms.join('、')
|
|
236
|
+
}
|