CloudBuilder.Topshelf/Task/AI/NameGenderPredictorTask.cs
owenchen a8789b2e42 ow
2026-06-05 08:12:54 +08:00

71 lines
2.8 KiB
C#

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<string, string> 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<AiSentenceEntity> repositoryAiSentenceEntity = service.GetRepository<IRepository<AiSentenceEntity>>();
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);
}
}
}
}