refactor: 移除後端高價值詞彙判定邏輯,完全交由前端處理

 移除的高價值判定方法:
- PostProcessWordAnalysisWithUserLevel (重新判定高價值)
- ExtractHighValueWords (提取高價值詞彙)
- IsHighValueWordDynamic (動態判定高價值)

 簡化analyze-sentence API:
- 移除HighValueWords欄位回應
- 移除高價值重新處理邏輯
- 直接回傳AI原始分析結果

🎯 責任分離:
- 後端:純AI分析、翻譯、語法修正
- 前端:高價值判定基於CEFR等級比較

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
鄭沛軒 2025-09-21 22:38:02 +08:00
parent 500d70839b
commit f60570390e
1 changed files with 7 additions and 101 deletions

View File

@ -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]; // 移除高價值詞彙判定,由前端負責
}
/// <summary>
/// 動態判斷高價值詞彙
/// </summary>
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());
}
/// <summary>
/// 獲取詞彙翻譯
@ -689,73 +661,7 @@ public class AIController : ControllerBase
};
}
/// <summary>
/// 後處理詞彙分析,根據用戶程度重新判定重點學習詞彙
/// </summary>
private Dictionary<string, object> PostProcessWordAnalysisWithUserLevel(
Dictionary<string, WordAnalysisResult> originalAnalysis, string userLevel)
{
var processedAnalysis = new Dictionary<string, object>();
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;
}
/// <summary>
/// 從詞彙分析中提取重點學習詞彙
/// </summary>
private string[] ExtractHighValueWords(Dictionary<string, WordAnalysisResult> wordAnalysis, string userLevel)
{
var highValueWords = new List<string>();
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();
}
/// <summary>
/// 取得有學習價值的例句