CloudBuilder.Topshelf/Task/AI/ChineseNameExtractorTask.cs
owenchen 39494fb5c3 ow
2026-06-02 15:22:10 +08:00

117 lines
4.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<string, string> bodyDict)
{
if (!bodyDict.ContainsKey("book_id")) return;
string guid = bodyDict["book_id"];
PythonCommand python = new PythonCommand(service);
try
{
Hanlp2 hanlp = new Hanlp2(python);
IRepository<AiSentenceViewEntity> repository = service.GetRepository<IRepository<AiSentenceViewEntity>>();
IRepository<AiSentenceEntity> repositoryAiSentenceEntity = service.GetRepository<IRepository<AiSentenceEntity>>();
IRepository<AiChapterPersonEntity> repositoryAiChapterPersonEntity = service.GetRepository<IRepository<AiChapterPersonEntity>>();
string depRoot;
List<NamedEntity> ners;
List<List<MeaningRepresentationParsingEntity>> srls;
List<HanlpConstituencyNode> cons = null;
SpeakerAnalysisHelper speakerAnalysisHelper = 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; // 已处理行数计数器
AiChapterPersonEntity aiChapterPerson;
List<AiChapterPersonEntity> cps = new List<AiChapterPersonEntity>();
foreach (var ent in ents)
{
processedLines++;
person = speakerAnalysisHelper.GetPerson(hanlp, ent.Content);
paragraphIndex = ent.ParagraphId;
if (!string.IsNullOrEmpty(person))
{
string[] ps = person.Split(',');
int orderIndex = 1;
foreach (var p in ps)
{
if (p.Length == 1) continue;
aiChapterPerson = new AiChapterPersonEntity();
aiChapterPerson.PersonName = p;
aiChapterPerson.BookId = ent.BookId;
aiChapterPerson.ChapterId = ent.ChapterId;
aiChapterPerson.ParagraphIndex = ent.ParagraphId;
aiChapterPerson.SentenceIndex= ent.SentenceIndex;
aiChapterPerson.OrderIndex = orderIndex++;
cps.Add(aiChapterPerson);
repositoryAiChapterPersonEntity.InsertNow(aiChapterPerson);
}
}
UpdateProgress(processedLines, totalLines);
}
speakerAnalysisHelper.AiChapterPersons = cps.ToArray();
speakerAnalysisHelper.MatchPerson(ents, repositoryAiSentenceEntity);
}
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();
}
}
}