From 202fb5ade9195eb7c5f38a8c4d39b4620bc2585f Mon Sep 17 00:00:00 2001 From: TTlaji Date: Mon, 8 Jun 2026 14:54:41 +0800 Subject: [PATCH] feat: add practice answer helpers --- app.js | 149 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) diff --git a/app.js b/app.js index 285f4ae..6ce3623 100644 --- a/app.js +++ b/app.js @@ -723,6 +723,134 @@ function getSecondaryCues() { return video?.subtitles?.[config.secondaryCode]?.cues || []; } +const practiceModeLabels = { + bilingual: "双语", + english: "英文", + chinese: "中文", + dictation: "听写", + translation: "中译英", +}; + +function normalizePracticeAnswer(text) { + return String(text || "") + .toLowerCase() + .replace(/[’‘]/g, "'") + .replace(/[“”]/g, "\"") + .replace(/[-–—]+/g, " ") + .replace(/[.,!?;:"()[\]{},。!?;:“”‘’、]/g, " ") + .replace(/\s+/g, " ") + .trim(); +} + +function comparePracticeAnswer(input, expected) { + const reference = String(expected || "").trim(); + const normalizedInput = normalizePracticeAnswer(input); + const normalizedExpected = normalizePracticeAnswer(expected); + + if (!normalizedInput) { + return { + status: "empty", + isCorrect: false, + reference, + message: "请输入答案后再核对。", + }; + } + + if (!normalizedExpected) { + return { + status: "missing-reference", + isCorrect: false, + reference, + message: "当前句没有可核对的英文参考。", + }; + } + + if (normalizedInput === normalizedExpected) { + return { + status: "correct", + isCorrect: true, + reference, + message: "完全匹配。", + }; + } + + return { + status: "incorrect", + isCorrect: false, + reference, + message: "还不完全一致,参考答案如下。", + }; +} + +function getPracticeViewState(mode, cuePair = {}) { + const englishText = cuePair.english?.text?.trim() || ""; + const chineseText = cuePair.chinese?.text?.trim() || ""; + const normalizedMode = practiceModeLabels[mode] ? mode : "bilingual"; + + if (normalizedMode === "dictation") { + return { + mode: normalizedMode, + label: practiceModeLabels[normalizedMode], + promptText: "听音频,写出当前英文句子。", + referenceText: "", + expectedText: englishText, + inputVisible: true, + canCheck: Boolean(englishText), + feedbackHint: englishText ? "" : "当前句没有可核对的英文参考。", + }; + } + + if (normalizedMode === "translation") { + return { + mode: normalizedMode, + label: practiceModeLabels[normalizedMode], + promptText: chineseText || "当前句没有中文字幕。", + referenceText: "根据中文写出英文句子。", + expectedText: englishText, + inputVisible: true, + canCheck: Boolean(englishText), + feedbackHint: englishText ? "" : "当前句没有可核对的英文参考。", + }; + } + + if (normalizedMode === "english") { + return { + mode: normalizedMode, + label: practiceModeLabels[normalizedMode], + promptText: englishText || "当前句没有英文字幕。", + referenceText: "", + expectedText: englishText, + inputVisible: false, + canCheck: false, + feedbackHint: "", + }; + } + + if (normalizedMode === "chinese") { + return { + mode: normalizedMode, + label: practiceModeLabels[normalizedMode], + promptText: chineseText || "当前句没有中文字幕。", + referenceText: "", + expectedText: englishText, + inputVisible: false, + canCheck: false, + feedbackHint: "", + }; + } + + return { + mode: "bilingual", + label: practiceModeLabels.bilingual, + promptText: englishText || "当前句没有英文字幕。", + referenceText: chineseText, + expectedText: englishText, + inputVisible: false, + canCheck: false, + feedbackHint: "", + }; +} + function getActiveSubtitleModeConfig() { const video = getActiveVideo(); return getSubtitleModeConfig(subtitleMode, video?.subtitles || {}); @@ -751,6 +879,24 @@ function getSubtitleModeConfig(mode, subtitles = {}) { }; } + if (mode === "translation") { + return { + primaryCode: hasChinese ? "zh" : fallbackCode, + secondaryCode: "", + showSecondary: false, + message: hasChinese ? "" : "当前素材没有中文字幕。", + }; + } + + if (mode === "dictation") { + return { + primaryCode: hasEnglish ? "en" : fallbackCode, + secondaryCode: "", + showSecondary: false, + message: "", + }; + } + return { primaryCode: hasEnglish ? "en" : fallbackCode, secondaryCode: "", @@ -1007,6 +1153,9 @@ if (typeof module !== "undefined" && module.exports) { calculatePracticeSummary, formatProgressSummary, getSubtitleModeConfig, + normalizePracticeAnswer, + comparePracticeAnswer, + getPracticeViewState, parseSubtitle, }; }