CloudBuilder.Topshelf/Task/Novel/ParagraphSplitterTask.cs
owenchen 597b88d075 ow
2026-05-28 15:49:25 +08:00

97 lines
3.6 KiB
C#

using CloudBuilder.AI.Data;
using CloudBuilder.AI.Entity;
using CloudBuilder.AI.Service;
using CloudBuilder.Core.DatabaseAccessor.Entity;
using CloudBuilder.Core.DependencyInjection.Task;
using CloudBuilder.Topshelf.Task.Novel.Process;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace CloudBuilder.Topshelf.Task.Novel
{
//task:ParagraphSplitterTask book_title:凡人修仙传
public class ParagraphSplitterTask : IScheduleTask
{
private readonly IApplicationService service;
public ParagraphSplitterTask(IApplicationService service)
{
this.service = service;
}
public void Run(Dictionary<string, string> bodyDict)
{
if (!bodyDict.ContainsKey("book_title") || string.IsNullOrEmpty(bodyDict["book_title"])) return;
string title = bodyDict["book_title"];
AiChapterEntity[] aiChapters = service.ServiceProvider.GetService<IAiChapterService>().FindByTitle(title);
if (aiChapters == null || aiChapters.Length == 0) return;
ParagraphSplitter paragraphSplitter = new ParagraphSplitter();
List<AiParagraphEntity> aiParagraphs = new List<AiParagraphEntity>();
List<AiSentenceEntity> aiSentences = new List<AiSentenceEntity>();
AiBookData data;
foreach (var cpt in aiChapters)
{
data = paragraphSplitter.SplitChapterIntoParagraphs(cpt);
if (data.AiParagraphs == null || data.AiParagraphs.Count() == 0) continue;
aiParagraphs.AddRange(data.AiParagraphs);
}
if (aiParagraphs == null || aiParagraphs.Count() == 0) return;
SentenceSplitter sentenceSplitter = new SentenceSplitter();
AiSentenceEntity aiSentence;
foreach (var ph in aiParagraphs)
{
if (ph.DialogueIndc != YesNoPolicy.YES)
{
aiSentence = new AiSentenceEntity();
aiSentence.Guid = Guid.NewGuid().ToString();
aiSentence.Content = ph.Content;
aiSentence.DialogueIndc = YesNoPolicy.NO;
aiSentence.SentenceIndex = 1;
aiSentence.ParagraphGuid = ph.Guid;
aiSentence.Status = "G";
//D-【待确认对话】p-【待确认人物】A-【等待分配角色】G-【生成中】C-【完成】
aiSentences.Add(aiSentence);
continue;
}
var s = sentenceSplitter.SplitByConfigurableQuotes(ph);
if (s == null || s.Count() == 0) continue;
aiSentences.AddRange(s);
}
var transactionOptions = new TransactionOptions
{
IsolationLevel = IsolationLevel.ReadCommitted,
Timeout = TimeSpan.FromSeconds(60 * 10)
};
using (TransactionScope _transactionScope = new TransactionScope(
TransactionScopeOption.Required,
transactionOptions,
TransactionScopeAsyncFlowOption.Enabled))
{
service.ServiceProvider.GetService<IAiParagraphService>().Inserts(aiParagraphs.ToArray());
service.ServiceProvider.GetService<IAiSentenceService>().Inserts(aiSentences.ToArray());
_transactionScope.Complete();
}
}
}
}