feat: add practice answer helpers

This commit is contained in:
TTlaji committed 2026-06-08 14:54:41 +08:00
1 parent 2e62600b0d
commit 202fb5ade9
1 file changed
+149
+149
View File
@@ -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,
};
}