All files / web/src/components/toys/coordinate-plane/wordProblems grammar.ts

99.22% Statements 386/389
97.01% Branches 65/67
100% Functions 12/12
99.22% Lines 386/389

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 3901x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2329x 2329x 2329x 1x 1x 1x 893x 893x 893x 893x 1x 893x 893x 893x 893x 893x 625x 625x 625x 625x 625x 191x 191x 191x 191x 191x 191x 191x 625x 215x 215x 215x 215x 215x 215x 215x 434x 219x 219x 219x 219x 219x 219x 219x 219x 625x 268x 268x 268x 268x 893x 76x 76x 76x 76x 76x 76x 76x 893x 127x 127x 127x 127x 127x 127x 127x 192x 65x 65x 65x 65x 65x 65x 65x 65x 893x 1x 718x 718x 718x 718x 718x 718x 521x 521x 521x 203x 203x 203x 203x 203x 203x 203x 521x 178x 178x 178x 178x 178x 178x 178x 318x 140x 140x 140x 140x 140x 140x 140x 521x 197x 197x 197x 197x 718x 57x 57x 44x 44x 44x 44x 44x 44x 57x 13x 13x 13x 13x 13x 13x 718x 50x 50x 50x 50x 50x 50x 50x 140x 90x 90x 90x 90x 90x 90x 90x 718x 1x 718x 718x 718x 718x 718x 718x 521x 521x 521x 306x 306x 306x 306x 306x 306x 306x 306x 306x 306x 521x 215x 215x 215x 215x 215x 215x 215x 521x 197x 197x 197x 197x 718x 76x 76x 76x 76x 76x 76x 718x 121x 121x 121x 121x 121x 121x 718x 1x 718x 718x 718x 718x 521x 521x 521x       197x 197x 197x 197x 197x 718x 88x 88x 88x 88x 88x 88x 718x 109x 109x 718x 1x 1x 119x 119x 119x 119x 119x 119x 119x 119x 119x 119x 119x 119x 119x 119x 119x 119x 119x 1x 1x 175x 175x 175x 175x 175x 175x 1x 175x 175x 175x 175x 175x 104x 104x 104x 37x 37x 37x 37x 37x 37x 95x 67x 67x 67x 67x 67x 67x 67x 104x 71x 71x 71x 71x 71x 175x 23x 23x 23x 23x 23x 175x 48x 48x 48x 48x 48x 48x 175x 1x 1x 718x 718x 718x 718x 718x 718x 718x 718x 718x 718x 300x 418x 718x 718x 718x 1x 1x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1049x 1049x 1049x 1049x 1049x 1049x 1049x 1049x 1049x  
import type {
  SemanticFrame,
  SubjectEntry,
  GeneratedNumbers,
  DifficultyLevel,
  AnnotatedSpan,
} from './types'
import {
  formatWithUnit,
  pluralize,
  conjugate3p,
  conjugateBase,
  conjugateFor,
  capitalize,
  midSentence,
} from './inflect'
import type { SeededRandom } from '../../../../lib/SeededRandom'
 
/** A production is a function that returns an array of annotated spans */
type Production = (ctx: GrammarContext) => AnnotatedSpan[]
 
interface GrammarContext {
  frame: SemanticFrame
  nums: GeneratedNumbers
  difficulty: DifficultyLevel
  rng: SeededRandom
}
 
// ── Helpers ──────────────────────────────────────────────────────
 
function pickSubject(ctx: GrammarContext): SubjectEntry {
  return ctx.rng.pick(ctx.frame.subjects)
}
 
// ── Individual productions ────────────────────────────────────────
 
function setupBlock(ctx: GrammarContext): AnnotatedSpan[] {
  const phrase = ctx.rng.pick(ctx.frame.setupPhrases)
  return [{ text: phrase, tag: 'context' }]
}
 
function rateSentence(ctx: GrammarContext): AnnotatedSpan[] {
  const { frame, nums, rng } = ctx
  const mFormatted = formatWithUnit(nums.m, frame.yUnit, frame.yUnitPosition)
 
  if (frame.xRole === 'elapsed') {
    // Time-based: subject is the actor, not the x-unit
    const subject = pickSubject(ctx)
    const variant = rng.nextInt(0, 2)
 
    if (variant === 0) {
      // "The car travels 42 miles each hour."
      return [
        { text: `${subject.phrase} ` },
        { text: `${conjugateFor(frame.rateVerb, subject)} ` },
        { text: mFormatted, tag: 'slope', value: nums.m },
        { text: ` each ${frame.xNoun.singular}.` },
      ]
    } else if (variant === 1) {
      // "Every week, the plant grows 3 inches."
      return [
        { text: `Every ${frame.xNoun.singular}, ${midSentence(subject.phrase)} ` },
        { text: `${conjugateFor(frame.rateVerb, subject)} ` },
        { text: mFormatted, tag: 'slope', value: nums.m },
        { text: '.' },
      ]
    } else {
      // "The rate is 42 miles per hour."
      const rateLabel = frame.category === 'money' ? 'The rate is' : 'The rate is'
      return [
        { text: `${rateLabel} ` },
        { text: mFormatted, tag: 'slope', value: nums.m },
        { text: ` per ${frame.xNoun.singular}.` },
      ]
    }
  }
 
  // Acquired: x-unit is the subject
  const variant = rng.nextInt(0, 2)
 
  if (variant === 0) {
    // "Each slice costs $3."
    return [
      { text: `Each ${frame.xNoun.singular} ` },
      { text: `${conjugate3p(frame.rateVerb)} ` },
      { text: mFormatted, tag: 'slope', value: nums.m },
      { text: '.' },
    ]
  } else if (variant === 1) {
    // "Slices cost $3 each."
    return [
      { text: `${capitalize(frame.xNoun.plural)} ` },
      { text: `${conjugateBase(frame.rateVerb)} ` },
      { text: mFormatted, tag: 'slope', value: nums.m },
      { text: ' each.' },
    ]
  } else {
    // "The price is $3 per slice."
    const rateLabel = frame.category === 'money' ? 'The price is' : 'The rate is'
    return [
      { text: `${rateLabel} ` },
      { text: mFormatted, tag: 'slope', value: nums.m },
      { text: ` per ${frame.xNoun.singular}.` },
    ]
  }
}
 
function baseSentence(ctx: GrammarContext): AnnotatedSpan[] {
  const { frame, nums, rng } = ctx
  const bFormatted = formatWithUnit(nums.b, frame.yUnit, frame.yUnitPosition)
  const subject = pickSubject(ctx)
 
  if (frame.xRole === 'elapsed') {
    const variant = rng.nextInt(0, 2)
 
    if (variant === 0) {
      // "The car started at 38 miles." / "She started with $50."
      const prep = frame.yUnitPosition === 'prefix' ? 'with' : 'at'
      return [
        { text: `${subject.phrase} started ${prep} ` },
        { text: bFormatted, tag: 'intercept', value: nums.b },
        { text: '.' },
      ]
    } else if (variant === 1) {
      // "The starting total is $50." / "The starting distance is 38 miles."
      const noun = frame.category === 'money' ? 'total' : frame.yNoun.singular + ' count'
      return [
        { text: `The starting ${noun} is ` },
        { text: bFormatted, tag: 'intercept', value: nums.b },
        { text: '.' },
      ]
    } else {
      // "At the start, they had 38 miles."
      return [
        { text: `At the start, ${midSentence(subject.phrase)} had ` },
        { text: bFormatted, tag: 'intercept', value: nums.b },
        { text: '.' },
      ]
    }
  }
 
  // Acquired
  const variant = rng.nextInt(0, 2)
 
  if (variant === 0) {
    // "There is a $5 base fee." / "There is a starting amount of 3 cups."
    if (frame.yUnitPosition === 'prefix') {
      const feeWord = frame.category === 'money' ? 'base fee' : 'starting amount'
      return [
        { text: `There is a ` },
        { text: bFormatted, tag: 'intercept', value: nums.b },
        { text: ` ${feeWord}.` },
      ]
    } else {
      return [
        { text: `There is a starting amount of ` },
        { text: bFormatted, tag: 'intercept', value: nums.b },
        { text: '.' },
      ]
    }
  } else if (variant === 1) {
    // "The starting total is $5."
    const noun = frame.category === 'money' ? 'total' : frame.yNoun.singular + ' count'
    return [
      { text: `The starting ${noun} is ` },
      { text: bFormatted, tag: 'intercept', value: nums.b },
      { text: '.' },
    ]
  } else {
    // "Sonia already has $5."
    return [
      { text: `${subject.phrase} already has ` },
      { text: bFormatted, tag: 'intercept', value: nums.b },
      { text: '.' },
    ]
  }
}
 
function goalSentence(ctx: GrammarContext): AnnotatedSpan[] {
  const { frame, nums, rng } = ctx
  const tFormatted = formatWithUnit(nums.yTarget, frame.yUnit, frame.yUnitPosition)
  const subject = pickSubject(ctx)
 
  if (frame.xRole === 'elapsed') {
    const variant = rng.nextInt(0, 1)
 
    if (variant === 0) {
      // "The car needs to reach 290 miles."
      const verb = conjugateFor(
        { base: 'need', thirdPerson: 'needs', pastTense: 'needed', gerund: 'needing' },
        subject
      )
      return [
        { text: `${subject.phrase} ${verb} to reach ` },
        { text: tFormatted, tag: 'target', value: nums.yTarget },
        { text: '.' },
      ]
    } else {
      // "The goal is 290 miles."
      return [
        { text: 'The goal is ' },
        { text: tFormatted, tag: 'target', value: nums.yTarget },
        { text: '.' },
      ]
    }
  }
 
  // Acquired
  const variant = rng.nextInt(0, 1)
 
  if (variant === 0) {
    const budgetWord = frame.category === 'money' ? 'a budget of' : 'a goal of'
    return [
      { text: `${subject.phrase} has ${budgetWord} ` },
      { text: tFormatted, tag: 'target', value: nums.yTarget },
      { text: '.' },
    ]
  } else {
    return [
      { text: 'The total is ' },
      { text: tFormatted, tag: 'target', value: nums.yTarget },
      { text: '.' },
    ]
  }
}
 
function questionSentence(ctx: GrammarContext): AnnotatedSpan[] {
  const { frame, rng } = ctx
 
  if (frame.xRole === 'elapsed') {
    if (frame.solveForXQuestions && frame.solveForXQuestions.length > 0) {
      return [{ text: rng.pick(frame.solveForXQuestions), tag: 'question' }]
    }
    // Generic fallback
    return [{ text: `How many ${frame.xNoun.plural} will it take?`, tag: 'question' }]
  }
 
  // Acquired
  const subject = pickSubject(ctx)
  const variant = rng.nextInt(0, 1)
 
  if (variant === 0) {
    return [
      {
        text: `How many ${frame.xNoun.plural} can ${midSentence(subject.phrase)} get?`,
        tag: 'question',
      },
    ]
  } else {
    return [{ text: `How many ${frame.xNoun.plural} is that?`, tag: 'question' }]
  }
}
 
// Level 1: just identify a constant
function level1Sentence(ctx: GrammarContext): AnnotatedSpan[] {
  const { frame, nums, rng } = ctx
  const bFormatted = formatWithUnit(nums.b, frame.yUnit, frame.yUnitPosition)
  const setup = rng.pick(frame.setupPhrases)
  const nounPl = frame.yNoun.plural
 
  return [
    { text: setup, tag: 'context' },
    { text: ' ' },
    { text: `The number of ${nounPl} is always ` },
    { text: bFormatted, tag: 'intercept', value: nums.b },
    { text: ', no matter how many ' },
    { text: frame.xNoun.plural, tag: 'x_unit' },
    { text: '. ' },
    { text: `What line shows ${bFormatted}?`, tag: 'question' },
  ]
}
 
// Level 2: y = mx (proportional)
function level2Sentence(ctx: GrammarContext): AnnotatedSpan[] {
  const setup = setupBlock(ctx)
  const rate = rateSentence(ctx)
  const question = questionForSolveY(ctx)
  return [...setup, { text: ' ' }, ...rate, { text: ' ' }, ...question]
}
 
function questionForSolveY(ctx: GrammarContext): AnnotatedSpan[] {
  const { frame, nums, rng } = ctx
  const xFormatted = `${nums.xAnswer} ${pluralize(frame.xNoun, nums.xAnswer)}`
 
  if (frame.xRole === 'elapsed') {
    const variant = rng.nextInt(0, 1)
 
    if (variant === 0) {
      // "After 3 hours, what is the total?"
      return [
        { text: `After ` },
        { text: xFormatted, tag: 'answer', value: nums.xAnswer },
        { text: `, what is the total?`, tag: 'question' },
      ]
    } else {
      // "What is the total after 3 hours?"
      return [
        { text: `What is the total after ` },
        { text: xFormatted, tag: 'answer', value: nums.xAnswer },
        { text: `?`, tag: 'question' },
      ]
    }
  }
 
  // Acquired
  const subject = pickSubject(ctx)
  const variant = rng.nextInt(0, 1)
 
  if (variant === 0) {
    return [
      { text: `If ${midSentence(subject.phrase)} gets ` },
      { text: xFormatted, tag: 'answer', value: nums.xAnswer },
      { text: `, what is the total?`, tag: 'question' },
    ]
  } else {
    return [
      { text: `How much for ` },
      { text: xFormatted, tag: 'answer', value: nums.xAnswer },
      { text: `?`, tag: 'question' },
    ]
  }
}
 
// Level 3: y = mx + b, solve for x
function level3Sentence(ctx: GrammarContext): AnnotatedSpan[] {
  const setup = setupBlock(ctx)
  const rate = rateSentence(ctx)
  const base = baseSentence(ctx)
  const goal = goalSentence(ctx)
  const question = questionSentence(ctx)
 
  // Randomize order of rate and base sentences
  const orderRateFirst = ctx.rng.chance(0.5)
  const middle = orderRateFirst
    ? [...rate, { text: ' ' } as AnnotatedSpan, ...base]
    : [...base, { text: ' ' } as AnnotatedSpan, ...rate]
 
  return [...setup, { text: ' ' }, ...middle, { text: ' ' }, ...goal, { text: ' ' }, ...question]
}
 
// Level 4: two points → derive equation
function level4Sentence(ctx: GrammarContext): AnnotatedSpan[] {
  const { frame, nums, rng } = ctx
  const setup = rng.pick(frame.setupPhrases)
 
  const p1 = nums.point1!
  const p2 = nums.point2!
  const xUnit = frame.xNoun.plural
 
  return [
    { text: setup, tag: 'context' },
    { text: ' ' },
    {
      text: `After ${p1.x} ${xUnit}, there were ${formatWithUnit(p1.y, frame.yUnit, frame.yUnitPosition)}`,
      tag: 'point1',
    },
    { text: '. ' },
    {
      text: `After ${p2.x} ${xUnit}, there were ${formatWithUnit(p2.y, frame.yUnit, frame.yUnitPosition)}`,
      tag: 'point2',
    },
    { text: '. ' },
    { text: 'What equation describes this pattern?', tag: 'question' },
  ]
}
 
// ── Production registry per difficulty ────────────────────────────
 
const PRODUCTIONS: Record<DifficultyLevel, Production> = {
  1: level1Sentence,
  2: level2Sentence,
  3: level3Sentence,
  4: level4Sentence,
  5: level3Sentence, // level 5 reuses level 3 structure (point-slope is a ruler task, not a text task)
}
 
/**
 * Expand the grammar for a given context, producing annotated spans.
 */
export function expandGrammar(
  frame: SemanticFrame,
  nums: GeneratedNumbers,
  difficulty: DifficultyLevel,
  rng: SeededRandom
): AnnotatedSpan[] {
  const ctx: GrammarContext = { frame, nums, difficulty, rng }
  const production = PRODUCTIONS[difficulty]
  return production(ctx)
}