Chapter 20 of 22Part IV: Data, Media & Machine Intelligence
Inside the Mind of Artificial Intelligence
5 Verified Book Snippets
Every .js file below is byte-identical (SHA-256 verified) to the printed code in Volume 1. Runnable blocks execute inside an isolated sandboxed iframe and Web Worker with a 2-second timeout and zero network access.
Printed book snippet (byte-identical) · 533 bytes · SHA-256: 116187413c22…
Standalone runnable
Dependencies: None (standard ES2022 JavaScript). Defines pure JavaScript functions/data; runs cleanly in the sandbox (add a console.log call in your own test to inspect return values).
const trainData = [
[0.1, 0], [0.2, 0], [0.3, 0],
[0.7, 1], [0.8, 1], [0.9, 1]
];
function sigmoid(z) { return 1 / (1 + Math.exp(-z)); }
function train(data, epochs = 400) {
let w = 0, b = 0;
for (let epoch = 0; epoch < epochs; epoch++) {
let dw = 0, db = 0;
for (const [x, y] of data) {
const error = sigmoid(w*x+b) - y;
dw += error*x; db += error;
}
w -= 0.5*dw/data.length;
b -= 0.5*db/data.length;
}
return {w,b};
}
function predict(model, x) {
return sigmoid(model.w*x + model.b);
}
Printed book snippet (byte-identical) · 623 bytes · SHA-256: 09347b118a3a…
Standalone runnable
Dependencies: None (standard ES2022 JavaScript). Defines pure JavaScript functions/data; runs cleanly in the sandbox (add a console.log call in your own test to inspect return values).
const corpus = [
'the sky is blue', 'the sky is clear',
'the sky is blue', 'the lake is calm'
];
function nextWords(lines, word) {
const counts = new Map();
for (const line of lines) {
const tokens = line.toLowerCase().match(/[a-z]+/g) || [];
for (let i = 0; i + 1 < tokens.length; i++) {
if (tokens[i] === word.toLowerCase()) {
const t = tokens[i+1];
counts.set(t, (counts.get(t)||0)+1);
}
}
}
const total = [...counts.values()].reduce((a,b)=>a+b,0);
return [...counts].map(([word,n]) => ({word,p:n/total}))
.sort((a,b)=>b.p-a.p || a.word.localeCompare(b.word));
}
Printed book snippet (byte-identical) · 260 bytes · SHA-256: 6d9a403b776f…
Standalone runnable
Dependencies: None (standard ES2022 JavaScript). Defines pure JavaScript functions/data; runs cleanly in the sandbox (add a console.log call in your own test to inspect return values).
const notes = [
{id: 'C03', text: 'A barcode identifies a product.'},
{id: 'C04', text: 'QR codes use error correction.'},
{id: 'C10', text: 'GPS receivers estimate position using signals.'},
{id: 'C13', text: 'A hash helps detect changes in data.'}
];
Printed book snippet (byte-identical) · 624 bytes · SHA-256: 2aed6bfb569f…
Standalone runnable
Dependencies: None (standard ES2022 JavaScript). Defines pure JavaScript functions/data; runs cleanly in the sandbox (add a console.log call in your own test to inspect return values).
function terms(s) {
const stop = new Set(['a','an','the','is','what','does','how','in']);
return new Set((s.toLowerCase().match(/[a-z]+/g)||[])
.filter(t => !stop.has(t)));
}
function answer(question, library = notes) {
const q = terms(question);
const scored = library.map(n => ({...n, score:
[...terms(n.text)].filter(t=>q.has(t)).length}));
scored.sort((a,b)=>b.score-a.score);
if (!scored.length || scored[0].score < 1 ||
(scored[1] && scored[1].score === scored[0].score))
return 'I need more evidence or a clearer question.';
return scored[0].text + ' [Source: ' + scored[0].id + ']';
}