shukdev3 commited on
Commit
a81861d
ยท
verified ยท
1 Parent(s): 760ff2c

Create js/main.js

Browse files
Files changed (1) hide show
  1. static/js/main.js +634 -0
static/js/main.js ADDED
@@ -0,0 +1,634 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* ============================================================
2
+ Text Vectorization Lab โ€” frontend logic
3
+ Talks to the real Flask/sklearn/gensim backend in app.py and
4
+ animates the response stage by stage along each pipeline tape.
5
+ ============================================================ */
6
+
7
+ (() => {
8
+ "use strict";
9
+
10
+ const PALETTE = {
11
+ amber: "242,169,59",
12
+ teal: "52,214,184",
13
+ pink: "239,93,168",
14
+ violet: "155,140,242",
15
+ };
16
+
17
+ // ---------------------------------------------------------- utilities
18
+ const $ = (sel, root = document) => root.querySelector(sel);
19
+ const $$ = (sel, root = document) => Array.from(root.querySelectorAll(sel));
20
+
21
+ function escapeHtml(str) {
22
+ return String(str)
23
+ .replace(/&/g, "&")
24
+ .replace(/</g, "&lt;")
25
+ .replace(/>/g, "&gt;")
26
+ .replace(/"/g, "&quot;");
27
+ }
28
+
29
+ function formatNum(v) {
30
+ if (typeof v !== "number") return escapeHtml(v);
31
+ if (Number.isInteger(v)) return String(v);
32
+ return v.toFixed(3).replace(/0+$/, "").replace(/\.$/, ".0");
33
+ }
34
+
35
+ function heat(colorKey, v, max) {
36
+ if (!v || v <= 0) return "transparent";
37
+ const rgb = PALETTE[colorKey] || PALETTE.teal;
38
+ const alpha = max > 0 ? 0.16 + 0.55 * (v / max) : 0.2;
39
+ return `rgba(${rgb}, ${alpha.toFixed(2)})`;
40
+ }
41
+
42
+ function chipList(items, colorClass = "", delayStep = 0.04) {
43
+ return `<div class="chip-row">${items
44
+ .map(
45
+ (t, i) =>
46
+ `<span class="chip ${colorClass}" style="animation-delay:${(i * delayStep).toFixed(2)}s">${escapeHtml(t)}</span>`
47
+ )
48
+ .join("")}</div>`;
49
+ }
50
+
51
+ function matrixTable(rowLabels, colLabels, matrix, opts = {}) {
52
+ let max = 0;
53
+ matrix.forEach((row) => row.forEach((v) => { if (typeof v === "number" && v > max) max = v; }));
54
+ let html = `<div class="table-wrap"><table class="matrix"><thead><tr><th>${escapeHtml(opts.corner || "")}</th>`;
55
+ colLabels.forEach((c) => (html += `<th>${escapeHtml(c)}</th>`));
56
+ html += "</tr></thead><tbody>";
57
+ matrix.forEach((row, ri) => {
58
+ html += `<tr><td class="row-head">${escapeHtml(rowLabels[ri])}</td>`;
59
+ row.forEach((v, ci) => {
60
+ const idx = ri * row.length + ci;
61
+ const bg = opts.heat ? heat(opts.heat, v, max) : "transparent";
62
+ html += `<td><span class="cell" style="animation-delay:${(idx * 0.018).toFixed(2)}s"><span class="cell-fill" style="background:${bg}">${formatNum(v)}</span></span></td>`;
63
+ });
64
+ html += "</tr>";
65
+ });
66
+ html += "</tbody></table></div>";
67
+ return html;
68
+ }
69
+
70
+ function kvList(pairs, colorClass) {
71
+ return `<div class="kv-list">${pairs
72
+ .map(([k, v]) => `<div class="kv-row"><span class="k">${escapeHtml(k)}</span><span class="v" style="${colorClass ? `color: rgb(${PALETTE[colorClass]})` : ""}">${typeof v === "number" ? formatNum(v) : escapeHtml(v)}</span></div>`)
73
+ .join("")}</div>`;
74
+ }
75
+
76
+ // ---------------------------------------------------------- tape control
77
+ function resetTape(tape) {
78
+ $$(".tape-stage", tape).forEach((s) => s.classList.remove("done", "current"));
79
+ }
80
+
81
+ function setTapeStage(tape, stageIndex) {
82
+ $$(".tape-stage", tape).forEach((s) => {
83
+ const idx = Number(s.dataset.stage);
84
+ s.classList.remove("done", "current");
85
+ if (idx < stageIndex) s.classList.add("done");
86
+ else if (idx === stageIndex) s.classList.add("current");
87
+ });
88
+ }
89
+
90
+ // ---------------------------------------------------------- sequential reveal
91
+ function reveal(containerId, tapeId, items) {
92
+ const container = document.getElementById(containerId);
93
+ const tape = document.getElementById(tapeId);
94
+ container.innerHTML = "";
95
+ resetTape(tape);
96
+ items.forEach((item, i) => {
97
+ setTimeout(() => {
98
+ setTapeStage(tape, item.stage);
99
+ const el = document.createElement("div");
100
+ el.className = "step";
101
+ el.innerHTML = `<div class="step-label">${escapeHtml(item.label)}</div><div class="step-body">${item.html}</div>`;
102
+ container.appendChild(el);
103
+ requestAnimationFrame(() => el.classList.add("show"));
104
+ }, i * 480);
105
+ });
106
+ if (items.length) {
107
+ setTimeout(() => setTapeStage(tape, items[items.length - 1].stage), items.length * 480);
108
+ }
109
+ }
110
+
111
+ function errorBlock(msg) {
112
+ return `<div class="callout warn"><strong>Backend error โ€”</strong> ${escapeHtml(msg)}</div>`;
113
+ }
114
+
115
+ function linesFromTextarea(id) {
116
+ return $("#" + id).value.split("\n").map((s) => s.trim()).filter(Boolean);
117
+ }
118
+
119
+ async function postJSON(url, body) {
120
+ const res = await fetch(url, {
121
+ method: "POST",
122
+ headers: { "Content-Type": "application/json" },
123
+ body: JSON.stringify(body || {}),
124
+ });
125
+ if (!res.ok) {
126
+ const detail = await res.json().catch(() => ({}));
127
+ throw new Error(detail.error || `Request to ${url} failed (${res.status})`);
128
+ }
129
+ return res.json();
130
+ }
131
+
132
+ function withLoading(btn, fn) {
133
+ return async (...args) => {
134
+ if (btn.disabled) return;
135
+ const originalHtml = btn.innerHTML;
136
+ btn.disabled = true;
137
+ btn.innerHTML = `<span class="spinner"></span> Runningโ€ฆ`;
138
+ try {
139
+ await fn(...args);
140
+ } catch (err) {
141
+ console.error(err);
142
+ throw err;
143
+ } finally {
144
+ btn.disabled = false;
145
+ btn.innerHTML = originalHtml;
146
+ }
147
+ };
148
+ }
149
+
150
+ // ============================================================
151
+ // 1. ONE-HOT ENCODING
152
+ // ============================================================
153
+ async function runOnehot() {
154
+ const corpus = linesFromTextarea("onehot-corpus");
155
+ let data;
156
+ try {
157
+ data = await postJSON("/api/onehot", { corpus });
158
+ } catch (err) {
159
+ $("#out-onehot").innerHTML = errorBlock(err.message);
160
+ return;
161
+ }
162
+
163
+ const vocab = data.vocabulary;
164
+ const identityRows = vocab.map((w) => vocab.map((c) => (data.vectors[w][vocab.indexOf(c)] ? 1 : 0)));
165
+
166
+ const sentenceHtml = data.sentences
167
+ .map(
168
+ (s) => `<div style="margin-bottom:10px;">
169
+ <div style="color:var(--text-1); margin-bottom:6px;">โ€œ${escapeHtml(s.sentence)}โ€</div>
170
+ ${chipList(
171
+ s.tokens.map((t, i) => `${t} โ†’ [${s.vectors[i].join(",")}]`),
172
+ "teal"
173
+ )}
174
+ </div>`
175
+ )
176
+ .join("");
177
+
178
+ reveal("out-onehot", "tape-onehot", [
179
+ {
180
+ stage: 0,
181
+ label: "Raw sentences",
182
+ html: `<div class="chip-row">${corpus.map((c) => `<span class="chip muted">"${escapeHtml(c)}"</span>`).join("")}</div>`,
183
+ },
184
+ {
185
+ stage: 1,
186
+ label: "Tokenize each sentence",
187
+ html: data.sentences
188
+ .map((s) => `<div style="margin-bottom:8px;">${chipList(s.tokens, "amber")}</div>`)
189
+ .join(""),
190
+ },
191
+ {
192
+ stage: 2,
193
+ label: `Build vocabulary โ€” ${vocab.length} unique words`,
194
+ html:
195
+ chipList(vocab.map((w, i) => `${i}: ${w}`), "teal") +
196
+ `<div class="callout" style="margin-top:12px;">Vector length will equal the vocabulary size: every word vector here is <strong>${vocab.length}</strong>-dimensional.</div>`,
197
+ },
198
+ {
199
+ stage: 3,
200
+ label: "One-hot vectors (identity matrix over the vocabulary)",
201
+ html:
202
+ matrixTable(vocab, vocab, identityRows, { heat: "amber", corner: "word \\ index" }) +
203
+ `<div style="margin-top:14px;">${sentenceHtml}</div>`,
204
+ },
205
+ {
206
+ stage: 4,
207
+ label: "Cross-check with sklearn's OneHotEncoder",
208
+ html:
209
+ matrixTable(
210
+ data.sklearnCheck.inputWords.map((w, i) => `${w} (#${i})`),
211
+ data.sklearnCheck.categories,
212
+ data.sklearnCheck.matrix,
213
+ { heat: "violet", corner: "token" }
214
+ ) +
215
+ `<div class="callout warn" style="margin-top:14px;"><strong>Two real limitations:</strong> the vector size grows with every new word in the vocabulary, and a one-hot vector can't tell you that "cat" and "dog" are more alike than "cat" and "umbrella" โ€” there's no notion of relationship or context.</div>`,
216
+ },
217
+ ]);
218
+ }
219
+
220
+ // ============================================================
221
+ // 2. COUNT VECTORIZER
222
+ // ============================================================
223
+ async function runCount() {
224
+ const corpus = linesFromTextarea("count-corpus");
225
+ const stopWords = $("#count-stopwords").checked;
226
+ const maxFeatures = $("#count-maxfeatures").value || null;
227
+ const newDoc = $("#count-newdoc").value.trim();
228
+
229
+ let data;
230
+ try {
231
+ data = await postJSON("/api/count-vectorizer", { corpus, stopWords, maxFeatures, newDoc });
232
+ } catch (err) {
233
+ $("#out-count").innerHTML = errorBlock(err.message);
234
+ return;
235
+ }
236
+
237
+ const rowLabels = corpus.map((_, i) => `Doc ${i + 1}`);
238
+ const items = [
239
+ {
240
+ stage: 0,
241
+ label: "Raw corpus",
242
+ html: `<div class="chip-row">${corpus.map((c) => `<span class="chip muted">"${escapeHtml(c)}"</span>`).join("")}</div>`,
243
+ },
244
+ {
245
+ stage: 1,
246
+ label: "Tokenize every document",
247
+ html: data.tokenizedDocs
248
+ .map((toks, i) => `<div style="margin-bottom:8px;"><span class="card-note">Doc ${i + 1}:</span> ${chipList(toks, "amber")}</div>`)
249
+ .join(""),
250
+ },
251
+ {
252
+ stage: 2,
253
+ label: `CountVectorizer.fit() โ†’ vocabulary (${data.vocabulary.length} terms)`,
254
+ html: chipList(data.vocabulary, "teal") +
255
+ (data.settings.stopWords ? `<div class="callout" style="margin-top:10px;">English stop words removed before fitting.</div>` : "") +
256
+ (data.settings.maxFeatures ? `<div class="callout" style="margin-top:10px;">Limited to the top <strong>${data.settings.maxFeatures}</strong> most frequent terms.</div>` : ""),
257
+ },
258
+ {
259
+ stage: 3,
260
+ label: "Documentโ€“term count matrix",
261
+ html: matrixTable(rowLabels, data.vocabulary, data.matrix, { heat: "teal", corner: "" }),
262
+ },
263
+ ];
264
+
265
+ if (data.newDocResult) {
266
+ items.push({
267
+ stage: 4,
268
+ label: "Transform a brand-new document with the fitted vocabulary",
269
+ html:
270
+ `<div class="card-note" style="margin-bottom:8px;">โ€œ${escapeHtml(data.newDocResult.doc)}โ€</div>` +
271
+ matrixTable(["New doc"], data.vocabulary, [data.newDocResult.vector], { heat: "pink" }) +
272
+ `<div class="callout" style="margin-top:10px;">Any word here that wasn't in the original vocabulary is simply ignored โ€” <code>CountVectorizer.transform()</code> never grows the vocabulary after <code>fit()</code>.</div>`,
273
+ });
274
+ } else {
275
+ items.push({
276
+ stage: 4,
277
+ label: "Transform a new document",
278
+ html: `<div class="empty-hint">Type a sentence into โ€œNew document to transformโ€ above and run again to see <code>.transform()</code> applied to text the vectorizer never saw during <code>.fit()</code>.</div>`,
279
+ });
280
+ }
281
+
282
+ reveal("out-count", "tape-count", items);
283
+ }
284
+
285
+ // ============================================================
286
+ // 3. BAG OF WORDS
287
+ // ============================================================
288
+ async function runBow() {
289
+ const corpus = linesFromTextarea("bow-corpus");
290
+ let data;
291
+ try {
292
+ data = await postJSON("/api/bow", { corpus });
293
+ } catch (err) {
294
+ $("#out-bow").innerHTML = errorBlock(err.message);
295
+ return;
296
+ }
297
+
298
+ const rowLabels = corpus.map((_, i) => `Doc ${i + 1}`);
299
+
300
+ reveal("out-bow", "tape-bow", [
301
+ {
302
+ stage: 0,
303
+ label: "Raw corpus",
304
+ html: `<div class="chip-row">${corpus.map((c) => `<span class="chip muted">"${escapeHtml(c)}"</span>`).join("")}</div>`,
305
+ },
306
+ {
307
+ stage: 1,
308
+ label: "Tokenize",
309
+ html: data.tokenizedDocs
310
+ .map((toks, i) => `<div style="margin-bottom:8px;"><span class="card-note">Doc ${i + 1}:</span> ${chipList(toks, "amber")}</div>`)
311
+ .join(""),
312
+ },
313
+ {
314
+ stage: 2,
315
+ label: `Build the bag โ€” ${data.vocabulary.length} unique words across the corpus`,
316
+ html: chipList(data.vocabulary, "teal"),
317
+ },
318
+ {
319
+ stage: 3,
320
+ label: "Bag-of-Words frequency matrix (hand-rolled counter)",
321
+ html: matrixTable(rowLabels, data.vocabulary, data.matrix, { heat: "teal" }),
322
+ },
323
+ {
324
+ stage: 4,
325
+ label: "Binary BoW โ€” presence (1) vs. absence (0), not raw counts",
326
+ html: matrixTable(rowLabels, data.binaryVocabulary, data.binaryMatrix, { heat: "amber" }),
327
+ },
328
+ {
329
+ stage: 5,
330
+ label: "Cosine similarity between documents, derived from the BoW vectors",
331
+ html:
332
+ matrixTable(rowLabels, rowLabels, data.cosineSimilarity, { heat: "pink" }) +
333
+ `<div class="callout" style="margin-top:12px;">1.0 means identical word-frequency profiles; 0 means no shared vocabulary at all. Documents that share more frequent words end up closer together.</div>`,
334
+ },
335
+ ]);
336
+ }
337
+
338
+ // ============================================================
339
+ // 4. N-GRAMS
340
+ // ============================================================
341
+ async function runNgrams() {
342
+ const sentence = $("#ngrams-sentence").value.trim();
343
+ const corpus = linesFromTextarea("ngrams-corpus");
344
+ let data;
345
+ try {
346
+ data = await postJSON("/api/ngrams", { sentence, corpus });
347
+ } catch (err) {
348
+ $("#out-ngrams").innerHTML = errorBlock(err.message);
349
+ return;
350
+ }
351
+
352
+ const rowLabels = data.corpus.map((_, i) => `Doc ${i + 1}`);
353
+ const m = data.ngramMatrices;
354
+
355
+ reveal("out-ngrams", "tape-ngrams", [
356
+ {
357
+ stage: 0,
358
+ label: "Sentence",
359
+ html: `<div class="chip-row"><span class="chip muted">"${escapeHtml(data.sentence)}"</span></div>`,
360
+ },
361
+ {
362
+ stage: 1,
363
+ label: "Unigrams (N=1) โ€” identical to plain tokenization",
364
+ html: chipList(data.manual.unigrams, "teal"),
365
+ },
366
+ {
367
+ stage: 2,
368
+ label: "Bigrams (N=2) โ€” consecutive word pairs",
369
+ html: chipList(data.manual.bigrams, "amber"),
370
+ },
371
+ {
372
+ stage: 3,
373
+ label: "Trigrams (N=3) โ€” consecutive word triples",
374
+ html: chipList(data.manual.trigrams, "pink"),
375
+ },
376
+ {
377
+ stage: 4,
378
+ label: "N-gram documentโ€“term matrices over the corpus",
379
+ html: `
380
+ <div class="card-note" style="margin-bottom:6px;">${m.unigrams.label} โ€” ${m.unigrams.vocabulary.length} terms</div>
381
+ ${matrixTable(rowLabels, m.unigrams.vocabulary, m.unigrams.matrix, { heat: "teal" })}
382
+ <div class="card-note" style="margin:16px 0 6px;">${m.bigrams.label} โ€” ${m.bigrams.vocabulary.length} terms</div>
383
+ ${matrixTable(rowLabels, m.bigrams.vocabulary, m.bigrams.matrix, { heat: "amber" })}
384
+ <div class="card-note" style="margin:16px 0 6px;">${m.uni_bi.label} โ€” ${m.uni_bi.vocabulary.length} terms</div>
385
+ ${matrixTable(rowLabels, m.uni_bi.vocabulary, m.uni_bi.matrix, { heat: "pink" })}
386
+ <div class="callout" style="margin-top:14px;">Character-level bigrams+trigrams (<code>analyzer='char_wb'</code>) on the same corpus produce <strong>${data.charLevel.vocabularySize}</strong> features. Sample: ${data.charLevel.sample.map((s) => `<code>"${escapeHtml(s)}"</code>`).join(", ")} โ€ฆ</div>
387
+ `,
388
+ },
389
+ ]);
390
+ }
391
+
392
+ // ============================================================
393
+ // 5. TF-IDF
394
+ // ============================================================
395
+ async function runTfidf() {
396
+ const corpus = linesFromTextarea("tfidf-corpus");
397
+ let data;
398
+ try {
399
+ data = await postJSON("/api/tfidf", { corpus });
400
+ } catch (err) {
401
+ $("#out-tfidf").innerHTML = errorBlock(err.message);
402
+ return;
403
+ }
404
+
405
+ const rowLabels = corpus.map((_, i) => `Doc ${i + 1}`);
406
+
407
+ const tfHtml = data.manualPerDoc
408
+ .map(
409
+ (d, i) => `<div style="margin-bottom:14px;">
410
+ <div class="card-note" style="margin-bottom:6px;">Doc ${i + 1}: โ€œ${escapeHtml(d.doc)}โ€</div>
411
+ ${kvList(Object.entries(d.tf), "teal")}
412
+ </div>`
413
+ )
414
+ .join("");
415
+
416
+ const idfHtml = kvList(Object.entries(data.idf), "amber");
417
+
418
+ const tfidfHtml = data.manualPerDoc
419
+ .map(
420
+ (d, i) => `<div style="margin-bottom:14px;">
421
+ <div class="card-note" style="margin-bottom:6px;">Doc ${i + 1}: โ€œ${escapeHtml(d.doc)}โ€</div>
422
+ ${kvList(Object.entries(d.tfidf), "pink")}
423
+ </div>`
424
+ )
425
+ .join("");
426
+
427
+ const topWordsHtml = data.topWords
428
+ .map(
429
+ (d) => `<div style="margin-bottom:10px;">
430
+ <div class="card-note" style="margin-bottom:6px;">โ€œ${escapeHtml(d.doc)}โ€</div>
431
+ ${chipList(d.top.map((t) => `${t.word} ยท ${t.score}`), "amber")}
432
+ </div>`
433
+ )
434
+ .join("");
435
+
436
+ reveal("out-tfidf", "tape-tfidf", [
437
+ {
438
+ stage: 0,
439
+ label: "Raw corpus",
440
+ html: `<div class="chip-row">${corpus.map((c) => `<span class="chip muted">"${escapeHtml(c)}"</span>`).join("")}</div>`,
441
+ },
442
+ { stage: 1, label: "Term Frequency โ€” count(t, d) / total words in d", html: `<div class="two-col">${tfHtml}</div>` },
443
+ { stage: 2, label: "Inverse Document Frequency โ€” log(N / (1 + df(t))) + 1, across the whole corpus", html: idfHtml },
444
+ { stage: 3, label: "TF ร— IDF, computed by hand per document (non-zero terms only)", html: `<div class="two-col">${tfidfHtml}</div>` },
445
+ {
446
+ stage: 4,
447
+ label: "scikit-learn's TfidfVectorizer, for comparison",
448
+ html: matrixTable(rowLabels, data.sklearn.vocabulary, data.sklearn.matrix, { heat: "violet" }) +
449
+ `<div class="callout" style="margin-top:10px;">sklearn additionally L2-normalizes each row, so the exact numbers differ slightly from the hand-rolled version above โ€” the ranking of important words matches.</div>`,
450
+ },
451
+ { stage: 5, label: "Top 3 highest-weighted words per document", html: topWordsHtml },
452
+ ]);
453
+ }
454
+
455
+ // ============================================================
456
+ // 6. WORD EMBEDDINGS
457
+ // ============================================================
458
+ function renderScatter(points) {
459
+ const w = 560, h = 340, pad = 40;
460
+ if (!points.length) return `<div class="empty-hint">Not enough plottable words.</div>`;
461
+ const xs = points.map((p) => p.x), ys = points.map((p) => p.y);
462
+ const minX = Math.min(...xs), maxX = Math.max(...xs);
463
+ const minY = Math.min(...ys), maxY = Math.max(...ys);
464
+ const spanX = maxX - minX || 1, spanY = maxY - minY || 1;
465
+ const colors = [PALETTE.amber, PALETTE.teal, PALETTE.pink, PALETTE.violet];
466
+
467
+ const sx = (x) => pad + ((x - minX) / spanX) * (w - 2 * pad);
468
+ const sy = (y) => h - pad - ((y - minY) / spanY) * (h - 2 * pad);
469
+
470
+ let svg = `<svg viewBox="0 0 ${w} ${h}" width="100%" height="${h}" xmlns="http://www.w3.org/2000/svg">`;
471
+ svg += `<line x1="${pad}" y1="${h / 2}" x2="${w - pad}" y2="${h / 2}" stroke="#283149" stroke-dasharray="4 4"/>`;
472
+ svg += `<line x1="${w / 2}" y1="${pad}" x2="${w / 2}" y2="${h - pad}" stroke="#283149" stroke-dasharray="4 4"/>`;
473
+ points.forEach((p, i) => {
474
+ const cx = sx(p.x), cy = sy(p.y);
475
+ const c = colors[i % colors.length];
476
+ svg += `<circle cx="${cx}" cy="${cy}" r="6" fill="rgba(${c},0.9)" stroke="#0a0d13" stroke-width="1.5">
477
+ <animate attributeName="r" from="0" to="6" dur="0.4s" begin="${i * 0.08}s" fill="freeze"/>
478
+ </circle>`;
479
+ svg += `<text x="${cx + 9}" y="${cy + 4}" font-size="11.5" fill="#eef1f7">${escapeHtml(p.word)}</text>`;
480
+ });
481
+ svg += `</svg>`;
482
+ return `<div class="scatter-wrap">${svg}</div><div class="legend"><span>PCA reduces the 50-dimensional Word2Vec space to 2D so it can be drawn โ€” distance and direction on this plot are only an approximation of similarity in the real, high-dimensional space.</span></div>`;
483
+ }
484
+
485
+ async function runEmbeddings() {
486
+ const sentences = linesFromTextarea("embed-sentences");
487
+ let data;
488
+ try {
489
+ data = await postJSON("/api/embeddings", { sentences });
490
+ } catch (err) {
491
+ $("#out-embeddings").innerHTML = errorBlock(err.message);
492
+ return;
493
+ }
494
+
495
+ const simRows = data.similarities.map((s) =>
496
+ s.error
497
+ ? `<tr><td>${escapeHtml(s.pair.join(" โ†” "))}</td><td colspan="2" class="card-note">${escapeHtml(s.error)}</td></tr>`
498
+ : `<tr><td>${escapeHtml(s.pair.join(" โ†” "))}</td><td>${formatNum(s.skipgram)}</td><td>${formatNum(s.cbow)}</td></tr>`
499
+ );
500
+
501
+ const mostSimHtml = Object.entries(data.mostSimilar)
502
+ .map(
503
+ ([word, sims]) => `<div style="margin-bottom:10px;">
504
+ <span class="card-note">${escapeHtml(word)} is most similar to:</span>
505
+ ${chipList(sims.map((s) => `${s.word} ยท ${s.score}`), "teal")}
506
+ </div>`
507
+ )
508
+ .join("");
509
+
510
+ const items = [
511
+ {
512
+ stage: 0,
513
+ label: `Training sentences (tokenized) โ€” vocabulary of ${data.vocabSize} words`,
514
+ html: chipList(data.sentences, "muted", 0.03),
515
+ },
516
+ {
517
+ stage: 1,
518
+ label: `Train Word2Vec (Skip-gram & CBOW) โ€” finished in ${data.trainSeconds}s`,
519
+ html: data.sampleWord
520
+ ? `<div class="card-note" style="margin-bottom:6px;">First 10 of 50 dimensions for โ€œ${escapeHtml(data.sampleWord)}โ€:</div>
521
+ <div class="formula">[ ${data.sampleVector.map(formatNum).join(", ")}, โ€ฆ ]</div>`
522
+ : `<div class="empty-hint">No vocabulary produced โ€” add a few more training sentences.</div>`,
523
+ },
524
+ {
525
+ stage: 2,
526
+ label: "Cosine similarity between word pairs, Skip-gram vs CBOW",
527
+ html: `<table class="compare-table"><thead><tr><th>pair</th><th>skip-gram</th><th>cbow</th></tr></thead><tbody>${simRows.join("")}</tbody></table>`,
528
+ },
529
+ {
530
+ stage: 3,
531
+ label: "Most similar words (Skip-gram model)",
532
+ html: mostSimHtml || `<div class="empty-hint">None of the probe words were found in this vocabulary โ€” try adding "cat", "king" or "paris" to your sentences.</div>`,
533
+ },
534
+ {
535
+ stage: 4,
536
+ label: "All trained vectors, projected to 2D with PCA",
537
+ html: renderScatter(data.pcaPoints),
538
+ },
539
+ ];
540
+
541
+ if (data.fastText && !data.fastText.error) {
542
+ items.push({
543
+ stage: 5,
544
+ label: "FastText: vectors for out-of-vocabulary words via character n-grams",
545
+ html: `
546
+ <div class="two-col">
547
+ <div>
548
+ <div class="card-note" style="margin-bottom:6px;">โ€œ${escapeHtml(data.fastText.inVocabWord)}โ€ โ€” seen during training</div>
549
+ <div class="formula">[ ${data.fastText.inVocabVector.map(formatNum).join(", ")}, โ€ฆ ]</div>
550
+ </div>
551
+ <div>
552
+ <div class="card-note" style="margin-bottom:6px;">โ€œ${escapeHtml(data.fastText.oovWord)}โ€ โ€” never seen during training</div>
553
+ <div class="formula">[ ${data.fastText.oovVector.map(formatNum).join(", ")}, โ€ฆ ]</div>
554
+ </div>
555
+ </div>
556
+ <div class="callout" style="margin-top:12px;"><strong>Why this works:</strong> ${escapeHtml(data.fastText.note)} Word2Vec, by contrast, would raise a <code>KeyError</code> for an unseen word โ€” it has no concept of subword structure.</div>
557
+ `,
558
+ });
559
+ }
560
+
561
+ reveal("out-embeddings", "tape-embeddings", items);
562
+ }
563
+
564
+ // ============================================================
565
+ // Navigation + wiring
566
+ // ============================================================
567
+ const RUNNERS = {
568
+ onehot: runOnehot,
569
+ count: runCount,
570
+ bow: runBow,
571
+ ngrams: runNgrams,
572
+ tfidf: runTfidf,
573
+ embeddings: runEmbeddings,
574
+ };
575
+
576
+ const DEFAULT_TEXT = {
577
+ "onehot-corpus": "I love NLP\nNLP is fun\nI love coding",
578
+ "count-corpus": "I love NLP and I love Python\nNLP is amazing and fun\nPython is great for NLP",
579
+ "bow-corpus": "the cat sat on the mat\nthe dog sat on the log\nthe cat and the dog are friends",
580
+ "ngrams-sentence": "I love studying Natural Language Processing",
581
+ "ngrams-corpus": "I love NLP and machine learning\nmachine learning is part of AI\nNLP is a branch of AI",
582
+ "tfidf-corpus": "I love NLP and machine learning\nmachine learning is part of AI\nNLP is a branch of AI\nI love AI and deep learning",
583
+ "embed-sentences":
584
+ "the cat sat on the mat\nthe dog ran on the grass\ncats and dogs are pets\ni love my cat\ni love my dog\nking and queen are royalty\nman and woman are humans\nparis is the capital of france\nberlin is the capital of germany",
585
+ };
586
+
587
+ const autoRan = new Set();
588
+
589
+ function activateSection(target) {
590
+ $$(".section").forEach((s) => s.classList.toggle("active", s.id === `sec-${target}`));
591
+ $$(".nav-item").forEach((b) => b.classList.toggle("active", b.dataset.target === target));
592
+ window.scrollTo({ top: 0, behavior: "instant" in window ? "instant" : "auto" });
593
+
594
+ if (RUNNERS[target] && !autoRan.has(target)) {
595
+ autoRan.add(target);
596
+ RUNNERS[target]();
597
+ }
598
+ }
599
+
600
+ function wireNav() {
601
+ $$(".nav-item, .tech-card").forEach((btn) => {
602
+ btn.addEventListener("click", () => activateSection(btn.dataset.target));
603
+ });
604
+ }
605
+
606
+ function wireRunButtons() {
607
+ $$("[data-run]").forEach((btn) => {
608
+ const key = btn.dataset.run;
609
+ btn.addEventListener("click", withLoading(btn, RUNNERS[key]));
610
+ });
611
+ $$("[data-reset]").forEach((btn) => {
612
+ btn.addEventListener("click", () => {
613
+ const key = btn.dataset.reset;
614
+ Object.keys(DEFAULT_TEXT)
615
+ .filter((id) => id.startsWith(key + "-"))
616
+ .forEach((id) => {
617
+ const el = document.getElementById(id);
618
+ if (el) el.value = DEFAULT_TEXT[id];
619
+ });
620
+ if (key === "count") {
621
+ $("#count-maxfeatures").value = "";
622
+ $("#count-newdoc").value = "";
623
+ $("#count-stopwords").checked = false;
624
+ }
625
+ RUNNERS[key]();
626
+ });
627
+ });
628
+ }
629
+
630
+ document.addEventListener("DOMContentLoaded", () => {
631
+ wireNav();
632
+ wireRunButtons();
633
+ });
634
+ })();