CloudBuilder/CloudBuilder.AI/Utility/ParagraphSplitter.cs
owenchen ce37c632d3 ow
2026-06-04 16:02:01 +08:00

83 lines
2.6 KiB
C#

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.AI.Utility
{
public class ParagraphSplitter
{
public ParagraphSplitter()
{
}
public void PackParagraphs(AiBookData data)
{
if (data.AiChapters == null || data.AiChapters.Length == 0) return;
var sentences = new List<AiSentenceEntity>();
foreach (AiChapterDisplayData aiChapterEntity in data.AiChapters)
{
SplitChapterIntoParagraphs(data, aiChapterEntity);
if (data.AiSentences != null && data.AiSentences.Length > 0)
sentences.AddRange(data.AiSentences);
}
data.AiSentences = sentences.ToArray();
}
public AiBookData SplitChapterIntoParagraphs(AiBookData data, AiChapterDisplayData aiChapter)
{
data.AiSentences = null;
var paragraphs = new List<AiSentenceEntity>();
var sentences = new List<AiSentenceEntity>();
var temps = new List<AiSentenceEntity>();
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();
AiSentenceEntity aiParagraph;
int index = 1;
foreach (var text in paragraphTexts)
{
if (!ValidText(text)) continue;
aiParagraph = new AiSentenceEntity();
aiParagraph.Content = text;
aiParagraph.BookId = aiChapter.BookId;
aiParagraph.ChapterId = aiChapter.ChapterId;
aiParagraph.ParagraphId = index++;
aiParagraph.DialogueIndc = sentenceSplitter.IsDialogue(text) ? YesNoPolicy.YES : YesNoPolicy.NO;
paragraphs.Add(aiParagraph);
temps = sentenceSplitter.SplitByConfigurableQuotes(aiParagraph);
if (temps != null && temps.Count() > 0) sentences.AddRange(temps);
}
data.AiSentences = sentences.ToArray();
return data;
}
private bool ValidText(string text)
{
if (string.IsNullOrWhiteSpace(text.Trim())) return false;
return true;
}
}
}