CloudBuilder/CloudBuilder.AI.Service/Utility/ParagraphSplitter.cs
owenchen 30c22a3d0c ow
2026-05-21 14:52:36 +08:00

89 lines
3.0 KiB
C#

using CloudBuilder.AI.Data;
using CloudBuilder.AI.Entity;
using DocumentFormat.OpenXml.Spreadsheet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CloudBuilder.AI.Service.Utility
{
public class ParagraphSplitter
{
public ParagraphSplitter()
{
}
public void PackParagraphs(AiBookData data)
{
if (data.AiChapters == null || data.AiChapters.Length == 0) return;
var paragraphs = new List<AiParagraphEntity>();
var sentences = new List<AiSentenceEntity>();
foreach (AiChapterEntity aiChapterEntity in data.AiChapters)
{
SplitChapterIntoParagraphs(data, aiChapterEntity);
if (data.AiParagraphs != null && data.AiParagraphs.Length > 0)
paragraphs.AddRange(data.AiParagraphs);
if (data.AiSentences != null && data.AiSentences.Length > 0)
sentences.AddRange(data.AiSentences);
}
data.AiParagraphs = paragraphs.ToArray();
data.AiSentences = sentences.ToArray();
}
public AiBookData SplitChapterIntoParagraphs(AiBookData data, AiChapterEntity aiChapter)
{
data.AiParagraphs = null;
data.AiSentences = null;
var paragraphs = new List<AiParagraphEntity>();
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();
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);
temps = sentenceSplitter.SplitByConfigurableQuotes(aiParagraph);
if (temps != null && temps.Count() > 0) sentences.AddRange(temps);
}
data.AiParagraphs = paragraphs.ToArray();
data.AiSentences = sentences.ToArray();
return data;
}
private bool ValidText(string text)
{
if (string.IsNullOrWhiteSpace(text.Trim())) return false;
return true;
}
}
}