From f60570390e70d57b3615125defdbcc2c2590d224 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=84=AD=E6=B2=9B=E8=BB=92?= Date: Sun, 21 Sep 2025 22:38:02 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=A7=BB=E9=99=A4=E5=BE=8C?= =?UTF-8?q?=E7=AB=AF=E9=AB=98=E5=83=B9=E5=80=BC=E8=A9=9E=E5=BD=99=E5=88=A4?= =?UTF-8?q?=E5=AE=9A=E9=82=8F=E8=BC=AF=EF=BC=8C=E5=AE=8C=E5=85=A8=E4=BA=A4?= =?UTF-8?q?=E7=94=B1=E5=89=8D=E7=AB=AF=E8=99=95=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ❌ 移除的高價值判定方法: - PostProcessWordAnalysisWithUserLevel (重新判定高價值) - ExtractHighValueWords (提取高價值詞彙) - IsHighValueWordDynamic (動態判定高價值) ✅ 簡化analyze-sentence API: - 移除HighValueWords欄位回應 - 移除高價值重新處理邏輯 - 直接回傳AI原始分析結果 🎯 責任分離: - 後端:純AI分析、翻譯、語法修正 - 前端:高價值判定基於CEFR等級比較 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- .../DramaLing.Api/Controllers/AIController.cs | 108 ++---------------- 1 file changed, 7 insertions(+), 101 deletions(-) diff --git a/backend/DramaLing.Api/Controllers/AIController.cs b/backend/DramaLing.Api/Controllers/AIController.cs index 4fc5b2b..a8eb13b 100644 --- a/backend/DramaLing.Api/Controllers/AIController.cs +++ b/backend/DramaLing.Api/Controllers/AIController.cs @@ -98,21 +98,19 @@ public class AIController : ControllerBase var finalText = aiAnalysis.GrammarCorrection.HasErrors ? aiAnalysis.GrammarCorrection.CorrectedText : request.InputText; - // 3. 準備AI分析響應資料(包含個人化資訊) + // 3. 準備AI分析響應資料 var baseResponseData = new { AnalysisId = Guid.NewGuid(), InputText = request.InputText, - UserLevel = userLevel, // 新增:顯示使用的程度 - HighValueCriteria = CEFRLevelService.GetTargetLevelRange(userLevel), // 新增:顯示高價值判定範圍 + UserLevel = userLevel, GrammarCorrection = aiAnalysis.GrammarCorrection, SentenceMeaning = new { Translation = aiAnalysis.Translation }, FinalAnalysisText = finalText ?? request.InputText, - WordAnalysis = PostProcessWordAnalysisWithUserLevel(aiAnalysis.WordAnalysis, userLevel), - HighValueWords = ExtractHighValueWords(aiAnalysis.WordAnalysis, userLevel), + WordAnalysis = aiAnalysis.WordAnalysis, PhrasesDetected = new object[0] // 暫時簡化 }; @@ -146,7 +144,6 @@ public class AIController : ControllerBase }, FinalAnalysisText = finalText, WordAnalysis = analysis.WordAnalysis, - HighValueWords = analysis.HighValueWords, PhrasesDetected = analysis.PhrasesDetected }; @@ -253,7 +250,7 @@ public class AIController : ControllerBase Translation = card.Translation, Explanation = card.Definition, // 使用 AI 生成的定義作為解釋 WordAnalysis = GenerateWordAnalysisForSentence(text), - HighValueWords = GetHighValueWordsForSentence(text), + HighValueWords = new string[0], // 移除高價值詞彙判定,由前端負責 PhrasesDetected = new[] { new @@ -318,7 +315,7 @@ public class AIController : ControllerBase Translation = translation, Explanation = explanation, WordAnalysis = GenerateWordAnalysisForSentence(text), - HighValueWords = GetHighValueWordsForSentence(text), + HighValueWords = new string[0], // 移除高價值詞彙判定,由前端負責 PhrasesDetected = new[] { new @@ -480,8 +477,6 @@ public class AIController : ControllerBase var cleanWord = word.Trim(); if (string.IsNullOrEmpty(cleanWord) || cleanWord.Length < 2) continue; - // 判斷是否為高價值詞彙 - var isHighValue = IsHighValueWordDynamic(cleanWord); var difficulty = GetWordDifficulty(cleanWord); analysis[cleanWord] = new @@ -494,10 +489,7 @@ public class AIController : ControllerBase synonyms = GetSynonyms(cleanWord), antonyms = new string[0], isPhrase = false, - isHighValue = isHighValue, - learningPriority = isHighValue ? "high" : "low", - difficultyLevel = difficulty, - costIncurred = isHighValue ? 0 : 1 + difficultyLevel = difficulty }; } @@ -510,29 +502,9 @@ public class AIController : ControllerBase private string[] GetHighValueWordsForSentence(string text) { var words = text.ToLower().Split(new[] { ' ', '.', ',', '!', '?' }, StringSplitOptions.RemoveEmptyEntries); - return words.Where(w => IsHighValueWordDynamic(w.Trim())).ToArray(); + return new string[0]; // 移除高價值詞彙判定,由前端負責 } - /// - /// 動態判斷高價值詞彙 - /// - private bool IsHighValueWordDynamic(string word) - { - // B1+ 詞彙或特殊概念詞彙視為高價值 - var highValueWords = new[] - { - "animals", "instincts", "safe", "food", "find", - "brought", "meeting", "agreed", "thing", - "study", "learn", "important", "necessary", - "beautiful", "wonderful", "amazing", - "problem", "solution", "different", "special", - "cut", "slack", "job", "new", "think", "should", - "ashamed", "mistake", "apologized", "felt", - "strong", "wind", "knocked", "tree", "park" - }; - - return highValueWords.Contains(word.ToLower()); - } /// /// 獲取詞彙翻譯 @@ -689,73 +661,7 @@ 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例句 - _logger.LogInformation("🔍 詞彙 {Word}: AI例句={Example}, AI翻譯={ExampleTranslation}", - wordPair.Key, wordData.Example ?? "null", wordData.ExampleTranslation ?? "null"); - - // 保留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 = !string.IsNullOrEmpty(wordData.Example) ? - wordData.Example : - GetQualityExampleSentence(wordPair.Key), // 優先使用AI例句 - exampleTranslation = !string.IsNullOrEmpty(wordData.ExampleTranslation) ? - wordData.ExampleTranslation : - GetQualityExampleTranslation(wordPair.Key) // 優先使用AI翻譯 - }; - } - - 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(); - } /// /// 取得有學習價值的例句