diff --git a/backend/DramaLing.Api/Controllers/AIController.cs b/backend/DramaLing.Api/Controllers/AIController.cs index 6997120..63850b4 100644 --- a/backend/DramaLing.Api/Controllers/AIController.cs +++ b/backend/DramaLing.Api/Controllers/AIController.cs @@ -562,8 +562,8 @@ public class AIController : ControllerBase Translation = aiAnalysis.Translation }, FinalAnalysisText = finalText ?? request.InputText, - WordAnalysis = aiAnalysis.WordAnalysis, - HighValueWords = aiAnalysis.HighValueWords, + WordAnalysis = PostProcessWordAnalysisWithUserLevel(aiAnalysis.WordAnalysis, userLevel), + HighValueWords = ExtractHighValueWords(aiAnalysis.WordAnalysis, userLevel), PhrasesDetected = new object[0] // 暫時簡化 }; @@ -1277,6 +1277,66 @@ public class AIController : ControllerBase }; } + /// + /// 後處理詞彙分析,根據用戶程度重新判定重點學習詞彙 + /// + private Dictionary PostProcessWordAnalysisWithUserLevel( + Dictionary originalAnalysis, string userLevel) + { + var processedAnalysis = new Dictionary(); + + foreach (var wordPair in originalAnalysis) + { + var wordData = wordPair.Value; + + // 從AI分析結果取得詞彙難度等級 + string wordLevel = wordData.DifficultyLevel ?? "A2"; + + // 使用CEFRLevelService重新判定是否為重點學習詞彙 + bool isHighValue = CEFRLevelService.IsHighValueForUser(wordLevel, userLevel); + + // 保留AI分析的其他資料,只重新設定isHighValue + processedAnalysis[wordPair.Key] = new + { + word = wordData.Word ?? wordPair.Key, + translation = wordData.Translation ?? "", + definition = wordData.Definition ?? "", + partOfSpeech = wordData.PartOfSpeech ?? "noun", + pronunciation = wordData.Pronunciation ?? $"/{wordPair.Key}/", + isHighValue = isHighValue, // 重新判定 + difficultyLevel = wordLevel, + synonyms = GetSynonyms(wordPair.Key), // 補充同義詞 + example = $"This is an example sentence using {wordPair.Key}.", + exampleTranslation = $"這是使用 {wordPair.Key} 的例句翻譯。" + }; + } + + return processedAnalysis; + } + + /// + /// 從詞彙分析中提取重點學習詞彙 + /// + private string[] ExtractHighValueWords(Dictionary wordAnalysis, string userLevel) + { + var highValueWords = new List(); + + foreach (var wordPair in wordAnalysis) + { + var wordData = wordPair.Value; + + string wordLevel = wordData.DifficultyLevel ?? "A2"; + + // 使用CEFRLevelService判定 + if (CEFRLevelService.IsHighValueForUser(wordLevel, userLevel)) + { + highValueWords.Add(wordPair.Key); + } + } + + return highValueWords.ToArray(); + } + #endregion /// diff --git a/backend/DramaLing.Api/Services/CEFRLevelService.cs b/backend/DramaLing.Api/Services/CEFRLevelService.cs index 3e01a5d..8c5b4cf 100644 --- a/backend/DramaLing.Api/Services/CEFRLevelService.cs +++ b/backend/DramaLing.Api/Services/CEFRLevelService.cs @@ -56,4 +56,62 @@ public static class CEFRLevelService var nextIndex = Math.Min(currentIndex + steps, Levels.Length - 1); return Levels[nextIndex]; } + + /// + /// 取得所有有效的CEFR等級 + /// + /// CEFR等級數組 + public static string[] GetAllLevels() + { + return (string[])Levels.Clone(); + } + + /// + /// 驗證CEFR等級是否有效 + /// + /// 要驗證的等級 + /// 是否為有效等級 + public static bool IsValidLevel(string level) + { + return !string.IsNullOrEmpty(level) && + Array.IndexOf(Levels, level.ToUpper()) != -1; + } + + /// + /// 取得等級的描述 + /// + /// CEFR等級 + /// 等級描述 + public static string GetLevelDescription(string level) + { + return level.ToUpper() switch + { + "A1" => "初學者 - 能理解基本詞彙和簡單句子", + "A2" => "基礎 - 能處理日常對話和常見主題", + "B1" => "中級 - 能理解清楚標準語言的要點", + "B2" => "中高級 - 能理解複雜文本的主要內容", + "C1" => "高級 - 能流利表達,理解含蓄意思", + "C2" => "精通 - 接近母語水平", + _ => "未知等級" + }; + } + + /// + /// 取得等級的範例詞彙 + /// + /// CEFR等級 + /// 範例詞彙數組 + public static string[] GetLevelExamples(string level) + { + return level.ToUpper() switch + { + "A1" => new[] { "hello", "good", "house", "eat", "happy" }, + "A2" => new[] { "important", "difficult", "interesting", "beautiful", "understand" }, + "B1" => new[] { "analyze", "opportunity", "environment", "responsibility", "development" }, + "B2" => new[] { "sophisticated", "implications", "comprehensive", "substantial", "methodology" }, + "C1" => new[] { "meticulous", "predominantly", "intricate", "corroborate", "paradigm" }, + "C2" => new[] { "ubiquitous", "ephemeral", "perspicacious", "multifarious", "idiosyncratic" }, + _ => new[] { "example" } + }; + } } \ No newline at end of file diff --git a/frontend/app/generate/page.tsx b/frontend/app/generate/page.tsx index a00ea08..5c5200d 100644 --- a/frontend/app/generate/page.tsx +++ b/frontend/app/generate/page.tsx @@ -33,6 +33,10 @@ function GenerateContent() { setIsAnalyzing(true) try { + // 取得用戶設定的程度 + const userLevel = localStorage.getItem('userEnglishLevel') || 'A2' + console.log('🎯 使用用戶程度:', userLevel) + const response = await fetch('http://localhost:5000/api/ai/analyze-sentence', { method: 'POST', headers: { @@ -40,7 +44,9 @@ function GenerateContent() { 'Authorization': `Bearer ${localStorage.getItem('auth_token')}` }, body: JSON.stringify({ - inputText: textInput + inputText: textInput, + userLevel: userLevel, // 傳遞用戶程度 + analysisMode: 'full' }) })