using CloudBuilder.AI.Data; using CloudBuilder.AI.Entity; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CloudBuilder.Topshelf.Task.Novel.Process { public class ParagraphSplitter { public ParagraphSplitter() { } public AiBookData SplitChapterIntoParagraphs(AiChapterEntity aiChapter) { AiBookData data = new AiBookData(); var paragraphs = new List(); var sentences = new List(); if (string.IsNullOrWhiteSpace(aiChapter.Content)) return data; // 按换行拆分段落(过滤空段落) var paragraphTexts = aiChapter.Content.Split("\r\n") .Select(p => p.Trim()) .Where(p => !string.IsNullOrWhiteSpace(p)) .ToList(); SentenceSplitter sentenceSplitter = new SentenceSplitter(); AiParagraphEntity aiParagraph; int index = 1; foreach (var text in paragraphTexts) { if (!ValidText(text)) continue; aiParagraph = new AiParagraphEntity(); aiParagraph.Content = text; aiParagraph.ChapterGuid = aiChapter.Guid; aiParagraph.Guid = Guid.NewGuid().ToString(); aiParagraph.ParagraphIndex = index++; aiParagraph.DialogueIndc = sentenceSplitter.IsDialogue(text) ? YesNoPolicy.YES : YesNoPolicy.NO; paragraphs.Add(aiParagraph); } data.AiParagraphs = paragraphs.ToArray(); data.AiSentences = sentences.ToArray(); return data; } private bool ValidText(string text) { if (string.IsNullOrWhiteSpace(text.Trim())) return false; return true; } } }