103 lines
3.9 KiB
C#
103 lines
3.9 KiB
C#
using CloudBuilder.AI.Entity;
|
||
using CloudBuilder.Core.DatabaseAccessor.Entity;
|
||
using CloudBuilder.Core.DependencyInjection.Task;
|
||
using CloudBuilder.Topshelf.Python;
|
||
using CloudBuilder.Topshelf.Task.Novel.Process;
|
||
using static System.Net.Mime.MediaTypeNames;
|
||
|
||
namespace CloudBuilder.Topshelf.Task
|
||
{
|
||
//task:ChineseNameExtractorTask guid:B000006
|
||
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("guid")) return;
|
||
|
||
string guid = bodyDict["guid"];
|
||
|
||
PythonCommand python = new PythonCommand(service);
|
||
try
|
||
{
|
||
Hanlp2 hanlp = new Hanlp2(python);
|
||
|
||
IRepository<AiSentenceDialogueViewEntity> repository = service.GetRepository<IRepository<AiSentenceDialogueViewEntity>>();
|
||
|
||
IRepository<AiSentenceEntity> repositoryAiSentenceEntity = service.GetRepository<IRepository<AiSentenceEntity>>();
|
||
|
||
string depRoot;
|
||
|
||
List<NamedEntity> ners;
|
||
|
||
List<List<MeaningRepresentationParsingEntity>> srls;
|
||
|
||
List<HanlpConstituencyNode> cons = null;
|
||
|
||
SpeakerAnalysisHelper speakerFeatureLearner = new SpeakerAnalysisHelper();
|
||
|
||
AiSentenceDialogueViewEntity[] ents = repository.DetachedEntities.Where(x => x.BookGuid == guid).OrderBy(x => x.ChapterIndex).ThenBy(x => x.ParagraphIndex).ThenBy(x => x.DialogueIndc).ToArray();
|
||
AiSentenceEntity aiSentenceEntity;
|
||
string person = null;
|
||
int paragraphIndex = 0;
|
||
|
||
int totalLines = ents.Length;
|
||
int processedLines = 0; // 已处理行数计数器
|
||
|
||
foreach (var ent in ents)
|
||
{
|
||
processedLines++;
|
||
|
||
if (!string.IsNullOrEmpty(person) && paragraphIndex == ent.ParagraphIndex)
|
||
{
|
||
paragraphIndex = 0;
|
||
aiSentenceEntity = repositoryAiSentenceEntity.Entities.Where(x => x.Guid == ent.Guid).FirstOrDefault();
|
||
aiSentenceEntity.PersonName = person;
|
||
|
||
repositoryAiSentenceEntity.UpdateNow(aiSentenceEntity);
|
||
continue;
|
||
}
|
||
|
||
depRoot = hanlp.GetDepRoot(ent.Content);
|
||
ners = hanlp.GetHanlpNer(ent.Content);
|
||
srls = hanlp.GetHanlpSrl(ent.Content);
|
||
|
||
person = speakerFeatureLearner.GetPerson(depRoot, ners, srls, cons);
|
||
paragraphIndex = ent.ParagraphIndex;
|
||
|
||
UpdateProgress(processedLines, totalLines);
|
||
}
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Console.WriteLine(ex.Message);
|
||
}
|
||
finally
|
||
{
|
||
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();
|
||
}
|
||
}
|
||
}
|