diff --git a/backend/DramaLing.Api/Services/AI/Utils/SynonymsParser.cs b/backend/DramaLing.Api/Services/AI/Utils/SynonymsParser.cs new file mode 100644 index 0000000..3021cc1 --- /dev/null +++ b/backend/DramaLing.Api/Services/AI/Utils/SynonymsParser.cs @@ -0,0 +1,36 @@ +using System.Text.Json; + +namespace DramaLing.Api.Services.AI.Utils; + +/// +/// AI 生成同義詞的解析工具類 +/// +public static class SynonymsParser +{ + /// + /// 解析 AI 生成的同義詞 JSON 字串為字串陣列 + /// + /// JSON 格式的同義詞字串,如 ["word1", "word2"] + /// 解析後的同義詞陣列 + public static string[] ParseSynonymsJson(string? synonymsJson) + { + if (string.IsNullOrWhiteSpace(synonymsJson)) + return Array.Empty(); + + try + { + var synonyms = JsonSerializer.Deserialize(synonymsJson); + return synonyms ?? Array.Empty(); + } + catch (JsonException) + { + // JSON 解析失敗,返回空陣列 + return Array.Empty(); + } + catch (Exception) + { + // 其他異常,返回空陣列 + return Array.Empty(); + } + } +} \ No newline at end of file diff --git a/backend/DramaLing.Api/Services/Review/ReviewService.cs b/backend/DramaLing.Api/Services/Review/ReviewService.cs index 86b8cfc..064d479 100644 --- a/backend/DramaLing.Api/Services/Review/ReviewService.cs +++ b/backend/DramaLing.Api/Services/Review/ReviewService.cs @@ -5,6 +5,7 @@ using DramaLing.Api.Controllers; using DramaLing.Api.Utils; using DramaLing.Api.Services; using DramaLing.Api.Data; +using DramaLing.Api.Services.AI.Utils; namespace DramaLing.Api.Services.Review; @@ -62,8 +63,8 @@ public class ReviewService : IReviewService hasExampleImage = false, primaryImageUrl = (string?)null, - // 同義詞(暫時空陣列,未來可擴展) - synonyms = new string[] { }, + // 同義詞(從資料庫讀取,使用 AI 工具類解析) + synonyms = SynonymsParser.ParseSynonymsJson(item.Flashcard.Synonyms), // 測驗選項 (AI 生成的混淆選項) quizOptions = generatedQuizOptions, diff --git a/frontend/components/shared/Navigation.tsx b/frontend/components/shared/Navigation.tsx index da93262..6ac7696 100644 --- a/frontend/components/shared/Navigation.tsx +++ b/frontend/components/shared/Navigation.tsx @@ -132,7 +132,7 @@ export function Navigation({ showExitLearning = false, onExitLearning }: Navigat
{user?.username?.[0]?.toUpperCase() || 'U'}
- 👤 個人檔案 + {user?.username}