dramaling-vocab-learning/backend/DramaLing.Api/Controllers/OptionsVocabularyTestContro...

177 lines
5.8 KiB
C#

using DramaLing.Api.Services;
using Microsoft.AspNetCore.Mvc;
namespace DramaLing.Api.Controllers;
/// <summary>
/// 選項詞彙庫服務測試控制器 (僅用於開發測試)
/// </summary>
[Route("api/test/[controller]")]
public class OptionsVocabularyTestController : BaseController
{
private readonly IOptionsVocabularyService _optionsVocabularyService;
public OptionsVocabularyTestController(
IOptionsVocabularyService optionsVocabularyService,
ILogger<OptionsVocabularyTestController> logger) : base(logger)
{
_optionsVocabularyService = optionsVocabularyService;
}
/// <summary>
/// 測試智能干擾選項生成
/// </summary>
[HttpGet("generate-distractors")]
public async Task<ActionResult> TestGenerateDistractors(
[FromQuery] string targetWord = "beautiful",
[FromQuery] string cefrLevel = "B1",
[FromQuery] string partOfSpeech = "adjective",
[FromQuery] int count = 3)
{
try
{
var distractors = await _optionsVocabularyService.GenerateDistractorsAsync(
targetWord, cefrLevel, partOfSpeech, count);
return Ok(new
{
success = true,
targetWord,
cefrLevel,
partOfSpeech,
requestedCount = count,
actualCount = distractors.Count,
distractors
});
}
catch (Exception ex)
{
_logger.LogError(ex, "測試生成干擾選項失敗");
return StatusCode(500, new { success = false, error = ex.Message });
}
}
/// <summary>
/// 測試詞彙庫充足性檢查
/// </summary>
[HttpGet("check-sufficiency")]
public async Task<ActionResult> TestVocabularySufficiency(
[FromQuery] string cefrLevel = "B1",
[FromQuery] string partOfSpeech = "adjective")
{
try
{
var hasSufficient = await _optionsVocabularyService.HasSufficientVocabularyAsync(
cefrLevel, partOfSpeech);
return Ok(new
{
success = true,
cefrLevel,
partOfSpeech,
hasSufficientVocabulary = hasSufficient
});
}
catch (Exception ex)
{
_logger.LogError(ex, "測試詞彙庫充足性檢查失敗");
return StatusCode(500, new { success = false, error = ex.Message });
}
}
/// <summary>
/// 測試帶詳細資訊的干擾選項生成
/// </summary>
[HttpGet("generate-distractors-detailed")]
public async Task<ActionResult> TestGenerateDistractorsWithDetails(
[FromQuery] string targetWord = "beautiful",
[FromQuery] string cefrLevel = "B1",
[FromQuery] string partOfSpeech = "adjective",
[FromQuery] int count = 3)
{
try
{
var distractorsWithDetails = await _optionsVocabularyService.GenerateDistractorsWithDetailsAsync(
targetWord, cefrLevel, partOfSpeech, count);
var result = distractorsWithDetails.Select(d => new
{
d.Word,
d.CEFRLevel,
d.PartOfSpeech,
d.WordLength,
d.IsActive
}).ToList();
return Ok(new
{
success = true,
targetWord,
cefrLevel,
partOfSpeech,
requestedCount = count,
actualCount = result.Count,
distractors = result
});
}
catch (Exception ex)
{
_logger.LogError(ex, "測試生成詳細干擾選項失敗");
return StatusCode(500, new { success = false, error = ex.Message });
}
}
/// <summary>
/// 測試多種詞性的詞彙庫覆蓋率
/// </summary>
[HttpGet("coverage-test")]
public async Task<ActionResult> TestVocabularyCoverage()
{
try
{
var testCases = new[]
{
new { CEFR = "A1", PartOfSpeech = "noun" },
new { CEFR = "A1", PartOfSpeech = "verb" },
new { CEFR = "A1", PartOfSpeech = "adjective" },
new { CEFR = "B1", PartOfSpeech = "noun" },
new { CEFR = "B1", PartOfSpeech = "verb" },
new { CEFR = "B1", PartOfSpeech = "adjective" },
new { CEFR = "B2", PartOfSpeech = "noun" },
new { CEFR = "C1", PartOfSpeech = "noun" }
};
var results = new List<object>();
foreach (var testCase in testCases)
{
var hasSufficient = await _optionsVocabularyService.HasSufficientVocabularyAsync(
testCase.CEFR, testCase.PartOfSpeech);
var distractors = await _optionsVocabularyService.GenerateDistractorsAsync(
"test", testCase.CEFR, testCase.PartOfSpeech, 3);
results.Add(new
{
cefrLevel = testCase.CEFR,
partOfSpeech = testCase.PartOfSpeech,
hasSufficientVocabulary = hasSufficient,
generatedCount = distractors.Count,
sampleDistractors = distractors.Take(3).ToList()
});
}
return Ok(new
{
success = true,
coverageResults = results
});
}
catch (Exception ex)
{
_logger.LogError(ex, "測試詞彙庫覆蓋率失敗");
return StatusCode(500, new { success = false, error = ex.Message });
}
}
}