using CloudBuilder.AI.Entity; using CloudBuilder.Core.DatabaseAccessor.Entity; using CloudBuilder.Core.DependencyInjection.Task; using CloudBuilder.Topshelf.Python; using CloudBuilder.Topshelf.Utility; using static System.Net.Mime.MediaTypeNames; namespace CloudBuilder.Topshelf.Task { //task:ChineseNameExtractorTask book_id:B000008 public class ChineseNameExtractorTask : IScheduleTask { private readonly IApplicationService service; public ChineseNameExtractorTask(IApplicationService service) { this.service = service; } public void Run(Dictionary bodyDict) { if (!bodyDict.ContainsKey("book_id")) return; string guid = bodyDict["book_id"]; PythonCommand python = new PythonCommand(service); try { Hanlp2 hanlp = new Hanlp2(python); IRepository repository = service.GetRepository>(); IRepository repositoryAiSentenceEntity = service.GetRepository>(); string depRoot; List ners; List> srls; List cons = null; SpeakerAnalysisHelper speakerFeatureLearner = new SpeakerAnalysisHelper(); AiSentenceViewEntity[] ents = repository.DetachedEntities.Where(x => x.BookId == guid).OrderBy(x => x.ChapterId).ThenBy(x => x.ParagraphId).ThenBy(x => x.SentenceIndex).ToArray(); if (ents == null || ents.Length == 0) return; AiSentenceEntity aiSentenceEntity; string person = null; long paragraphIndex = 0; int totalLines = ents.Length; int processedLines = 0; // 已处理行数计数器 foreach (var ent in ents) { processedLines++; person = speakerFeatureLearner.GetPerson(hanlp, ent.Content); paragraphIndex = ent.ParagraphId; if (!string.IsNullOrEmpty(person)) { aiSentenceEntity = repositoryAiSentenceEntity.Entities.Where(x => x.SentenceId == ent.SentenceId).FirstOrDefault(); aiSentenceEntity.PersonName = person; repositoryAiSentenceEntity.UpdateNow(aiSentenceEntity); } UpdateProgress(processedLines, totalLines); } } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { Console.WriteLine(""); python.Dispose(); } } private static void UpdateProgress(int processed, int total) { double progressPercent = (double)processed / total * 100; int progressBarLength = 50; // 进度条总长度 int filledLength = (int)(progressPercent / 100 * progressBarLength); // 构建进度条(如:[██████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░]) string progressBar = "[" + new string('█', filledLength) + new string('░', progressBarLength - filledLength) + "]"; string progressInfo = $"{progressBar} {progressPercent:F2}% | 已处理:{processed}/{total} "; Console.Write($"\r{progressInfo}"); Console.Out.Flush(); } } }