egatari 0.6.8 → 0.6.9
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/CHANGELOG.md +37 -1
- package/README.ja.md +2 -2
- package/README.md +2 -2
- package/components.lock.json +84 -3
- package/components2.lock.json +202 -1
- package/kit/check-messages.mjs +5 -0
- package/kit/check.mjs +18 -4
- package/kit/core/check-parity.mjs +62 -0
- package/kit/core/check-source.mjs +56 -0
- package/kit/core/colab-core.mjs +1 -1
- package/kit/core/color-vocab.mjs +8 -2
- package/kit/core/layout-scan.mjs +7 -4
- package/kit/core/llm-pack-assemble.mjs +1 -1
- package/kit/core/llm-pack-data.mjs +185 -9
- package/kit/core/llm-pack.mjs +1 -1
- package/kit/core/narration-lint.mjs +12 -0
- package/kit/core/new-deck.mjs +3 -0
- package/kit/core/part-labels.mjs +6 -0
- package/kit/core/template-text.mjs +5 -3
- package/kit/deck.js +88 -13
- package/kit/instruction-version.json +1 -1
- package/kit/player.json +1 -1
- package/kit/player.lock.json +2 -2
- package/kit/presenter.js +33 -9
- package/kit/tools.js +4 -0
- package/package.json +1 -1
- package/themes/asagi/theme.json +1 -1
- package/themes/asagi/tokens.css +12 -0
- package/themes/business/theme.json +1 -1
- package/themes/business/tokens.css +12 -0
- package/themes/default/README.ja.md +24 -1
- package/themes/default/README.md +24 -1
- package/themes/default/theme.json +1 -1
- package/themes/default/tokens.css +12 -0
- package/themes/kogecha/theme.json +1 -1
- package/themes/kogecha/tokens.css +12 -0
- package/themes/kokugo/theme.json +1 -1
- package/themes/kokugo/tokens.css +12 -0
- package/themes/murasaki/theme.json +1 -1
- package/themes/murasaki/tokens.css +12 -0
- package/themes/sumi/theme.json +1 -1
- package/themes/sumi/tokens.css +12 -0
- package/themes/tokiwa/theme.json +1 -1
- package/themes/tokiwa/tokens.css +12 -0
- package/themes.lock.json +80 -24
- package/vendor/components/CATALOG.md +7 -1
- package/vendor/components/sha256/1298d779fe60ccfc09747fbdba47e2111175bd7fd2385a5b68c8f2644cd267be/README.en.md +23 -0
- package/vendor/components/sha256/1298d779fe60ccfc09747fbdba47e2111175bd7fd2385a5b68c8f2644cd267be/index.js +120 -0
- package/vendor/components/sha256/7aea8341beffcd4ad95c12b7e7e3be3f0f52eb7932d424be9e74e2fbd7339e45/README.en.md +23 -0
- package/vendor/components/sha256/7aea8341beffcd4ad95c12b7e7e3be3f0f52eb7932d424be9e74e2fbd7339e45/index.js +289 -0
- package/vendor/components/sha256/90ed438b40ed8d765348834b2bed2baad9b0053c2168c218335b6d95b9c80283/README.en.md +44 -0
- package/vendor/components/sha256/90ed438b40ed8d765348834b2bed2baad9b0053c2168c218335b6d95b9c80283/index.js +203 -0
- package/vendor/components/sha256/a69ab76ff7d0746d093b4e606d7b113be921cbb156edc16c2ad40aafab280d42/README.en.md +22 -0
- package/vendor/components/sha256/a69ab76ff7d0746d093b4e606d7b113be921cbb156edc16c2ad40aafab280d42/index.js +145 -0
- package/vendor/components/sha256/d75c461bb7b2b1c9a2b6d15bee1e11bcec88ef94931174d6b222cd309f6a0287/README.en.md +24 -0
- package/vendor/components/sha256/d75c461bb7b2b1c9a2b6d15bee1e11bcec88ef94931174d6b222cd309f6a0287/index.js +103 -0
- package/vendor/components/sha256/decb7c026527ea269a401efedcb5d4d56d186074b286d2f94772068bef56b256/README.en.md +31 -0
- package/vendor/components/sha256/decb7c026527ea269a401efedcb5d4d56d186074b286d2f94772068bef56b256/index.js +267 -0
- package/vendor/components/sha256/f13af97d7ebf9893d5783eff27e4e1ce092ccd01783b332ad01aa0d201356548/README.en.md +38 -0
- package/vendor/components/sha256/f13af97d7ebf9893d5783eff27e4e1ce092ccd01783b332ad01aa0d201356548/index.js +266 -0
- package/vendor/components-2/manifest-2.6.0.json +2104 -0
- package/vendor/egatari-spec/SOURCE.json +2 -2
- package/vendor/egatari-spec/schema/egatari-audio.schema.json +21 -0
- package/vendor/themes/asagi/1.1.0/theme.json +14 -0
- package/vendor/themes/asagi/1.1.0/tokens.css +18 -0
- package/vendor/themes/business/1.1.0/theme.json +5 -0
- package/vendor/themes/business/1.1.0/tokens.css +29 -0
- package/vendor/themes/default/1.3.0/theme.json +4 -0
- package/vendor/themes/default/1.3.0/tokens.css +74 -0
- package/vendor/themes/kogecha/1.1.0/theme.json +10 -0
- package/vendor/themes/kogecha/1.1.0/tokens.css +17 -0
- package/vendor/themes/kokugo/1.1.0/theme.json +5 -0
- package/vendor/themes/kokugo/1.1.0/tokens.css +13 -0
- package/vendor/themes/murasaki/1.1.0/theme.json +10 -0
- package/vendor/themes/murasaki/1.1.0/tokens.css +13 -0
- package/vendor/themes/sumi/1.1.0/theme.json +10 -0
- package/vendor/themes/sumi/1.1.0/tokens.css +37 -0
- package/vendor/themes/tokiwa/1.1.0/theme.json +10 -0
- package/vendor/themes/tokiwa/1.1.0/tokens.css +15 -0
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
// m-ci: repeated normal samples and known-sigma z confidence intervals.
|
|
2
|
+
egatariParts.define({name: "ci", version: "2.0.0", requires: {base: "1.1.0", plot: "1.1.1"}, element: "m-ci"}, function(deps){
|
|
3
|
+
"use strict";
|
|
4
|
+
var base = deps.base;
|
|
5
|
+
var TEXT = {
|
|
6
|
+
ja: {count: function(h, r, p){ return "μ を含む:" + h + " / " + r + "(" + p + " %)"; }, meaning: "信頼区間:反復した区間の約", end: " % が μ を含む"},
|
|
7
|
+
en: {count: function(h, r, p){ return "contain μ: " + h + " / " + r + " (" + p + " %)"; }, meaning: "Confidence intervals: about ", end: "% of repeated intervals contain μ"}
|
|
8
|
+
};
|
|
9
|
+
function set(e, a, v){ e.setAttribute(a, String(v)); }
|
|
10
|
+
function random(seed){
|
|
11
|
+
return function(){
|
|
12
|
+
seed = (seed + 0x6D2B79F5) | 0;
|
|
13
|
+
var z = Math.imul(seed ^ seed >>> 15, 1 | seed);
|
|
14
|
+
z ^= z + Math.imul(z ^ z >>> 7, 61 | z);
|
|
15
|
+
return ((z ^ z >>> 14) >>> 0) / 4294967296;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
// Acklam's rational inverse-normal approximation; central and tail regions.
|
|
19
|
+
function quantile(p){
|
|
20
|
+
var q, r;
|
|
21
|
+
if (p < 0.02425 || p > 0.97575){
|
|
22
|
+
q = Math.sqrt(-2 * Math.log(p < 0.02425 ? p : 1 - p));
|
|
23
|
+
r = (((((-0.007784894002430293 * q - 0.3223964580411365) * q - 2.400758277161838) * q - 2.549732539343734) * q + 4.374664141464968) * q + 2.938163982698783) /
|
|
24
|
+
((((0.007784695709041462 * q + 0.3224671290700398) * q + 2.445134137142996) * q + 3.754408661907416) * q + 1);
|
|
25
|
+
return p < 0.02425 ? r : -r;
|
|
26
|
+
}
|
|
27
|
+
q = p - 0.5; r = q * q;
|
|
28
|
+
return (((((-39.69683028665376 * r + 220.9460984245205) * r - 275.9285104469687) * r + 138.357751867269) * r - 30.66479806614716) * r + 2.506628277459239) * q /
|
|
29
|
+
(((((-54.47609879822406 * r + 161.5858368580409) * r - 155.6989798598866) * r + 66.80131188771972) * r - 13.28068155288572) * r + 1);
|
|
30
|
+
}
|
|
31
|
+
function ticks(lo, hi){
|
|
32
|
+
var rough = (hi - lo) / 5, power = Math.pow(10, Math.floor(Math.log10(rough))), f = rough / power;
|
|
33
|
+
var step = (f <= 1 ? 1 : f <= 2 ? 2 : f <= 5 ? 5 : 10) * power, out = [];
|
|
34
|
+
for (var i = Math.ceil(lo / step); out.length < 10 && i * step <= hi + step * 1e-9; i++) out.push(+(i * step).toPrecision(12));
|
|
35
|
+
return out;
|
|
36
|
+
}
|
|
37
|
+
function num(v){ return String(+v.toPrecision(6)); }
|
|
38
|
+
return class extends base.MotionElement {
|
|
39
|
+
static partName = "ci";
|
|
40
|
+
static preview = "none";
|
|
41
|
+
static steps = 3;
|
|
42
|
+
static shape = {schema: {type: "object", additionalProperties: false, required: ["mu", "sigma", "n"], properties: {
|
|
43
|
+
mu: {type: "number"}, sigma: {type: "number"}, n: {type: "number"}, reps: {type: "number"}, level: {type: "number"}, seed: {type: "number"}
|
|
44
|
+
}}, example: {mu: 50, sigma: 10, n: 25, reps: 40, level: 0.95, seed: 2}};
|
|
45
|
+
static props = {
|
|
46
|
+
dur: {type: "number", default: 1, doc: "段階の秒数"},
|
|
47
|
+
fast: {type: "number", default: 2.5, doc: "残りの区間の秒数"},
|
|
48
|
+
size: {type: "number", default: 0, doc: "文字 px(0 は 26)"}
|
|
49
|
+
};
|
|
50
|
+
duration(k){
|
|
51
|
+
if (k < 1 || k > 3) return 0;
|
|
52
|
+
var r = this.model ? this.model.reps : this.data.reps === undefined ? 40 : this.data.reps;
|
|
53
|
+
return k === 2 ? Math.min(3, r) * this.props.dur + (r > 3 ? this.props.fast : 0) : this.props.dur;
|
|
54
|
+
}
|
|
55
|
+
label(g, x, y, s, anchor){
|
|
56
|
+
var e = base.el("text", {x: x, y: y, "font-size": this.fs, "text-anchor": anchor || "start", fill: "var(--fg)"}, g);
|
|
57
|
+
e.textContent = s; return e;
|
|
58
|
+
}
|
|
59
|
+
mount(ctx){
|
|
60
|
+
var self = this, d = this.data, P = this.props;
|
|
61
|
+
this.T = /^ja\b/i.test(ctx.lang || "") ? TEXT.ja : TEXT.en;
|
|
62
|
+
["dur", "fast"].forEach(function(key){ if (!(Number.isFinite(P[key]) && P[key] > 0)){ ctx.report("attr", "[ci] invalid " + key + "; using default"); P[key] = key === "dur" ? 1 : 2.5; } });
|
|
63
|
+
if (!(P.size === 0 || Number.isFinite(P.size) && P.size >= 24)){ ctx.report("attr", "[ci] size must be 0 or at least 24; using 26"); P.size = 0; }
|
|
64
|
+
this.fs = P.size || 26;
|
|
65
|
+
this.style.display = "block"; this.style.position = "relative"; this.style.writingMode = "horizontal-tb";
|
|
66
|
+
this.setAttribute("role", "img");
|
|
67
|
+
var reps = d.reps === undefined ? 40 : d.reps, level = d.level === undefined ? 0.95 : d.level, seed = d.seed === undefined ? 2 : d.seed;
|
|
68
|
+
if (!(d.sigma > 0) || !Number.isSafeInteger(d.n) || d.n < 1 || !Number.isSafeInteger(reps) || reps < 1 || !(level > 0 && level < 1)){
|
|
69
|
+
ctx.report("data", "[ci] sigma > 0, positive safe integers n and reps and 0 < level < 1 required"); return;
|
|
70
|
+
}
|
|
71
|
+
if (!Number.isSafeInteger(seed)){ ctx.report("data", "[ci] seed must be a safe integer; using 2"); seed = 2; }
|
|
72
|
+
var z = level === 0.9 ? 1.645 : level === 0.95 ? 1.96 : level === 0.99 ? 2.576 : -quantile((1 - level) / 2);
|
|
73
|
+
var half = z * (d.sigma / Math.sqrt(d.n)), rng = random(seed), intervals = [], spare = null;
|
|
74
|
+
function normal(){
|
|
75
|
+
if (spare !== null){ var saved = spare; spare = null; return saved; }
|
|
76
|
+
// 1-rng() is in (0,1], so Box–Muller never takes log(0).
|
|
77
|
+
var radius = Math.sqrt(-2 * Math.log(1 - rng())), angle = 2 * Math.PI * rng();
|
|
78
|
+
spare = radius * Math.sin(angle);
|
|
79
|
+
return radius * Math.cos(angle);
|
|
80
|
+
}
|
|
81
|
+
for (var i = 0; i < reps; i++){
|
|
82
|
+
var sum = 0;
|
|
83
|
+
for (var j = 0; j < d.n; j++){
|
|
84
|
+
sum += normal();
|
|
85
|
+
}
|
|
86
|
+
var mean = d.mu + d.sigma * (sum / d.n), lo = mean - half, hi = mean + half;
|
|
87
|
+
intervals.push({mean: mean, lo: lo, hi: hi, hit: lo <= d.mu && hi >= d.mu});
|
|
88
|
+
}
|
|
89
|
+
var low = Math.min(d.mu, ...intervals.map(function(o){ return o.lo; })), high = Math.max(d.mu, ...intervals.map(function(o){ return o.hi; }));
|
|
90
|
+
if (![z, half, low, high, high - low].every(Number.isFinite) || !(high > low)){
|
|
91
|
+
ctx.report("data", "[ci] numerical range cannot represent these intervals; rescale mu and sigma"); return;
|
|
92
|
+
}
|
|
93
|
+
var hits = intervals.filter(function(o){ return o.hit; }).length;
|
|
94
|
+
this.model = {mu: d.mu, sigma: d.sigma, n: d.n, reps: reps, level: level, seed: seed, z: z, half: half, intervals: intervals, hits: hits};
|
|
95
|
+
var count = this.T.count(hits, reps, +(100 * hits / reps).toFixed(1)), muLabel = "μ = " + num(d.mu);
|
|
96
|
+
if (!this.getAttribute("aria-label")) this.setAttribute("aria-label", this.T.meaning + num(level * 100) + this.T.end + "; " + muLabel + "; σ = " + num(d.sigma) + "; n = " + d.n + "; " + count);
|
|
97
|
+
var fs = this.fs, line = fs * 1.45 + 4, W = Math.max(1, this.clientWidth), H = Math.max(1, this.clientHeight);
|
|
98
|
+
var axisY = H - 2 * line - fs * 0.4 - 4, top = line + 14, bottom = axisY - 14;
|
|
99
|
+
var pad = (high - low) * 0.05;
|
|
100
|
+
if (!Number.isFinite(low - pad) || !Number.isFinite(high + pad) || !Number.isFinite((high + pad) - (low - pad))) pad = 0;
|
|
101
|
+
low -= pad; high += pad;
|
|
102
|
+
var plot = deps.plot.Plot(this, {xr: [low, high], yr: [0, 1], xt: [], yt: [], noY: true, m: {l: 48, r: 48, t: 0, b: H - axisY}});
|
|
103
|
+
this.svg = plot.svg; set(this.svg, "width", W); set(this.svg, "height", H); set(this.svg, "aria-hidden", "true");
|
|
104
|
+
var axes = Array.from(this.svg.children).filter(function(e){ return e.getAttribute("class") === "axes"; })[0]; this.svg.removeChild(axes);
|
|
105
|
+
this.axisLayer = base.el("g", {opacity: 0}, this.svg);
|
|
106
|
+
base.el("line", {x1: 48, x2: W - 48, y1: axisY, y2: axisY, stroke: "var(--muted)", "stroke-width": 2}, this.axisLayer);
|
|
107
|
+
var small = W < 160 || bottom - top < Math.max(1, reps - 1) * 6, lastRight = -Infinity;
|
|
108
|
+
ticks(low, high).forEach(function(v){
|
|
109
|
+
var x = plot.x(v), s = num(v), w = base.textWidth(s, fs, {font: self});
|
|
110
|
+
if (x - w / 2 < lastRight + 4 || x - w / 2 < 0 || x + w / 2 > W) small = true;
|
|
111
|
+
lastRight = x + w / 2;
|
|
112
|
+
base.el("line", {x1: x, x2: x, y1: axisY, y2: axisY + 6, stroke: "var(--muted)", "stroke-width": 2}, self.axisLayer);
|
|
113
|
+
self.label(self.axisLayer, x, axisY + line, s, "middle");
|
|
114
|
+
});
|
|
115
|
+
base.el("line", {x1: plot.x(d.mu), x2: plot.x(d.mu), y1: top - 4, y2: axisY, stroke: "var(--muted)", "stroke-width": 2, "stroke-dasharray": "6 4"}, this.axisLayer);
|
|
116
|
+
var muWidth = base.textWidth(muLabel, fs, {font: this}), mx = Math.max(muWidth / 2 + 4, Math.min(W - muWidth / 2 - 4, plot.x(d.mu)));
|
|
117
|
+
this.label(this.axisLayer, mx, fs + 4, muLabel, "middle");
|
|
118
|
+
this.intervals = intervals.map(function(o, i){
|
|
119
|
+
var g = base.el("g", {opacity: 0}, self.svg), y = reps === 1 ? (top + bottom) / 2 : top + i * (bottom - top) / (reps - 1);
|
|
120
|
+
var segment = base.el("line", {x1: plot.x(o.lo), x2: plot.x(o.hi), y1: y, y2: y, stroke: "var(--term)", "stroke-width": 2}, g);
|
|
121
|
+
var dot = base.el("circle", {cx: plot.x(o.mean), cy: y, r: 2.5, fill: "var(--term)"}, g);
|
|
122
|
+
return {g: g, segment: segment, dot: dot};
|
|
123
|
+
});
|
|
124
|
+
this.finalLayer = base.el("g", {opacity: 0}, this.svg);
|
|
125
|
+
this.label(this.finalLayer, W / 2, H - fs * 0.4 - 4, count, "middle");
|
|
126
|
+
if (base.textWidth(count, fs, {font: this}) > W - 8 || muWidth > W - 8 || top > bottom) small = true;
|
|
127
|
+
if (small) ctx.report("size", "[ci] intervals or labels do not fit; enlarge the element or reduce reps");
|
|
128
|
+
}
|
|
129
|
+
render(k, t){
|
|
130
|
+
if (!this.model) return;
|
|
131
|
+
var P = this.props, m = this.model;
|
|
132
|
+
set(this.axisLayer, "opacity", k > 1 ? 1 : k < 1 ? 0 : base.prog(t, 0, P.dur));
|
|
133
|
+
this.intervals.forEach(function(o, i){
|
|
134
|
+
var start = i < 3 ? i * P.dur : 3 * P.dur + (i - 3) * P.fast / (m.reps - 3);
|
|
135
|
+
var dur = i < 3 ? P.dur : P.fast / (m.reps - 3);
|
|
136
|
+
set(o.g, "opacity", k > 2 ? 1 : k < 2 ? 0 : base.prog(t, start, dur));
|
|
137
|
+
var miss = k >= 3 && !m.intervals[i].hit;
|
|
138
|
+
set(o.segment, "style", "stroke:var(--" + (miss ? "emph" : "term") + ")");
|
|
139
|
+
set(o.segment, "stroke-width", miss ? 4 : 2);
|
|
140
|
+
set(o.dot, "style", "fill:var(--" + (miss ? "emph" : "term") + ")");
|
|
141
|
+
});
|
|
142
|
+
set(this.finalLayer, "opacity", k > 3 ? 1 : k < 3 ? 0 : base.prog(t, 0, P.dur));
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
---
|
|
2
|
+
summary: Rotate validation folds
|
|
3
|
+
steps: Rows → mean (k+1)
|
|
4
|
+
props:
|
|
5
|
+
dur: Seconds
|
|
6
|
+
size: Text px
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
JSON:
|
|
10
|
+
- `k`: 2–10; k=5: ≥316px tall (264 without errors)
|
|
11
|
+
- `errors`: k errors (optional).
|
|
12
|
+
- `labels`: k names (optional).
|
|
13
|
+
|
|
14
|
+
<!-- llm: omit -->
|
|
15
|
+
Equal blocks. Row i validates block i: emph tint + outline; training: term tint. Legend explains outline. Blocks ≥40px with enough height: T/V; short rows drop letters first, retaining the legend and outlines. Names once above. Errors/mean: 2 decimals. Final rule and mean below errors; omitted errors leave completed splits at k+1.
|
|
16
|
+
|
|
17
|
+
Wrong lengths, negative errors or invalid k: report, no draw. dur 1s; size 0=26px, ≥24. Bad props default/report.
|
|
18
|
+
|
|
19
|
+
Suggested width: 1184px. Rows shrink from ~46px to 32px before reporting. At default 26px text, minimum height = ceil(99.4+32k+56.1) = **156+32k px** with errors; **104+32k px** without. k=5: 316/264px; k=10: 476/424px. Compact row numbers/errors use monospace.
|
|
20
|
+
|
|
21
|
+
General: f=text px, L=1.45f+4, R=max(32,ceil(1.2f)); minimum=ceil(2L+16+kR+footer+4), footer=L+.4f with errors, else 0. Size reports give k, required/current height and its terms; legend width needed/available; label index and width needed/available.
|
|
22
|
+
|
|
23
|
+
ctx.lang selects ja/en. Font boxes ≥1.43em plus gap. Image role, final summary; preserve author label, hide SVG. No answer preview. Fictional demo.
|
|
24
|
+
<!-- llm: end -->
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// m-folds: the validation block advances through k train/validation splits.
|
|
2
|
+
egatariParts.define({name: "folds", version: "2.0.0", requires: {base: "1.1.0"}, element: "m-folds"}, function(deps){
|
|
3
|
+
"use strict";
|
|
4
|
+
var base = deps.base;
|
|
5
|
+
var TEXT = {
|
|
6
|
+
ja: {train: "学習(学)", validate: "検証(検・枠)", t: "学", v: "検", error: "誤差", mean: "平均", fold: "分割"},
|
|
7
|
+
en: {train: "train (T)", validate: "validate (V, outline)", t: "T", v: "V", error: "error", mean: "mean", fold: "fold"}
|
|
8
|
+
};
|
|
9
|
+
function set(e, a, v){ e.setAttribute(a, String(v)); }
|
|
10
|
+
function fmt(v){ return v.toFixed(2); }
|
|
11
|
+
return class extends base.MotionElement {
|
|
12
|
+
static partName = "folds";
|
|
13
|
+
static preview = "none";
|
|
14
|
+
static shape = {schema: {type: "object", additionalProperties: false, required: ["k"], properties: {
|
|
15
|
+
k: {type: "number"}, errors: {type: "array", items: {type: "number"}}, labels: {type: "array", items: {type: "string"}}
|
|
16
|
+
}}, example: {k: 5, errors: [0.21, 0.18, 0.25, 0.19, 0.22], labels: ["1", "2", "3", "4", "5"]}};
|
|
17
|
+
static props = {
|
|
18
|
+
dur: {type: "number", default: 1, doc: "段階の秒数"},
|
|
19
|
+
size: {type: "number", default: 0, doc: "文字 px(0 は 26)"}
|
|
20
|
+
};
|
|
21
|
+
get steps(){ var k = this.data.k; return Number.isInteger(k) && k >= 2 && k <= 10 ? k + 1 : 0; }
|
|
22
|
+
duration(k){ return k >= 1 && k <= this.steps ? this.props.dur : 0; }
|
|
23
|
+
label(g, x, y, s, anchor){
|
|
24
|
+
var e = base.el("text", {x: x, y: y, "font-size": this.fs, "text-anchor": anchor || "start", fill: "var(--fg)"}, g);
|
|
25
|
+
e.textContent = s; return e;
|
|
26
|
+
}
|
|
27
|
+
mount(ctx){
|
|
28
|
+
var self = this, d = this.data, P = this.props;
|
|
29
|
+
this.T = /^ja\b/i.test(ctx.lang || "") ? TEXT.ja : TEXT.en;
|
|
30
|
+
var T = this.T;
|
|
31
|
+
if (!(Number.isFinite(P.dur) && P.dur > 0)){ ctx.report("attr", "[folds] dur must be positive; using 1"); P.dur = 1; }
|
|
32
|
+
if (!(P.size === 0 || Number.isFinite(P.size) && P.size >= 24)){ ctx.report("attr", "[folds] size must be 0 or at least 24; using 26"); P.size = 0; }
|
|
33
|
+
this.fs = P.size || 26;
|
|
34
|
+
this.style.display = "block"; this.style.position = "relative"; this.style.writingMode = "horizontal-tb"; this.setAttribute("role", "img");
|
|
35
|
+
if (!Number.isInteger(d.k) || d.k < 2 || d.k > 10){ ctx.report("data", "[folds] k must be an integer 2–10"); return; }
|
|
36
|
+
if (d.errors && (d.errors.length !== d.k || d.errors.some(function(v){ return v < 0; }))){ ctx.report("data", "[folds] errors must contain k non-negative numbers"); return; }
|
|
37
|
+
if (d.labels && d.labels.length !== d.k){ ctx.report("data", "[folds] labels must contain k names"); return; }
|
|
38
|
+
var labels = d.labels || Array.from({length: d.k}, function(_, i){ return String(i + 1); });
|
|
39
|
+
var errors = d.errors || null, mean = errors ? errors.reduce(function(s, v){ return s + v / d.k; }, 0) : null;
|
|
40
|
+
this.model = {k: d.k, labels: labels, errors: errors, mean: mean};
|
|
41
|
+
var summary = labels.map(function(s, i){ return T.fold + " " + (i + 1) + ": " + T.validate + " " + s + ", " + T.train + " " + labels.filter(function(_, j){ return j !== i; }).join(", ") + (errors ? "; " + T.error + " " + fmt(errors[i]) : ""); }).join("; ");
|
|
42
|
+
if (!this.getAttribute("aria-label")) this.setAttribute("aria-label", summary + (errors ? "; " + T.mean + " " + fmt(mean) : ""));
|
|
43
|
+
var W = Math.max(1, this.clientWidth), H = Math.max(1, this.clientHeight), fs = this.fs, line = fs * 1.45 + 4;
|
|
44
|
+
var width = function(s){ return base.textWidth(s, fs, {font: self}); };
|
|
45
|
+
var errorWidth = errors ? Math.max(width(T.error), width(T.mean + " " + fmt(mean)), ...errors.map(function(v){ return Math.max(width(fmt(v)), fmt(v).length * fs * 0.62); })) + 8 : 0;
|
|
46
|
+
var left = Math.max(42, width(String(d.k)) + 16), ex = W - errorWidth - 8, right = errors ? ex - 20 : W - 8;
|
|
47
|
+
var bw = (right - left) / d.k, top = 2 * line + 16;
|
|
48
|
+
var footer = errors ? line + fs * 0.4 : 0, minRow = Math.max(32, Math.ceil(fs * 1.2));
|
|
49
|
+
var minHeight = Math.ceil(top + d.k * minRow + footer + 4);
|
|
50
|
+
var rh = Math.max(minRow, Math.min(Math.max(44, line + 4), (H - top - footer - 4) / d.k)), bh = rh - 6;
|
|
51
|
+
var letters = bh >= fs * 1.43;
|
|
52
|
+
this.geometry = {row: rh, minRow: minRow, minHeight: minHeight, letters: letters};
|
|
53
|
+
// Compact row numbers/errors use a monospace font whose box fits 32px rows.
|
|
54
|
+
function rowLabel(g, x, y, s, anchor){
|
|
55
|
+
var e = self.label(g, x, y, s, anchor);
|
|
56
|
+
if (!letters) e.setAttribute("style", "font-family:ui-monospace, Menlo, Consolas, monospace;fill:var(--fg)");
|
|
57
|
+
return e;
|
|
58
|
+
}
|
|
59
|
+
this.svg = base.el("svg", {viewBox: "0 0 " + W + " " + H, width: W, height: H, "aria-hidden": "true"}, this);
|
|
60
|
+
this.header = base.el("g", {opacity: 0}, this.svg);
|
|
61
|
+
function swatch(x, validate, text){
|
|
62
|
+
base.el("rect", {x: x, y: 8, width: 32, height: fs * 1.45, fill: validate ? "var(--emph-tint)" : "var(--term-tint)", stroke: validate ? "var(--emph)" : "none", "stroke-width": 3}, self.header);
|
|
63
|
+
self.label(self.header, x + 42, fs + 4, text);
|
|
64
|
+
}
|
|
65
|
+
var legend2 = 8 + 42 + width(T.train) + 28;
|
|
66
|
+
swatch(8, false, T.train); swatch(legend2, true, T.validate);
|
|
67
|
+
var legendWidth = Math.ceil(legend2 + 42 + width(T.validate) + 8);
|
|
68
|
+
if (legendWidth > W) ctx.report("size", "[folds] legend needs " + legendWidth + " px width; element has " + W + " px");
|
|
69
|
+
if (bw < 8) ctx.report("size", "[folds] " + d.k + " blocks have " + Math.floor(bw) + " px each; allow at least 8 px per block");
|
|
70
|
+
labels.forEach(function(s, i){
|
|
71
|
+
self.label(self.header, left + (i + 0.5) * bw, 2 * line, s, "middle");
|
|
72
|
+
if (width(s) > bw - 8) ctx.report("size", "[folds] label " + (i + 1) + " needs " + Math.ceil(width(s)) + " px; block fits " + Math.max(0, Math.floor(bw - 8)) + " px (shorten the label)");
|
|
73
|
+
});
|
|
74
|
+
if (errors) this.label(this.header, ex, 2 * line, T.error);
|
|
75
|
+
this.rows = labels.map(function(_, i){
|
|
76
|
+
var g = base.el("g", {opacity: 0}, self.svg), y = top + i * rh;
|
|
77
|
+
rowLabel(g, left - 12, y + bh / 2 + fs * 0.31, String(i + 1), "end");
|
|
78
|
+
var blocks = labels.map(function(_, j){
|
|
79
|
+
var valid = i === j, x = left + j * bw + 3;
|
|
80
|
+
var rect = base.el("rect", {x: x, y: y, width: Math.max(1, bw - 6), height: bh, rx: 3, fill: valid ? "var(--emph-tint)" : "var(--term-tint)", stroke: valid ? "var(--emph)" : "none", "stroke-width": 3}, g);
|
|
81
|
+
if (letters && bw - 6 >= 40) self.label(g, x + (bw - 6) / 2, y + bh / 2 + fs * 0.31, valid ? T.v : T.t, "middle");
|
|
82
|
+
return {rect: rect, validation: valid};
|
|
83
|
+
});
|
|
84
|
+
if (errors) rowLabel(g, ex, y + bh / 2 + fs * 0.31, fmt(errors[i]));
|
|
85
|
+
return {g: g, blocks: blocks};
|
|
86
|
+
});
|
|
87
|
+
this.finalLayer = base.el("g", {opacity: 0}, this.svg);
|
|
88
|
+
var bottom = top + d.k * rh, finalY = bottom + line;
|
|
89
|
+
if (errors){
|
|
90
|
+
base.el("line", {x1: ex, x2: W - 8, y1: bottom + 6, y2: bottom + 6, stroke: "var(--muted)", "stroke-width": 2}, this.finalLayer);
|
|
91
|
+
this.label(this.finalLayer, ex, finalY, T.mean + " " + fmt(mean));
|
|
92
|
+
}
|
|
93
|
+
if (H < minHeight) ctx.report("size", "[folds] k = " + d.k + " needs at least " + minHeight + " px height (header " + top.toFixed(1) + " + " + d.k + " × " + minRow + " rows + " + (footer + 4).toFixed(1) + " footer); element has " + H + " px");
|
|
94
|
+
}
|
|
95
|
+
render(k, t){
|
|
96
|
+
if (!this.model) return;
|
|
97
|
+
var dur = this.props.dur, phase = function(step){ return k > step ? 1 : k < step ? 0 : base.prog(t, 0, dur); };
|
|
98
|
+
set(this.header, "opacity", phase(1));
|
|
99
|
+
this.rows.forEach(function(o, i){ set(o.g, "opacity", phase(i + 1)); });
|
|
100
|
+
set(this.finalLayer, "opacity", phase(this.model.k + 1));
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
---
|
|
2
|
+
summary: Outcomes → values → distribution
|
|
3
|
+
steps: Outcomes → arrows → groups → bars (4)
|
|
4
|
+
props:
|
|
5
|
+
dur: Motion s
|
|
6
|
+
stagger: Arrow delay s
|
|
7
|
+
digits: Decimals
|
|
8
|
+
size: Text px
|
|
9
|
+
example: {"outcomes":[{"label":"HH","x":2},{"label":"HT","x":1},{"label":"TH","x":1},{"label":"TT","x":0}],"name":"X","unit":"","fractions":true}
|
|
10
|
+
---
|
|
11
|
+
- `outcomes`: 2–12; `label`, `x`, `p` (default equal; sum ≠ 1 reported)
|
|
12
|
+
- `name` (X), `fractions`
|
|
13
|
+
- `picture`: `dots`, `grapes`, `dice`, `coins`, `balls` (colour names; 11 basic)
|
|
14
|
+
|
|
15
|
+
<!-- llm: omit -->
|
|
16
|
+
For introductory probability. Left: Ω; centre: values; right: distribution. Chips → arrows → shared colours, counts and sums → bars, end labels and total. Final state stands alone; no answer preview.
|
|
17
|
+
|
|
18
|
+
- `outcomes`: 2–12 items; short `label` (≤6 chars advised), finite `x`. Repeated values allowed. Input chip order; numerical axis increases upwards.
|
|
19
|
+
- `p`: optional. All omitted: `1/n`. Mixed: report `data`, missing = 0. Sum outside 1±1e-9 or p outside 0–1: report, retain values. No normalization.
|
|
20
|
+
- `name`: X; `unit`: suffix, default empty. Literal labels.
|
|
21
|
+
- `fractions`: defaults true for equal probabilities. Reduced fractions: denominator ≤10000, error ≤1e-9; otherwise `digits` decimals. Invalid totals stay unrounded.
|
|
22
|
+
- `picture`: draw outcomes: `dots`/`grapes` (x items, 0–15), `dice` (label face 1–6), `coins` (◯ heads, ● tails), `balls` (colour names per outcome `balls` or label characters; 11 basic colours; name shown)
|
|
23
|
+
|
|
24
|
+
Equal probabilities: shown once in the title. Colours cycle series-1..4 by ascending value. Bar scale includes 0–1; extends for invalid data.
|
|
25
|
+
|
|
26
|
+
About 1184×440px for 4 outcomes; rows ≥40px; tight space reports `size`.
|
|
27
|
+
|
|
28
|
+
Fictional grapes: X = berries per bunch.
|
|
29
|
+
|
|
30
|
+
Keep enough height for probabilities below chip labels.
|
|
31
|
+
<!-- llm: end -->
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
// m-mapping: outcomes → numerical values → a probability distribution.
|
|
2
|
+
egatariParts.define({name: "mapping", version: "2.0.0", requires: {base: "1.1.0"}, element: "m-mapping"}, function(deps){
|
|
3
|
+
"use strict";
|
|
4
|
+
var base = deps.base;
|
|
5
|
+
var TEXT = {
|
|
6
|
+
ja: {outcomes: "起こりうる結果", values: function(n){ return n + " の値"; }, count: function(n){ return n + " 通り"; }, total: "確率の合計 ", variable: "確率変数 ", each: function(t){ return "どれも " + t; }},
|
|
7
|
+
en: {outcomes: "Outcomes", values: function(n){ return "Value of " + n; }, count: function(n){ return n + (n === 1 ? " outcome" : " outcomes"); }, total: "Total ", variable: "Random variable ", each: function(t){ return "each " + t; }}
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
// 結果の絵(picture):数えられる形で結果を描く。dots・grapes は x 個の丸(0〜15)、dice は label の目(1〜6)、coins は label の表・裏(H・T)の並び。
|
|
11
|
+
// 描けないときは null(字のカードに戻す)。返す値は {w, h, draw(parent, x0, cy)}
|
|
12
|
+
// 物の色(形式 §8.4):11 の基本色。日本語・英語の名前(1 字の日本語も)から --obj-* へ。テーマになければ同じ色の値で
|
|
13
|
+
var OBJ = {white: "#ffffff", black: "#222222", red: "#d62728", green: "#2ca02c", yellow: "#f2c12e", blue: "#1f77b4", brown: "#8c564b", purple: "#9467bd", pink: "#e377c2", orange: "#ff7f0e", gray: "#7f7f7f"};
|
|
14
|
+
var OBJ_NAME = {"白": "white", "黒": "black", "赤": "red", "緑": "green", "黄": "yellow", "青": "blue", "茶": "brown", "紫": "purple", "桃": "pink", "橙": "orange", "灰": "gray",
|
|
15
|
+
"白色": "white", "黒色": "black", "赤色": "red", "緑色": "green", "黄色": "yellow", "青色": "blue", "茶色": "brown", "紫色": "purple", "桃色": "pink", "橙色": "orange", "灰色": "gray", "ピンク": "pink", "オレンジ": "orange", "グレー": "gray", "grey": "gray"};
|
|
16
|
+
function objColor(name){
|
|
17
|
+
var k = String(name).trim(), en = OBJ[k.toLowerCase()] ? k.toLowerCase() : OBJ_NAME[k] || OBJ_NAME[k.toLowerCase()];
|
|
18
|
+
return en && OBJ[en] ? "var(--obj-" + en + ", " + OBJ[en] + ")" : null;
|
|
19
|
+
}
|
|
20
|
+
function pictureOf(kind, o){
|
|
21
|
+
var n = o.x, R = 7, G = 3;
|
|
22
|
+
var circle = function(par, cx, cy, filled, r){ base.el("circle", {cx: cx.toFixed(1), cy: cy.toFixed(1), r: r || R, fill: filled ? "var(--fg)" : "var(--bg)", stroke: "var(--fg)", "stroke-width": 1.5}, par); };
|
|
23
|
+
if (kind === "dots" || kind === "grapes"){
|
|
24
|
+
if (!(Number.isInteger(n) && n >= 0 && n <= 15)) return null;
|
|
25
|
+
if (kind === "dots"){
|
|
26
|
+
var per = n > 8 ? Math.ceil(n / 2) : n, rows = n > 8 ? 2 : 1, w = Math.max(1, per) * (2 * R + G) - G;
|
|
27
|
+
return {w: w, h: rows * (2 * R + G) - G, draw: function(par, x0, cy){ for (var i = 0; i < n; i++){ var r = Math.floor(i / per), c = i % per; circle(par, x0 + R + c * (2 * R + G), cy - (rows - 1) * (R + G / 2) + r * (2 * R + G), true); } }};
|
|
28
|
+
}
|
|
29
|
+
// ぶどう:逆三角形の房(上の段が k 粒、1 段ごとに 1 粒へる。k は k(k+1)/2 ≥ n の最小)。上に軸
|
|
30
|
+
R = 10; // 房の粒は数えやすい大きさに
|
|
31
|
+
var kk = 1; while (kk * (kk + 1) / 2 < n) kk++;
|
|
32
|
+
var rowsG = [], left = n; for (var k = kk; k >= 1 && left > 0; k--){ var m = Math.min(k, left); rowsG.push(m); left -= m; }
|
|
33
|
+
var wG = Math.max(1, kk) * (2 * R + 1), hG = 10 + rowsG.length * (2 * R - 1);
|
|
34
|
+
return {w: wG, h: hG, draw: function(par, x0, cy){
|
|
35
|
+
var top = cy - hG / 2 + 10, mid = x0 + wG / 2;
|
|
36
|
+
base.el("path", {d: "M" + mid.toFixed(1) + "," + (top - 1).toFixed(1) + "l0,-8m0,4l7,-3", fill: "none", stroke: "var(--fg)", "stroke-width": 2, "stroke-linecap": "round"}, par);
|
|
37
|
+
rowsG.forEach(function(m, r){ for (var c = 0; c < m; c++) circle(par, mid + (c - (m - 1) / 2) * (2 * R + 1), top + R + r * (2 * R - 1), true); });
|
|
38
|
+
}};
|
|
39
|
+
}
|
|
40
|
+
// dice:結果の目(label の数字 1〜6。X とは別でよい:「5 以上なら 1」など)
|
|
41
|
+
if (kind === "dice"){
|
|
42
|
+
n = /^\s*[1-6]\s*$/.test(String(o.label)) ? +o.label : NaN;
|
|
43
|
+
if (!(Number.isInteger(n) && n >= 1 && n <= 6)) return null;
|
|
44
|
+
var S = 40, P = {1: [[0, 0]], 2: [[-1, -1], [1, 1]], 3: [[-1, -1], [0, 0], [1, 1]], 4: [[-1, -1], [1, -1], [-1, 1], [1, 1]], 5: [[-1, -1], [1, -1], [0, 0], [-1, 1], [1, 1]], 6: [[-1, -1], [1, -1], [-1, 0], [1, 0], [-1, 1], [1, 1]]}[n];
|
|
45
|
+
return {w: S, h: S, draw: function(par, x0, cy){
|
|
46
|
+
base.el("rect", {x: x0, y: (cy - S / 2).toFixed(1), width: S, height: S, rx: 6, fill: "var(--bg)", stroke: "var(--fg)", "stroke-width": 2}, par);
|
|
47
|
+
P.forEach(function(q){ circle(par, x0 + S / 2 + q[0] * 11, cy + q[1] * 11, true, 4); });
|
|
48
|
+
}};
|
|
49
|
+
}
|
|
50
|
+
// coins:表 = ◯(白)、裏 = ●(塗り)。見出しに「◯ 表・● 裏」と書く
|
|
51
|
+
if (kind === "coins"){
|
|
52
|
+
var seq = String(o.label).split("").filter(function(ch){ return /\S/.test(ch); });
|
|
53
|
+
if (!seq.length || seq.length > 6 || seq.some(function(ch){ return !/[表裏HT]/.test(ch); })) return null;
|
|
54
|
+
var Rc = 13, wC = seq.length * (2 * Rc + 6) - 6;
|
|
55
|
+
return {w: wC, h: 2 * Rc, draw: function(par, x0, cy){ seq.forEach(function(ch, i){ circle(par, x0 + Rc + i * (2 * Rc + 6), cy, ch === "裏" || ch === "T", Rc); }); }};
|
|
56
|
+
}
|
|
57
|
+
// balls:玉の色は物の色(形式 §8.4 Object colours、--obj-*。テーマになければ同じ色の字の値)。色は中身そのものなので、
|
|
58
|
+
// 名前を玉の横に必ず書く(label。色だけに頼らない)。玉の並びは outcome の balls(色の名前の列、日本語か英語)か、
|
|
59
|
+
// なければ label の 1 字ずつ(赤・青 …)。知らない名前は描かずに知らせる
|
|
60
|
+
if (kind === "balls"){
|
|
61
|
+
// label を 1 字ずつ読むのは、どの字も 1 字の色(白黒赤緑黄青茶紫桃橙灰)で、隣り合う 2 字が合わせた色(黄緑・青緑・赤紫・青紫・赤茶)に
|
|
62
|
+
// ならないときだけ。「黄緑」「水色」「茶色」などは推し量らずに描かず、balls の列で書くよう知らせる
|
|
63
|
+
var names = Array.isArray(o.balls) ? o.balls.map(String) : String(o.label).split("").filter(function(ch){ return /\S/.test(ch); });
|
|
64
|
+
if (!Array.isArray(o.balls)){
|
|
65
|
+
if (names.some(function(ch){ return "白黒赤緑黄青茶紫桃橙灰".indexOf(ch) < 0; })) return null;
|
|
66
|
+
for (var q = 1; q < names.length; q++) if (["黄緑", "青緑", "赤紫", "青紫", "赤茶"].indexOf(names[q - 1] + names[q]) >= 0) return null;
|
|
67
|
+
}
|
|
68
|
+
var cols = names.map(objColor);
|
|
69
|
+
if (!names.length || names.length > 6 || cols.some(function(c){ return !c; })) return null;
|
|
70
|
+
var Rb = 13, wB = names.length * (2 * Rb + 6) - 6;
|
|
71
|
+
return {w: wB, h: 2 * Rb, draw: function(par, x0, cy){ cols.forEach(function(c, i){
|
|
72
|
+
base.el("circle", {cx: (x0 + Rb + i * (2 * Rb + 6)).toFixed(1), cy: cy.toFixed(1), r: Rb, fill: c, stroke: "var(--fg)", "stroke-width": 1.5}, par);
|
|
73
|
+
}); }};
|
|
74
|
+
}
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
function set(e, a, v){ v = String(v); if (e.getAttribute(a) !== v) e.setAttribute(a, v); }
|
|
78
|
+
function gcd(a, b){ while (b){ var r = a % b; a = b; b = r; } return a; }
|
|
79
|
+
// Only use a fraction when it represents the supplied value to the sum tolerance.
|
|
80
|
+
function fraction(v){
|
|
81
|
+
for (var d = 1; d <= 10000; d++){
|
|
82
|
+
var n = Math.round(v * d);
|
|
83
|
+
if (Number.isSafeInteger(n) && Math.abs(n / d - v) <= 1e-9){ var g = gcd(Math.abs(n), d); return d / g === 1 ? String(n / g) : n / g + "/" + d / g; }
|
|
84
|
+
}
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
return class extends base.MotionElement {
|
|
88
|
+
static partName = "mapping";
|
|
89
|
+
static preview = "none";
|
|
90
|
+
static steps = 4;
|
|
91
|
+
static shape = {
|
|
92
|
+
schema: {type: "object", additionalProperties: false, required: ["outcomes"], properties: {
|
|
93
|
+
outcomes: {type: "array", minItems: 2, maxItems: 12, items: {type: "object", additionalProperties: false, required: ["label", "x"], properties: {
|
|
94
|
+
label: {type: "string"}, x: {type: "number"}, p: {type: "number"}, balls: {type: "array", items: {type: "string"}, maxItems: 6}
|
|
95
|
+
}}},
|
|
96
|
+
name: {type: "string"}, unit: {type: "string"}, fractions: {type: "boolean"}, picture: {enum: ["dots", "dice", "coins", "grapes", "balls"]}
|
|
97
|
+
}},
|
|
98
|
+
example: {outcomes: [{label: "表表", x: 2}, {label: "表裏", x: 1}, {label: "裏表", x: 1}, {label: "裏裏", x: 0}], name: "X", unit: "", fractions: true}
|
|
99
|
+
};
|
|
100
|
+
static props = {
|
|
101
|
+
dur: {type: "number", default: 1, doc: "各段階の動きの秒数(矢印は stagger のずれを足す)"},
|
|
102
|
+
stagger: {type: "number", default: 0.15, doc: "段階 2 の矢印ごとの開始のずれ(秒。0 以上)"},
|
|
103
|
+
digits: {type: "number", default: 2, doc: "分数でない確率の小数桁(0〜10 の整数)"},
|
|
104
|
+
size: {type: "number", default: 0, doc: "文字の大きさ(px。0 は既定の 26、指定時は 24 以上)"}
|
|
105
|
+
};
|
|
106
|
+
plan(){
|
|
107
|
+
if (this._plan) return this._plan;
|
|
108
|
+
var d = this.data, given = d.outcomes.filter(function(o){ return o.p !== undefined; }).length;
|
|
109
|
+
var p = {name: d.name === undefined ? "X" : d.name, unit: d.unit || "", notes: [], groups: []};
|
|
110
|
+
p.outcomes = d.outcomes.map(function(o){ return {label: o.label, x: o.x, balls: o.balls, p: given ? (o.p === undefined ? 0 : o.p) : 1 / d.outcomes.length}; });
|
|
111
|
+
p.total = p.outcomes.reduce(function(s, o){ return s + o.p; }, 0);
|
|
112
|
+
p.validTotal = Math.abs(p.total - 1) <= 1e-9;
|
|
113
|
+
if (given && given !== d.outcomes.length) p.notes.push("mixed p: missing probabilities are treated as 0");
|
|
114
|
+
if (!p.validTotal) p.notes.push("probability sum is " + p.total + ", expected 1 (not normalised)");
|
|
115
|
+
if (p.outcomes.some(function(o){ return o.p < 0 || o.p > 1; })) p.notes.push("p must be between 0 and 1; drawing the supplied values");
|
|
116
|
+
p.equal = p.validTotal && p.outcomes.every(function(o){ return Math.abs(o.p - 1 / d.outcomes.length) <= 1e-9; });
|
|
117
|
+
p.fractions = d.fractions === undefined ? p.equal : d.fractions;
|
|
118
|
+
// どれも同じ確率なら、結果ごとには書かず、見出しに「どれも 1/n」と 1 度だけ書く
|
|
119
|
+
p.showP = !p.equal && (given > 0 || p.fractions);
|
|
120
|
+
var values = Array.from(new Set(p.outcomes.map(function(o){ return o.x; }))).sort(function(a, b){ return a - b; });
|
|
121
|
+
p.groups = values.map(function(x, j){
|
|
122
|
+
var members = p.outcomes.filter(function(o){ return o.x === x; });
|
|
123
|
+
var g = {x: x, count: members.length, p: members.reduce(function(s, o){ return s + o.p; }, 0), tone: "series-" + (j % 4 + 1)};
|
|
124
|
+
members.forEach(function(o, i){ o.group = j; o.slot = i; });
|
|
125
|
+
return g;
|
|
126
|
+
});
|
|
127
|
+
return (this._plan = p);
|
|
128
|
+
}
|
|
129
|
+
duration(k){
|
|
130
|
+
var P = this.props, dur = Number.isFinite(P.dur) && P.dur > 0 ? P.dur : 1;
|
|
131
|
+
var stagger = Number.isFinite(P.stagger) && P.stagger >= 0 ? P.stagger : 0.15;
|
|
132
|
+
return k >= 1 && k <= 4 ? dur + (k === 2 ? (this.plan().outcomes.length - 1) * stagger : 0) : 0;
|
|
133
|
+
}
|
|
134
|
+
fmt(v){
|
|
135
|
+
var f = this.plan().fractions && fraction(v);
|
|
136
|
+
return f || v.toFixed(this.props.digits);
|
|
137
|
+
}
|
|
138
|
+
value(x){ return String(x) + this.plan().unit; }
|
|
139
|
+
accessible(){
|
|
140
|
+
var p = this.plan(), self = this, sep = this.T === TEXT.ja ? "、" : ", ";
|
|
141
|
+
return this.T.variable + p.name + ": " + p.outcomes.map(function(o){ return o.label + "→" + self.value(o.x); }).join(sep) + ". " +
|
|
142
|
+
p.groups.map(function(g){ return "P(" + p.name + "=" + self.value(g.x) + ")=" + self.fmt(g.p); }).join(", ") + ". " + this.totalText();
|
|
143
|
+
}
|
|
144
|
+
totalText(){
|
|
145
|
+
var p = this.plan();
|
|
146
|
+
// Do not round an invalid total to 1 just because digits is small.
|
|
147
|
+
return this.T.total + (p.validTotal ? "1" : String(p.total));
|
|
148
|
+
}
|
|
149
|
+
label(g, x, y, s, fs, anchor){
|
|
150
|
+
var e = base.el("text", {x: x, y: y, "font-size": fs || this.fs, "text-anchor": anchor || "start", fill: "var(--fg)"}, g);
|
|
151
|
+
e.textContent = s; return e;
|
|
152
|
+
}
|
|
153
|
+
mount(ctx){
|
|
154
|
+
var P = this.props, self = this, p = this.plan();
|
|
155
|
+
[["dur", 1, function(v){ return Number.isFinite(v) && v > 0; }], ["stagger", 0.15, function(v){ return Number.isFinite(v) && v >= 0; }],
|
|
156
|
+
["digits", 2, function(v){ return Number.isInteger(v) && v >= 0 && v <= 10; }], ["size", 0, function(v){ return v === 0 || Number.isFinite(v) && v >= 24; }]].forEach(function(a){
|
|
157
|
+
if (!a[2](P[a[0]])){ ctx.report("attr", "[mapping] invalid " + a[0] + "; using " + a[1]); P[a[0]] = a[1]; }
|
|
158
|
+
});
|
|
159
|
+
p.notes.forEach(function(n){ ctx.report("data", "[mapping] " + n); });
|
|
160
|
+
var lang = this.closest("[lang]");
|
|
161
|
+
this.T = /^ja\b/i.test((lang && lang.getAttribute("lang")) || ctx.lang || "") ? TEXT.ja : TEXT.en;
|
|
162
|
+
this.fs = P.size || 26;
|
|
163
|
+
this.style.display = "block"; this.style.position = "relative"; this.style.writingMode = "horizontal-tb";
|
|
164
|
+
this.setAttribute("role", "img");
|
|
165
|
+
if (!this.getAttribute("aria-label")) this.setAttribute("aria-label", this.accessible());
|
|
166
|
+
var W = this.clientWidth, H = this.clientHeight, fs = this.fs, small = Math.max(24, fs - 2);
|
|
167
|
+
var width = function(s, f){ return base.textWidth(s, f || fs, {font: self}) + 8; };
|
|
168
|
+
var max = function(a){ return Math.max.apply(null, a.concat([0])); };
|
|
169
|
+
var omega = "Ω " + this.T.outcomes + (p.equal ? "(" + this.T.each(this.fmt(p.outcomes[0].p)) + ")" : "");
|
|
170
|
+
if (p.equal && this.T === TEXT.en) omega = "Ω " + this.T.outcomes + " (" + this.T.each(this.fmt(p.outcomes[0].p)) + ")";
|
|
171
|
+
var kind0 = (this.data || {}).picture;
|
|
172
|
+
if (kind0 === "coins"){
|
|
173
|
+
var each = p.equal ? this.fmt(p.outcomes[0].p) : "";
|
|
174
|
+
omega = this.T === TEXT.en ? "Ω (" + (each ? "each " + each + "; " : "") + "◯ H ● T)" : "Ω(" + (each ? "どれも " + each + "・" : "") + "◯表 ●裏)";
|
|
175
|
+
}
|
|
176
|
+
// 結果の絵(picture)。描けない結果があれば知らせて、その結果は字だけ
|
|
177
|
+
var kind = (this.data || {}).picture, badPic = [];
|
|
178
|
+
var pics = p.outcomes.map(function(o){ if (!kind) return null; var q = pictureOf(kind, o); if (!q) badPic.push(o.label); return q; });
|
|
179
|
+
if (badPic.length) ctx.report("data", "[mapping] picture=\"" + kind + "\" cannot draw " + badPic.join(", ") + " (" + {dots: "x must be an integer 0–15", grapes: "x must be an integer 0–15", dice: "label must be a die face 1–6", coins: "label must be 1–6 of 表/裏 or H/T", balls: "1–6 balls named by colour: single-kanji colours in the label (赤青), or a balls array such as [\"黄緑\", \"青\"] for compound or other names (see --obj-* in spec §8.4)"}[kind] + "); shown as text");
|
|
180
|
+
// coins・balls は丸の中に字を書くので、名前は並べない
|
|
181
|
+
// coins は絵だけ(見出しに ◯表 ●裏)。balls は色の名前を横に書く(色だけに頼らない。形式 §8.4)
|
|
182
|
+
var labelOf = function(o, i){ return kind === "coins" && pics[i] ? "" : o.label; };
|
|
183
|
+
var picW = max(pics.map(function(q){ return q ? q.w : 0; })), picH = max(pics.map(function(q){ return q ? q.h : 0; }));
|
|
184
|
+
var cw = Math.max(width(omega), max(p.outcomes.map(function(o, i){ var lw = labelOf(o, i) ? width(o.label) : 0; return (picW ? picW + (lw ? 12 : 0) : 0) + Math.max(lw, p.showP ? width(self.fmt(o.p), small) : 0); })) + 24);
|
|
185
|
+
var vw = max(p.groups.map(function(g){ return width(self.value(g.x)); }));
|
|
186
|
+
var sw = max(p.groups.map(function(g){ return Math.max(width(self.T.count(g.count), small), width("= " + self.fmt(g.p), small)); }));
|
|
187
|
+
var bw = vw + sw + 44, bx = Math.max(cw + 160, W * 0.43), barX = bx + bw + 30;
|
|
188
|
+
var probW = max(p.groups.map(function(g){ return width(self.fmt(g.p), small); }));
|
|
189
|
+
var negativePad = p.groups.some(function(g){ return g.p < 0; }) ? probW + 12 : 0;
|
|
190
|
+
var barW = Math.max(0, W - barX - negativePad - probW - 24);
|
|
191
|
+
// Source Sans 3's SVG text box extends 1.024em above and 0.4em below the baseline.
|
|
192
|
+
// Reserve those full boxes, a 4px line gap and space inside/outside the chip frame.
|
|
193
|
+
var rowMin = Math.max(p.showP ? (fs + small) * 1.43 + 12 : Math.max(40, fs * 1.43 + 12), picH + 16);
|
|
194
|
+
var boxH = Math.max(40, (fs + small) * 1.43 + 12), top = fs * 1.43 + 16, bottom = H - fs * 1.43 - 8;
|
|
195
|
+
var row = Math.max(rowMin, (bottom - top) / p.outcomes.length);
|
|
196
|
+
if (bottom - top < p.outcomes.length * rowMin || barW < 100 || width(this.T.values(p.name)) > bw || width("P(" + p.name + " = x)") > barW + probW || width(this.totalText()) > W - barX){
|
|
197
|
+
ctx.report("size", "[mapping] labels or outcome rows do not fit; enlarge the element (each row needs at least " + Math.ceil(rowMin) + "px) or shorten labels");
|
|
198
|
+
}
|
|
199
|
+
this.svg = base.el("svg", {viewBox: "0 0 " + W + " " + H, width: W, height: H, "aria-hidden": "true", style: "display:block"}, this);
|
|
200
|
+
this.outcomeLayer = base.el("g", {opacity: 0}, this.svg);
|
|
201
|
+
this.label(this.outcomeLayer, 8, fs + 3, omega);
|
|
202
|
+
this.valueLayer = base.el("g", {opacity: 0}, this.svg);
|
|
203
|
+
this.label(this.valueLayer, bx, fs + 3, this.T.values(p.name));
|
|
204
|
+
var first = top + boxH / 2, last = Math.max(first, bottom - boxH / 2);
|
|
205
|
+
// True numerical axis, increasing upwards. Scale first to avoid overflow of max-min.
|
|
206
|
+
var scale = Math.max(1, max(p.groups.map(function(g){ return Math.abs(g.x); }))), lo = p.groups[0].x / scale, hi = p.groups[p.groups.length - 1].x / scale;
|
|
207
|
+
var ys = p.groups.map(function(g){ return hi === lo ? (first + last) / 2 : base.lerp(last, first, (g.x / scale - lo) / (hi - lo)); });
|
|
208
|
+
if (ys.some(function(y, i){ return i && ys[i - 1] - y < boxH + 8; })) ctx.report("size", "[mapping] value boxes are too close on the numerical axis; enlarge the element or use fewer/less closely spaced values");
|
|
209
|
+
// 値の箱は数の大きさの順に上へ(大きい値が上)。縦の軸の線は描かない(矢印の先と紛れるため)
|
|
210
|
+
this.distribution = base.el("g", {opacity: 0}, this.svg);
|
|
211
|
+
this.label(this.distribution, barX, fs + 3, "P(" + p.name + " = x)");
|
|
212
|
+
this.label(this.distribution, barX, H - fs * 0.4 - 2, this.totalText());
|
|
213
|
+
var minP = Math.min.apply(null, p.groups.map(function(g){ return g.p; }).concat([0]));
|
|
214
|
+
var maxP = Math.max.apply(null, p.groups.map(function(g){ return g.p; }).concat([1]));
|
|
215
|
+
var zero = barX + negativePad + barW * (-minP / (maxP - minP));
|
|
216
|
+
this.chips = []; this.boxes = []; this.arrows = []; this.bars = [];
|
|
217
|
+
p.groups.forEach(function(g, j){
|
|
218
|
+
var y = ys[j], box = base.el("g", {}, self.valueLayer);
|
|
219
|
+
var frame = base.el("rect", {class: "box", x: bx, y: y - boxH / 2, width: bw, height: boxH, rx: 10, fill: "none", stroke: "var(--muted)", "stroke-width": 2}, box);
|
|
220
|
+
self.label(box, bx + 12, y + fs * 0.312, self.value(g.x));
|
|
221
|
+
var summary = base.el("g", {opacity: 0}, box);
|
|
222
|
+
var summaryY = y - boxH / 2 + 6 + small * 1.024;
|
|
223
|
+
self.label(summary, bx + vw + 28, summaryY, self.T.count(g.count), small);
|
|
224
|
+
self.label(summary, bx + vw + 28, summaryY + small * 1.424 + 4, "= " + self.fmt(g.p), small);
|
|
225
|
+
self.boxes.push({frame: frame, summary: summary, tone: g.tone});
|
|
226
|
+
var end = zero + barW * (g.p / (maxP - minP));
|
|
227
|
+
base.el("line", {x1: zero, x2: zero, y1: y - 20, y2: y + 20, stroke: "var(--muted)", "stroke-width": 2}, self.distribution);
|
|
228
|
+
var bar = base.el("rect", {x: zero, y: y - 14, width: 0, height: 28, fill: "var(--" + g.tone + ")"}, self.distribution);
|
|
229
|
+
var label = self.label(self.distribution, end + (g.p < 0 ? -10 : 10), y + small * 0.3, self.fmt(g.p), small, g.p < 0 ? "end" : "start");
|
|
230
|
+
self.bars.push({bar: bar, label: label, zero: zero, end: end});
|
|
231
|
+
});
|
|
232
|
+
p.outcomes.forEach(function(o, i){
|
|
233
|
+
var y = top + row * (i + 0.5), g = p.groups[o.group]; // y:行の真ん中
|
|
234
|
+
var chip = base.el("g", {}, self.outcomeLayer);
|
|
235
|
+
var frame = base.el("rect", {class: "box", x: 8, y: y - rowMin / 2 + 2, width: cw - 8, height: rowMin - 4, rx: 10, fill: "none", stroke: "var(--term)", "stroke-width": 2}, chip);
|
|
236
|
+
var labelY = p.showP ? y - (fs + small) * 0.715 - 2 + fs * 1.024 : y + fs * 0.312, pic = pics[i], lab = labelOf(o, i);
|
|
237
|
+
// 絵があれば左に絵、右に名前(と確率)。なければ真ん中に名前
|
|
238
|
+
var tx = pic ? 20 + picW + 12 : cw / 2 + 4, anchor = pic ? "start" : "middle";
|
|
239
|
+
if (pic) pic.draw(chip, 20 + (picW - pic.w) / 2, y);
|
|
240
|
+
if (lab) self.label(chip, tx, labelY, lab, fs, anchor);
|
|
241
|
+
if (p.showP) self.label(chip, tx, (lab ? labelY + fs * 0.4 + small * 1.024 + 4 : y + small * 0.312), self.fmt(o.p), small, anchor);
|
|
242
|
+
self.chips.push({frame: frame, tone: g.tone});
|
|
243
|
+
var path = base.el("path", {fill: "none", stroke: "var(--muted)", "stroke-width": 2, pathLength: 1, "stroke-dasharray": "1", opacity: 0}, self.svg);
|
|
244
|
+
var head = base.el("path", {fill: "none", stroke: "var(--muted)", "stroke-width": 2, opacity: 0}, self.svg);
|
|
245
|
+
self.arrows.push({path: path, head: head, x: cw + 4, y: y, endX: bx - 5, endY: ys[o.group], spread: (o.slot - (g.count - 1) / 2) * Math.min(6, (boxH - 24) / g.count), tone: g.tone});
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
render(k, t){
|
|
249
|
+
if (!this.svg) return;
|
|
250
|
+
var P = this.props;
|
|
251
|
+
var progress = function(step, offset){ return k > step ? 1 : k < step ? 0 : base.prog(t, offset || 0, P.dur); };
|
|
252
|
+
var bundle = progress(3), bars = progress(4);
|
|
253
|
+
set(this.outcomeLayer, "opacity", progress(1)); set(this.valueLayer, "opacity", progress(2)); set(this.distribution, "opacity", bars);
|
|
254
|
+
this.chips.forEach(function(c){ set(c.frame, "style", "stroke:var(--" + (bundle > 0 ? c.tone : "term") + ")"); });
|
|
255
|
+
this.boxes.forEach(function(b){ set(b.frame, "style", "stroke:var(--" + (bundle > 0 ? b.tone : "muted") + ")"); set(b.summary, "opacity", bundle); });
|
|
256
|
+
this.arrows.forEach(function(a, i){
|
|
257
|
+
var e = progress(2, i * P.stagger), endY = a.endY + a.spread * bundle;
|
|
258
|
+
var bend = base.lerp((a.endX - a.x) * 0.35, 32, bundle);
|
|
259
|
+
set(a.path, "d", "M" + a.x + "," + a.y + "C" + (a.x + bend) + "," + a.y + " " + (a.endX - bend) + "," + endY + " " + a.endX + "," + endY);
|
|
260
|
+
set(a.path, "stroke-dashoffset", 1 - e); set(a.path, "opacity", e > 0 ? 1 : 0);
|
|
261
|
+
set(a.head, "d", "M" + (a.endX - 8) + "," + (endY - 5) + "L" + a.endX + "," + endY + "l-8,5"); set(a.head, "opacity", e === 1 ? 1 : 0);
|
|
262
|
+
[a.path, a.head].forEach(function(n){ set(n, "style", "stroke:var(--" + (bundle > 0 ? a.tone : "muted") + ")"); });
|
|
263
|
+
});
|
|
264
|
+
this.bars.forEach(function(b){ var end = base.lerp(b.zero, b.end, bars); set(b.bar, "x", Math.min(b.zero, end)); set(b.bar, "width", Math.abs(end - b.zero)); set(b.label, "opacity", bars === 1 ? 1 : 0); });
|
|
265
|
+
}
|
|
266
|
+
};
|
|
267
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
---
|
|
2
|
+
summary: Code notes
|
|
3
|
+
steps: Code → notes → output (n+1/+2)
|
|
4
|
+
props:
|
|
5
|
+
dur: Seconds
|
|
6
|
+
size: Text px
|
|
7
|
+
outputPos: Output
|
|
8
|
+
example: {"lang":"python","code":"import pandas as pd\ndf = pd.read_csv(\"sales.csv\")\ndf.groupby(\"store\")[\"sales\"].mean()","steps":[{"lines":[1],"note":"Load pandas"},{"lines":[2],"note":"Read CSV"},{"lines":[3],"note":"Mean"}],"output":"store\nA 120.5\nB 98.0"}
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
`<m-code>` child JSON.
|
|
12
|
+
|
|
13
|
+
- `code`: ≤76 chars/line; height ≈ 132+34×lines+35×(list rows−1), ~40 chars/row; >440px: cut/split.
|
|
14
|
+
- `lang`: tag; `steps[{lines,note}]`: 1-based range, note.
|
|
15
|
+
- `output`: ≤8 lines.
|
|
16
|
+
|
|
17
|
+
Narration: explain each line; never read code aloud.
|
|
18
|
+
|
|
19
|
+
<!-- llm: omit -->
|
|
20
|
+
Displays supplied text; never runs code. Code first, then highlights and notes. Beside: natural code width ≤55% and every note fits; earlier notes stay muted, all shown at completion. Otherwise band: only the current note, prefixed “lines 3–4”. Final/print: numbered compact list, no code highlight. If the list needs more height, report and keep the last numbered note. Deterministic seeking. Preview uses --preview-opacity; the final list previews before notes begin.
|
|
21
|
+
|
|
22
|
+
dur .6s; size 0=24px code/26px notes, otherwise ≥24. outputPos auto/below/right. Beside auto: right for ≤8 lines when columns fit. Band auto: right when natural code width ≤70% and output fits, else below the band. Explicit right/below retained. Bad props report/default.
|
|
23
|
+
|
|
24
|
+
Python/R/shell # and JS // outside strings are muted. Tabs: four spaces. Monospace width .6em, wide characters 1em. Full-width band mode at 1184px/24px fits 77 ASCII characters with single-digit line numbers, 76 with two digits. Beside notes/right output reduce this. Beside notes wrap/stack with connectors; each band note is one truncated row.
|
|
25
|
+
|
|
26
|
+
Default height, one band row, right/no output: header 50 + n × row 34 + padding 16 + gap 8 + band 58 = **132 + 34n px**. General: r=ceil(1.4×size), header=r+16, b=ceil(1.45×note px+4)+16; H=header+n×r+16+8+b. No notes: omit 8+b. Below output adds 16+header+output rows×r+16. Final list (24px, 35px rows) packs “3–4: note” items; each row beyond one adds 35px. Auto output that would go below sits beside the band when its natural width ≤45%; the zone is then max(band, output). Every “give the element about H px” uses this formula; above 440px (body under a heading) reports give lines that fit at 440px and say fewer lines/shorter notes/split instead. If only the list misses, all code stays and the final state keeps the last note.
|
|
27
|
+
|
|
28
|
+
| Height | Code lines (one band row) |
|
|
29
|
+
|---|---|
|
|
30
|
+
| 380px | 7 (needs 370px) |
|
|
31
|
+
| 440px | 9 (438px) |
|
|
32
|
+
| 540px | 12 (540px) |
|
|
33
|
+
| 680px | 16 (676px) |
|
|
34
|
+
|
|
35
|
+
A 59-character line exceeds 70% width: auto output goes below. Seven lines + band + one output row need 486px; two output rows need 520px. Without output, 380px fits. Separate layout reports give line count/capacity/needed height, longest line/character capacity, or note index/width/capacity/target length. Oversize text truncates with …; tiny elements retain clipped content and numeric size reports. Code ≤16 lines, output ≤8. Invalid ranges report; no highlight/connector, notes/steps preserved.
|
|
36
|
+
|
|
37
|
+
ctx.lang ja/en. Figure role; author label preserved; SVG hidden. Full source/output in non-focusable sr-only pre. All notes remain in the accessible summary. Fictional sales data.
|
|
38
|
+
<!-- llm: end -->
|