fix(cefr): resolve authentic context sentence per term and repair legacy SRS cards

- Add resolve_context_sentence_for_term to replace placeholder blanks and lookup authentic sentences from cefr_questions or books
- Fix dual mistake ingestion in process_cefr_answer to assign distinct context to target and chosen distractor
- Provide authentic context_sentence in options_breakdown and pass it via data-context in cefr.js
- Add repair_mismatched_cefr_contexts in database.py to automatically repair cards with placeholder blanks
- Bump Service Worker cache to notes-v5.9
- Add test_cefr_context_resolution_and_repair covering resolution, dual ingestion, manual add, and DB repair
This commit is contained in:
Antigravity committed 2026-10-06 22:17:51 +03:00
1 parent 82b7a654ca
commit 3098d3016d
5 files changed
+230 -10

No files matched your search

+37
View File
@@ -379,6 +379,43 @@ def _migrate(conn: sqlite3.Connection):
conn.execute("CREATE INDEX IF NOT EXISTS idx_bg_tasks_user_status ON background_tasks(user_id, status);")
conn.execute("CREATE INDEX IF NOT EXISTS idx_bg_tasks_status ON background_tasks(status);")
# Repair legacy cards with placeholder blanks or mismatched context
repair_mismatched_cefr_contexts(conn)
def repair_mismatched_cefr_contexts(conn: sqlite3.Connection):
"""
Repairs legacy vocabulary cards that were imported with placeholder blanks ('___')
or with mismatched contexts from CEFR test questions.
"""
try:
from app.cefr import resolve_context_sentence_for_term
# Check if table exists
tbl = conn.execute("SELECT 1 FROM sqlite_master WHERE type='table' AND name='vocabulary_cards'").fetchone()
if not tbl:
return
rows = conn.execute(
"""
SELECT id, term, context_sentence
FROM vocabulary_cards
WHERE instr(context_sentence, '___') > 0
"""
).fetchall()
for r in rows:
card_id = r["id"]
term = r["term"]
old_ctx = r["context_sentence"] or ""
new_ctx = resolve_context_sentence_for_term(conn, term, old_ctx)
if new_ctx and new_ctx != old_ctx:
conn.execute(
"UPDATE vocabulary_cards SET context_sentence = ? WHERE id = ?",
(new_ctx, card_id),
)
except Exception:
pass
def _seed_offline_dictionary(conn: sqlite3.Connection):