mirror of
https://github.com/bitbaum/heidi.git
synced 2026-12-04 11:57:08 +01:00
Claude-Session: https://claude.ai/code/session_019MDtmvb6xi489TfZMg7n9T Co-authored-by: Claude <noreply@anthropic.com>
146 lines
6.1 KiB
TypeScript
146 lines
6.1 KiB
TypeScript
import type { EntryMode } from "../../course/registry.ts";
|
|
import type { SituationPack } from "../../situations/pack.ts";
|
|
import type { DeclaredLevel, VarietyPack } from "../../variety/pack.ts";
|
|
import { closenessOf, levelOf, sentenceLevel, yardstickOf, type Level } from "../vocabulary/distance.ts";
|
|
import type { PracticeItem } from "./types.ts";
|
|
|
|
/**
|
|
* Every generated item's LEVEL, and where it comes from.
|
|
*
|
|
* A level is one of three steps a learner sees. WHAT decides the step depends
|
|
* on how the course is entered (`Course.entry`), so it is a SOURCE chosen by
|
|
* the course rather than one rule written into the generator:
|
|
*
|
|
* `sibling` measured: how far the item sits from the language the learner
|
|
* already has. `distanceLevels`.
|
|
* `zero` declared: the level somebody wrote on the word, the line or
|
|
* the topic. `declaredLevels`. Nothing can be measured against
|
|
* a language the learner does not have.
|
|
*
|
|
* A source answers for one item at a time and may have no answer; an item
|
|
* without one simply carries no level, as before.
|
|
*
|
|
* Server-side, once, so the browser never needs the pack to know a level.
|
|
*/
|
|
export type LevelSource = (item: PracticeItem) => Level | undefined;
|
|
|
|
/** The source a course's entry mode calls for. */
|
|
export function levelSourceFor(
|
|
entry: EntryMode,
|
|
pack: VarietyPack,
|
|
situations: readonly SituationPack[],
|
|
): LevelSource {
|
|
return entry === "sibling" ? distanceLevels(pack, situations) : declaredLevels(pack, situations);
|
|
}
|
|
|
|
/**
|
|
* The NAMES of the three levels, for the course the learner is in.
|
|
*
|
|
* A name says what the level measures, so it follows the same choice as the
|
|
* level itself: «like German / by a sound rule / quite different» is true of
|
|
* a `sibling` course and says nothing in a course entered from zero, whose
|
|
* steps are CEFR bands. Both sets are in the dictionaries; this picks one.
|
|
*/
|
|
export function levelNames<T>(names: { levels: T; levelsFromZero: T }, entry: EntryMode): T {
|
|
return entry === "sibling" ? names.levels : names.levelsFromZero;
|
|
}
|
|
|
|
export function withLevels(items: readonly PracticeItem[], source: LevelSource): PracticeItem[] {
|
|
return items.map((item) => {
|
|
const level = source(item);
|
|
return level ? ({ ...item, source: { ...item.source, level } } as PracticeItem) : item;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* DISTANCE FROM THE SIBLING (`vocabulary/distance.ts`), from the most
|
|
* specific evidence the item has:
|
|
*
|
|
* a word the word's own closeness (`Chind` 2, `nöd` 3, `Estrich` 3)
|
|
* a scene line the share of the line's words the sibling does not give away
|
|
* a sound rule 2: by definition free once the rule is known
|
|
* a grammar topic the sentence it shows, when it shows one; otherwise the
|
|
* topic's own band — "blocks" understanding (3) or only
|
|
* "marks" a speaker (2). A topic never ranks 1: it exists
|
|
* because it differs from the sibling.
|
|
*/
|
|
export function distanceLevels(pack: VarietyPack, situations: readonly SituationPack[]): LevelSource {
|
|
const rules = yardstickOf(pack);
|
|
const words = new Map((pack.vocabulary ?? []).map((w) => [w.target, levelOf(closenessOf(w, rules))]));
|
|
const topics = new Map((pack.grammar ?? []).map((t) => [t.id, (t.band === "blocks" ? 3 : 2) as Level]));
|
|
const scenes = new Map(situations.flatMap((p) => p.situations).map((s) => [s.id, s]));
|
|
|
|
return (item) => {
|
|
const source = item.source;
|
|
switch (source.kind) {
|
|
case "word":
|
|
return words.get(source.word);
|
|
case "situation": {
|
|
const phrase = source.line === undefined ? undefined : scenes.get(source.scene)?.phrases[source.line];
|
|
if (phrase) return sentenceLevel(phrase.target, phrase.bridge, rules);
|
|
// A passage is as hard as its hardest line.
|
|
if (item.kind === "gaptext") {
|
|
return Math.max(...item.lines.map((line) => sentenceLevel(line.prompt, line.bridge, rules))) as Level;
|
|
}
|
|
// A time or a price: the words around the number, against what they mean.
|
|
if ("listen" in item && item.listen && item.said) return sentenceLevel(item.said, item.listen.means, rules);
|
|
return undefined;
|
|
}
|
|
case "rule":
|
|
return 2;
|
|
case "grammar": {
|
|
const pair = sentencesOf(item);
|
|
if (pair) return Math.max(2, sentenceLevel(pair[0], pair[1], rules)) as Level;
|
|
return topics.get(source.topic);
|
|
}
|
|
case "saved":
|
|
return undefined;
|
|
}
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Which of the three steps a declared band is: the beginner's band, the one
|
|
* after it, and everything from independent use upward.
|
|
*/
|
|
export const STEP_OF: Readonly<Record<DeclaredLevel, Level>> = { A1: 1, A2: 2, B1: 3, B2: 3, C1: 3, C2: 3 };
|
|
|
|
/**
|
|
* THE LEVEL SOMEBODY DECLARED, on the entry the item came from: the word, the
|
|
* scene line, or the grammar topic. An item whose entry declares none has no
|
|
* level, and nothing is inferred to fill the gap: a guessed level is a claim
|
|
* about a language nobody measured.
|
|
*/
|
|
export function declaredLevels(pack: VarietyPack, situations: readonly SituationPack[]): LevelSource {
|
|
const step = (level: DeclaredLevel | undefined) => (level ? STEP_OF[level] : undefined);
|
|
const words = new Map((pack.vocabulary ?? []).map((w) => [w.target, step(w.level)]));
|
|
const topics = new Map((pack.grammar ?? []).map((t) => [t.id, step(t.level)]));
|
|
const scenes = new Map(situations.flatMap((p) => p.situations).map((s) => [s.id, s]));
|
|
|
|
return (item) => {
|
|
const source = item.source;
|
|
switch (source.kind) {
|
|
case "word":
|
|
return words.get(source.word);
|
|
case "situation":
|
|
return source.line === undefined ? undefined : step(scenes.get(source.scene)?.phrases[source.line]?.level);
|
|
case "grammar":
|
|
return topics.get(source.topic);
|
|
case "rule":
|
|
case "saved":
|
|
return undefined;
|
|
}
|
|
};
|
|
}
|
|
|
|
/** The target sentence and its sibling's, where the item carries both. */
|
|
function sentencesOf(item: PracticeItem): [string, string] | null {
|
|
switch (item.kind) {
|
|
case "translate":
|
|
case "build":
|
|
return [item.answer, item.prompt];
|
|
default:
|
|
return null;
|
|
}
|
|
}
|