107 lines
4.7 KiB
C#
107 lines
4.7 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;
|
|
using static System.Runtime.InteropServices.JavaScript.JSType;
|
|
|
|
namespace CloudBuilder.Topshelf.Task.AI
|
|
{
|
|
//task:NameGenderPredictorTask book_id:B000011 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);
|
|
}
|
|
|
|
string[] personNames = aiSentences.Select(x => x.PersonName).Distinct().ToArray();
|
|
if (personNames != null && personNames.Length > 0)
|
|
{
|
|
AiBookPersonEntity aiBookPersonEntity;
|
|
IRepository<AiBookPersonEntity> repositoryAiBookPersonEntity = service.GetRepository<IRepository<AiBookPersonEntity>>();
|
|
|
|
AiBookPersonEntity[] bps = repositoryAiBookPersonEntity.DetachedEntities.Where(x => x.BookId == aiSentences[0].BookId).ToArray();
|
|
|
|
if (bps != null && bps.Length > 0)
|
|
{
|
|
repositoryAiBookPersonEntity.DeleteNow(bps);
|
|
}
|
|
|
|
List<AiBookPersonEntity> ps = new List<AiBookPersonEntity>();
|
|
//旁白
|
|
aiBookPersonEntity = new AiBookPersonEntity();
|
|
aiBookPersonEntity.BookId = aiSentences[0].BookId;
|
|
aiBookPersonEntity.PersonName = null;
|
|
aiBookPersonEntity.ActorName = SoVITSTtsVoiceRandom.GetRandomByGender("女性").ToString();
|
|
ps.Add(aiBookPersonEntity);
|
|
|
|
foreach (var personName in personNames)
|
|
{
|
|
if (string.IsNullOrEmpty(personName)) continue;
|
|
aiBookPersonEntity = new AiBookPersonEntity();
|
|
aiBookPersonEntity.BookId = aiSentences[0].BookId;
|
|
aiBookPersonEntity.PersonName = personName;
|
|
aiBookPersonEntity.ActorName = SoVITSTtsVoiceRandom.GetRandomByGender(NameGenderPredictor.IsMale(personName) == true ? "男性" : "女性").ToString();
|
|
|
|
ps.Add(aiBookPersonEntity);
|
|
}
|
|
|
|
repositoryAiBookPersonEntity.InsertNow(ps);
|
|
}
|
|
|
|
repositoryAiSentenceEntity.UpdateNow(aiSentences);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
}
|
|
}
|
|
}
|
|
}
|