using CloudBuilder.AI.Entity; using CloudBuilder.AI.Utility; using CloudBuilder.Core.DatabaseAccessor.Entity; using CloudBuilder.Core.DependencyInjection.Task; using CloudBuilder.Topshelf.Python; using CloudBuilder.Topshelf.Utility; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CloudBuilder.Topshelf.Task.AI { //task:NameGenderPredictorTask book_id:B000010 from_chapter_id:177 to_chapter_id:200 public class NameGenderPredictorTask : IScheduleTask { private readonly IApplicationService service; public NameGenderPredictorTask(IApplicationService service) { this.service = service; } public void Run(Dictionary bodyDict) { try { if (!bodyDict.ContainsKey("book_id")) return; string bookId = bodyDict["book_id"]; string chapterId = string.Empty; if (bodyDict.ContainsKey("chapter_id")) chapterId = bodyDict["chapter_id"]; string from_chapter_id = string.Empty; if (bodyDict.ContainsKey("from_chapter_id")) from_chapter_id = bodyDict["from_chapter_id"]; string to_chapter_id = string.Empty; if (bodyDict.ContainsKey("to_chapter_id")) to_chapter_id = bodyDict["to_chapter_id"]; IRepository repositoryAiSentenceEntity = service.GetRepository>(); AiSentenceEntity[] aiSentences = repositoryAiSentenceEntity.DetachedEntities.Where(x => !string.IsNullOrEmpty(x.PersonName) && x.BookId == bookId && (string.IsNullOrEmpty(chapterId) || x.ChapterId == Convert.ToInt32(chapterId)) && (string.IsNullOrEmpty(from_chapter_id) || x.ChapterId >= Convert.ToInt32(from_chapter_id)) && (string.IsNullOrEmpty(to_chapter_id) || x.ChapterId <= Convert.ToInt32(to_chapter_id))).OrderBy(x => x.ChapterId).ThenBy(x => x.ParagraphId).ThenBy(x => x.SentenceIndex).ToArray(); if (aiSentences == null || aiSentences.Length == 0) return; int totalLines = aiSentences.Length; int processedLines = 0; // 已处理行数计数器 foreach (AiSentenceEntity aiSentence in aiSentences) { processedLines++; aiSentence.Gender = NameGenderPredictor.PredictGender(aiSentence.PersonName); ConsoleOutput.UpdateProgress(processedLines, totalLines); } repositoryAiSentenceEntity.UpdateNow(aiSentences); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } }