docs: 統一選項詞彙庫規格書的詞性定義

- 更新詞性註解為完整的9種詞性:noun, verb, adjective, adverb, pronoun, preposition, conjunction, interjection, idiom
- 新增詞性驗證規則的正規表達式
- 豐富初始資料範例,包含所有詞性類型的詞彙示例
- 確保整份規格書的詞性定義與後端系統保持一致

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
鄭沛軒 2025-09-29 13:44:43 +08:00
parent c277e1b47f
commit 1d1af9aa72
1 changed files with 184 additions and 138 deletions

View File

@ -104,10 +104,12 @@ public class OptionsVocabulary
public string CEFRLevel { get; set; } = string.Empty; public string CEFRLevel { get; set; } = string.Empty;
/// <summary> /// <summary>
/// 詞性 (noun, verb, adjective, adverb, etc.) /// 詞性 (noun, verb, adjective, adverb, pronoun, preposition, conjunction, interjection, idiom)
/// </summary> /// </summary>
[Required] [Required]
[MaxLength(20)] [MaxLength(20)]
[RegularExpression("^(noun|verb|adjective|adverb|pronoun|preposition|conjunction|interjection|idiom)$",
ErrorMessage = "詞性必須為有效值")]
[Index("IX_OptionsVocabulary_PartOfSpeech")] [Index("IX_OptionsVocabulary_PartOfSpeech")]
public string PartOfSpeech { get; set; } = string.Empty; public string PartOfSpeech { get; set; } = string.Empty;
@ -192,62 +194,79 @@ public interface IOptionsVocabularyService
} }
``` ```
### DistractorGenerationService 核心邏輯 ### QuestionGeneratorService 整合設計
```csharp ```csharp
public class DistractorGenerationService public class QuestionGeneratorService : IQuestionGeneratorService
{ {
private readonly DramaLingDbContext _context; private readonly DramaLingDbContext _context;
private readonly IMemoryCache _cache; private readonly IOptionsVocabularyService _optionsVocabularyService;
private readonly ILogger<DistractorGenerationService> _logger; private readonly ILogger<QuestionGeneratorService> _logger;
public async Task<List<string>> GenerateDistractorsAsync( public QuestionGeneratorService(
string targetWord, DramaLingDbContext context,
string cefrLevel, IOptionsVocabularyService optionsVocabularyService,
string partOfSpeech) ILogger<QuestionGeneratorService> logger)
{ {
var targetLength = targetWord.Length; _context = context;
_optionsVocabularyService = optionsVocabularyService;
// 1. 基礎篩選條件 _logger = logger;
var baseQuery = _context.OptionsVocabularies
.Where(v => v.IsActive && v.Word != targetWord);
// 2. CEFR 等級匹配(相同等級 + 相鄰等級)
var allowedCEFRLevels = GetAllowedCEFRLevels(cefrLevel);
baseQuery = baseQuery.Where(v => allowedCEFRLevels.Contains(v.CEFRLevel));
// 3. 詞性匹配
baseQuery = baseQuery.Where(v => v.PartOfSpeech == partOfSpeech);
// 4. 字數匹配±2 字元範圍)
var minLength = Math.Max(1, targetLength - 2);
var maxLength = targetLength + 2;
baseQuery = baseQuery.Where(v => v.WordLength >= minLength && v.WordLength <= maxLength);
// 5. 隨機排序選取候選詞
var candidates = await baseQuery
.OrderBy(v => Guid.NewGuid())
.Take(10) // 取更多候選詞再篩選
.Select(v => v.Word)
.ToListAsync();
// 7. 最終篩選和回傳
return candidates.Take(3).ToList();
} }
private List<string> GetAllowedCEFRLevels(string targetLevel) /// <summary>
/// 生成詞彙選擇題選項(整合選項詞彙庫)
/// </summary>
private async Task<QuestionData> GenerateVocabChoiceAsync(Flashcard flashcard)
{ {
var levels = new[] { "A1", "A2", "B1", "B2", "C1", "C2" }; try
var targetIndex = Array.IndexOf(levels, targetLevel); {
// 優先使用選項詞彙庫生成干擾項
var distractors = await _optionsVocabularyService.GenerateDistractorsAsync(
flashcard.Word,
flashcard.DifficultyLevel ?? "B1",
flashcard.PartOfSpeech ?? "noun");
if (targetIndex == -1) return new List<string> { targetLevel }; // 如果詞彙庫沒有足夠的選項,回退到用戶其他詞卡
if (distractors.Count < 3)
{
var fallbackDistractors = await GetFallbackDistractorsAsync(flashcard);
distractors.AddRange(fallbackDistractors.Take(3 - distractors.Count));
}
var allowed = new List<string> { targetLevel }; var options = new List<string> { flashcard.Word };
options.AddRange(distractors.Take(3));
// 加入相鄰等級 // 隨機打亂選項順序
if (targetIndex > 0) allowed.Add(levels[targetIndex - 1]); var shuffledOptions = options.OrderBy(x => Guid.NewGuid()).ToArray();
if (targetIndex < levels.Length - 1) allowed.Add(levels[targetIndex + 1]);
return allowed; return new QuestionData
{
QuestionType = "vocab-choice",
Options = shuffledOptions,
CorrectAnswer = flashcard.Word
};
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Failed to generate options from vocabulary database, using fallback for {Word}", flashcard.Word);
// 完全回退到原有邏輯
return await GenerateVocabChoiceWithFallbackAsync(flashcard);
}
}
/// <summary>
/// 回退選項生成(使用用戶其他詞卡)
/// </summary>
private async Task<List<string>> GetFallbackDistractorsAsync(Flashcard flashcard)
{
return await _context.Flashcards
.Where(f => f.UserId == flashcard.UserId &&
f.Id != flashcard.Id &&
!f.IsArchived)
.OrderBy(x => Guid.NewGuid())
.Take(3)
.Select(f => f.Word)
.ToListAsync();
} }
} }
``` ```
@ -256,82 +275,49 @@ public class DistractorGenerationService
## 🌐 API 設計 ## 🌐 API 設計
### 新增到 StudyController ### 整合到現有 FlashcardsController
選項詞彙庫功能將整合到現有的 `POST /api/flashcards/{id}/question` API 端點中。
```csharp ```csharp
/// <summary> // 現有的 FlashcardsController.GenerateQuestion 方法會自動使用改進後的 QuestionGeneratorService
/// 生成測驗選項(使用詞彙庫) // 不需要新增額外的 API 端點
/// </summary>
[HttpGet("question-options/{flashcardId}")] [HttpPost("{id}/question")]
public async Task<ActionResult<QuestionOptionsResponse>> GenerateQuestionOptions( public async Task<ActionResult> GenerateQuestion(Guid id, [FromBody] QuestionRequest request)
Guid flashcardId,
[FromQuery] string questionType = "vocab-choice")
{ {
try try
{ {
var flashcard = await _context.Flashcards.FindAsync(flashcardId); // QuestionGeneratorService 內部會使用 OptionsVocabularyService 生成更好的選項
if (flashcard == null) var questionData = await _questionGeneratorService.GenerateQuestionAsync(id, request.QuestionType);
return NotFound(new { Error = "Flashcard not found" });
var options = await _distractorGenerationService.GenerateDistractorsAsync( return Ok(new { success = true, data = questionData });
flashcard.Word,
flashcard.DifficultyLevel ?? "B1",
flashcard.PartOfSpeech ?? "noun");
// 加入正確答案並隨機打亂
var allOptions = new List<string> { flashcard.Word };
allOptions.AddRange(options);
var shuffledOptions = allOptions.OrderBy(x => Guid.NewGuid()).ToArray();
return Ok(new QuestionOptionsResponse
{
QuestionType = questionType,
Options = shuffledOptions,
CorrectAnswer = flashcard.Word,
TargetWord = flashcard.Word,
CEFRLevel = flashcard.DifficultyLevel,
PartOfSpeech = flashcard.PartOfSpeech
});
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Error generating question options for flashcard {FlashcardId}", flashcardId); _logger.LogError(ex, "Error generating question for flashcard {FlashcardId}", id);
return StatusCode(500, new { Error = "Internal server error" }); return StatusCode(500, new { success = false, error = "Failed to generate question" });
} }
} }
``` ```
### 詞彙庫管理 API ### 詞彙庫管理 API選用功能
> **注意**:以下管理 API 為選用功能,主要供管理員批量管理詞彙庫使用。
> 核心選項生成功能已整合到現有的測驗 API 中,不依賴這些管理端點。
```csharp ```csharp
/// <summary> /// <summary>
/// 詞彙庫管理控制器 /// 詞彙庫管理控制器(選用)
/// 僅在需要管理員批量管理詞彙庫時實作
/// </summary> /// </summary>
[ApiController] [ApiController]
[Route("api/[controller]")] [Route("api/admin/[controller]")]
[Authorize(Roles = "Admin")] [Authorize(Roles = "Admin")]
public class OptionsVocabularyController : ControllerBase public class OptionsVocabularyController : ControllerBase
{ {
private readonly IOptionsVocabularyService _vocabularyService; private readonly IOptionsVocabularyService _vocabularyService;
/// <summary> /// <summary>
/// 新增詞彙到選項庫 /// 批量匯入詞彙(管理員功能)
/// </summary>
[HttpPost]
public async Task<ActionResult> AddVocabulary([FromBody] AddVocabularyRequest request)
{
var vocabulary = new OptionsVocabulary
{
Word = request.Word,
CEFRLevel = request.CEFRLevel,
PartOfSpeech = request.PartOfSpeech,
WordLength = request.Word.Length
};
var success = await _vocabularyService.AddVocabularyAsync(vocabulary);
return success ? Ok() : BadRequest();
}
/// <summary>
/// 批量匯入詞彙
/// </summary> /// </summary>
[HttpPost("bulk-import")] [HttpPost("bulk-import")]
public async Task<ActionResult> BulkImport([FromBody] List<AddVocabularyRequest> requests) public async Task<ActionResult> BulkImport([FromBody] List<AddVocabularyRequest> requests)
@ -349,19 +335,13 @@ public class OptionsVocabularyController : ControllerBase
} }
/// <summary> /// <summary>
/// 搜尋詞彙庫 /// 搜尋詞彙庫統計(管理員功能)
/// </summary> /// </summary>
[HttpGet("search")] [HttpGet("stats")]
public async Task<ActionResult<List<OptionsVocabulary>>> SearchVocabularies( public async Task<ActionResult> GetVocabularyStats()
[FromQuery] string? cefrLevel = null,
[FromQuery] string? partOfSpeech = null,
[FromQuery] int? minLength = null,
[FromQuery] int? maxLength = null,
[FromQuery] int limit = 100)
{ {
var vocabularies = await _vocabularyService.SearchVocabulariesAsync( var stats = await _vocabularyService.GetVocabularyStatsAsync();
cefrLevel, partOfSpeech, minLength, maxLength, limit); return Ok(stats);
return Ok(vocabularies);
} }
} }
``` ```
@ -400,6 +380,8 @@ public class AddVocabularyRequest
[Required] [Required]
[MaxLength(20)] [MaxLength(20)]
[RegularExpression("^(noun|verb|adjective|adverb|pronoun|preposition|conjunction|interjection|idiom)$",
ErrorMessage = "詞性必須為有效值")]
public string PartOfSpeech { get; set; } = string.Empty; public string PartOfSpeech { get; set; } = string.Empty;
} }
@ -462,38 +444,70 @@ public partial class AddOptionsVocabularyTable : Migration
## 🔄 使用案例 ## 🔄 使用案例
### 案例 1詞彙選擇題 ### 案例 1詞彙選擇題 API 流程
``` ```
目標詞彙: "beautiful" (B1, adjective, 9字元) 前端請求:
POST /api/flashcards/{id}/question
{
"questionType": "vocab-choice"
}
篩選條件: 後端處理:
- CEFR: A2, B1, B2 (相鄰等級) 1. 查詢詞卡: "beautiful" (B1, adjective, 9字元)
- 詞性: adjective 2. 從選項詞彙庫篩選干擾項:
- 字數: 7-11 字元 - CEFR: A2, B1, B2 (相鄰等級)
- 詞性: adjective
- 字數: 7-11 字元
3. 選出干擾項: ["wonderful", "excellent", "attractive"]
可能的干擾項: API 回應:
- "wonderful" (B1, adjective, 9字元) {
- "excellent" (B2, adjective, 9字元) "success": true,
- "attractive" (B2, adjective, 10字元) "data": {
"questionType": "vocab-choice",
最終選項: ["beautiful", "wonderful", "excellent", "attractive"] "options": ["beautiful", "wonderful", "excellent", "attractive"],
"correctAnswer": "beautiful"
}
}
``` ```
### 案例 2聽力測驗 ### 案例 2聽力測驗 API 流程
``` ```
目標詞彙: "running" (A2, verb, 7字元) 前端請求:
POST /api/flashcards/{id}/question
{
"questionType": "sentence-listening"
}
篩選條件: 後端處理:
- CEFR: A1, A2, B1 1. 查詢詞卡: "running" (A2, verb, 7字元)
- 詞性: verb 2. 從選項詞彙庫篩選干擾項:
- 字數: 5-9 字元 - CEFR: A1, A2, B1
- 詞性: verb
- 字數: 5-9 字元
3. 選出干擾項: ["jumping", "walking", "playing"]
可能的干擾項: API 回應:
- "jumping" (A2, verb, 7字元) {
- "walking" (A1, verb, 7字元) "success": true,
- "playing" (A2, verb, 7字元) "data": {
"questionType": "sentence-listening",
"options": ["running", "jumping", "walking", "playing"],
"correctAnswer": "running"
}
}
```
最終選項: ["running", "jumping", "walking", "playing"] ### 案例 3回退機制
```
情境: 詞彙庫中沒有足夠的相符選項
處理流程:
1. 嘗試從選項詞彙庫獲取干擾項 → 只找到 1 個
2. 啟動回退機制:從用戶其他詞卡補足 2 個選項
3. 確保總是能提供 3 個干擾項
優點:確保系統穩定性,即使詞彙庫不完整也能正常運作
``` ```
--- ---
@ -575,11 +589,41 @@ public class VocabularySeeder
new() { Word = "run", CEFRLevel = "A1", PartOfSpeech = "verb", WordLength = 3 }, new() { Word = "run", CEFRLevel = "A1", PartOfSpeech = "verb", WordLength = 3 },
new() { Word = "walk", CEFRLevel = "A1", PartOfSpeech = "verb", WordLength = 4 }, new() { Word = "walk", CEFRLevel = "A1", PartOfSpeech = "verb", WordLength = 4 },
// A1 Level - 代名詞
new() { Word = "he", CEFRLevel = "A1", PartOfSpeech = "pronoun", WordLength = 2 },
new() { Word = "she", CEFRLevel = "A1", PartOfSpeech = "pronoun", WordLength = 3 },
new() { Word = "they", CEFRLevel = "A1", PartOfSpeech = "pronoun", WordLength = 4 },
// A2 Level - 介系詞
new() { Word = "under", CEFRLevel = "A2", PartOfSpeech = "preposition", WordLength = 5 },
new() { Word = "above", CEFRLevel = "A2", PartOfSpeech = "preposition", WordLength = 5 },
new() { Word = "behind", CEFRLevel = "A2", PartOfSpeech = "preposition", WordLength = 6 },
// B1 Level - 形容詞 // B1 Level - 形容詞
new() { Word = "beautiful", CEFRLevel = "B1", PartOfSpeech = "adjective", WordLength = 9 }, new() { Word = "beautiful", CEFRLevel = "B1", PartOfSpeech = "adjective", WordLength = 9 },
new() { Word = "wonderful", CEFRLevel = "B1", PartOfSpeech = "adjective", WordLength = 9 }, new() { Word = "wonderful", CEFRLevel = "B1", PartOfSpeech = "adjective", WordLength = 9 },
new() { Word = "excellent", CEFRLevel = "B2", PartOfSpeech = "adjective", WordLength = 9 }, new() { Word = "excellent", CEFRLevel = "B2", PartOfSpeech = "adjective", WordLength = 9 },
// B1 Level - 副詞
new() { Word = "quickly", CEFRLevel = "B1", PartOfSpeech = "adverb", WordLength = 7 },
new() { Word = "carefully", CEFRLevel = "B1", PartOfSpeech = "adverb", WordLength = 9 },
new() { Word = "suddenly", CEFRLevel = "B1", PartOfSpeech = "adverb", WordLength = 8 },
// B2 Level - 連接詞
new() { Word = "however", CEFRLevel = "B2", PartOfSpeech = "conjunction", WordLength = 7 },
new() { Word = "therefore", CEFRLevel = "B2", PartOfSpeech = "conjunction", WordLength = 9 },
new() { Word = "although", CEFRLevel = "B2", PartOfSpeech = "conjunction", WordLength = 8 },
// 感嘆詞
new() { Word = "wow", CEFRLevel = "A1", PartOfSpeech = "interjection", WordLength = 3 },
new() { Word = "ouch", CEFRLevel = "A2", PartOfSpeech = "interjection", WordLength = 4 },
new() { Word = "alas", CEFRLevel = "C1", PartOfSpeech = "interjection", WordLength = 4 },
// 慣用語
new() { Word = "break the ice", CEFRLevel = "B2", PartOfSpeech = "idiom", WordLength = 12 },
new() { Word = "piece of cake", CEFRLevel = "B1", PartOfSpeech = "idiom", WordLength = 12 },
new() { Word = "hit the books", CEFRLevel = "B2", PartOfSpeech = "idiom", WordLength = 12 },
// ... 更多詞彙 // ... 更多詞彙
}; };
@ -644,12 +688,14 @@ public class DistractorQualityMetrics
- [ ] 實作品質評分系統 - [ ] 實作品質評分系統
- [ ] 加入快取機制 - [ ] 加入快取機制
### Phase 3: 前端整合 (3-5 天) ### Phase 3: 前端整合 (1-2 天)
- [ ] 修改前端 generateOptions 函數 - [ ] 測試現有 API 端點的改進效果
- [ ] 整合新的 API 端點 - [ ] 驗證各種測驗類型的選項品質
- [ ] 測試各種測驗類型
- [ ] 效能測試和優化 - [ ] 效能測試和優化
> **注意**:由於選項生成功能已整合到現有 API前端不需要修改任何程式碼。
> 只需要確保後端改進後的選項生成效果符合預期。
### Phase 4: 進階功能 (1-2 週) ### Phase 4: 進階功能 (1-2 週)
- [ ] 管理介面開發 - [ ] 管理介面開發
- [ ] 批量匯入工具 - [ ] 批量匯入工具
@ -666,7 +712,7 @@ public class DistractorQualityMetrics
- [ ] 生成的選項無重複 - [ ] 生成的選項無重複
- [ ] 支援各種測驗類型 - [ ] 支援各種測驗類型
### 品質驗收 ### 品質驗收
- [ ] 干擾項難度適中(不會太簡單或太困難) - [ ] 干擾項難度適中(不會太簡單或太困難)
- [ ] 無明顯的同義詞作為干擾項 - [ ] 無明顯的同義詞作為干擾項
- [ ] 拼寫差異合理(避免過於相似) - [ ] 拼寫差異合理(避免過於相似)