using System;
namespace DramaLing.Api.Services;
public static class CEFRLevelService
{
private static readonly string[] Levels = { "A1", "A2", "B1", "B2", "C1", "C2" };
///
/// 取得 CEFR 等級的數字索引
///
public static int GetLevelIndex(string level)
{
if (string.IsNullOrEmpty(level)) return 1; // 預設 A2
return Array.IndexOf(Levels, level.ToUpper());
}
///
/// 判定詞彙對特定用戶是否為高價值
/// 規則:比用戶程度高 1-2 級的詞彙為高價值
///
public static bool IsHighValueForUser(string wordLevel, string userLevel)
{
var userIndex = GetLevelIndex(userLevel);
var wordIndex = GetLevelIndex(wordLevel);
// 無效等級處理
if (userIndex == -1 || wordIndex == -1) return false;
// 高價值 = 比用戶程度高 1-2 級
return wordIndex >= userIndex + 1 && wordIndex <= userIndex + 2;
}
///
/// 取得用戶的目標學習等級範圍
///
public static string GetTargetLevelRange(string userLevel)
{
var userIndex = GetLevelIndex(userLevel);
if (userIndex == -1) return "B1-B2";
var targetMin = Levels[Math.Min(userIndex + 1, Levels.Length - 1)];
var targetMax = Levels[Math.Min(userIndex + 2, Levels.Length - 1)];
return targetMin == targetMax ? targetMin : $"{targetMin}-{targetMax}";
}
///
/// 取得下一個等級
///
public static string GetNextLevel(string currentLevel, int steps = 1)
{
var currentIndex = GetLevelIndex(currentLevel);
if (currentIndex == -1) return "B1";
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" }
};
}
}