Files
Spanish/Conjuga/SharedModels/Sources/SharedModels/TextbookExerciseAttempt.swift
Trey T 63dfc5e41a Add textbook reader, exercise grading, stem-change toggle, extraction pipeline
Major changes:
- Textbook UI: chapter list, reader, and interactive exercise view (keyboard
  + Apple Pencil) surfaced under the Course tab. 30 chapters, 251 exercises.
- Stem-change conjugation toggle on Week 4 flashcard decks (E-IE, E-I, O-UE).
  Uses existing VerbForm + IrregularSpan data to render highlighted present
  tense conjugations inline.
- Deterministic on-device answer grader with partial credit (correct / close
  for accent-stripped or single-char-typo / wrong). 11 unit tests cover it.
- SharedModels: TextbookChapter (local), TextbookExerciseAttempt (cloud-
  synced), AnswerGrader helpers. Bumped schema.
- DataLoader: textbook seeder (version 8) + refresh helpers that preserve
  LanGo course decks when textbook data is re-seeded.
- Local extraction pipeline in Conjuga/Scripts/textbook/ — XHTML chapter
  parser, answer-key parser, macOS Vision image OCR + PDF page OCR, merger,
  NSSpellChecker validator, language-aware auto-fixer, and repair pass that
  re-pairs quarantined vocab rows using bounding-box coordinates.
- UI test target (ConjugaUITests) with three tests: end-to-end textbook
  flow, all-chapters screenshot audit, and stem-change toggle verification.

Generated textbook content (textbook_data.json, textbook_vocab.json) and
third-party source files are gitignored — re-run Scripts/textbook/run_pipeline.sh
locally to regenerate.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 15:12:55 -05:00

84 lines
2.6 KiB
Swift

import Foundation
import SwiftData
/// Per-prompt grading state recorded after the user submits an exercise.
public enum TextbookGrade: Int, Codable, Sendable {
case wrong = 0
case close = 1
case correct = 2
}
/// User's attempt for one exercise. Stored in the cloud container so progress
/// syncs across devices.
@Model
public final class TextbookExerciseAttempt {
/// Deterministic id: "<courseName>|<exerciseId>". CloudKit-synced models can't
/// use @Attribute(.unique); code that writes attempts must fetch-or-create.
public var id: String = ""
public var courseName: String = ""
public var chapterNumber: Int = 0
public var exerciseId: String = ""
/// JSON-encoded per-prompt state array.
/// Each entry: { "number": Int, "userText": String, "grade": Int }
public var stateJSON: Data = Data()
public var lastAttemptAt: Date = Date()
public var correctCount: Int = 0
public var closeCount: Int = 0
public var wrongCount: Int = 0
public var totalCount: Int = 0
public init(
id: String,
courseName: String,
chapterNumber: Int,
exerciseId: String,
stateJSON: Data = Data(),
lastAttemptAt: Date = Date(),
correctCount: Int = 0,
closeCount: Int = 0,
wrongCount: Int = 0,
totalCount: Int = 0
) {
self.id = id
self.courseName = courseName
self.chapterNumber = chapterNumber
self.exerciseId = exerciseId
self.stateJSON = stateJSON
self.lastAttemptAt = lastAttemptAt
self.correctCount = correctCount
self.closeCount = closeCount
self.wrongCount = wrongCount
self.totalCount = totalCount
}
public func promptStates() -> [TextbookPromptState] {
(try? JSONDecoder().decode([TextbookPromptState].self, from: stateJSON)) ?? []
}
public func setPromptStates(_ states: [TextbookPromptState]) {
stateJSON = (try? JSONEncoder().encode(states)) ?? Data()
correctCount = states.filter { $0.grade == .correct }.count
closeCount = states.filter { $0.grade == .close }.count
wrongCount = states.filter { $0.grade == .wrong }.count
totalCount = states.count
}
public static func attemptId(courseName: String, exerciseId: String) -> String {
"\(courseName)|\(exerciseId)"
}
}
public struct TextbookPromptState: Codable, Sendable {
public var number: Int
public var userText: String
public var grade: TextbookGrade
public init(number: Int, userText: String, grade: TextbookGrade) {
self.number = number
self.userText = userText
self.grade = grade
}
}