This commit is contained in:
owenchen committed 2026-05-21 14:52:36 +08:00
1 parent 2697285ce7
commit 30c22a3d0c
73 files changed
+7117

No files matched your search

@@ -0,0 +1,154 @@
using CloudBuilder.AI.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace CloudBuilder.AI.Service.Utility
{
/// <summary>
/// 章节分割器
/// 不同的小说有自己的分割方法,可以传入参数
/// </summary>
public class ChapterSplitter
{
public ChapterSplitter()
{
}
/// <summary>
/// 按章节分割小说文本
/// </summary>
public List<AiChapterEntity> SplitNovelIntoChapters(string novelText, string chapterPatterns)
{
var chapters = new List<AiChapterEntity>();
var lines = novelText.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
var currentAiChapterEntity = new StringBuilder();
var currentAiChapterEntityTitle = "未命名章节";
var chapterCount = 1;
foreach (var line in lines)
{
// 检查是否是章节标题
if (IsChapterTitle(line, chapterPatterns))
{
// 如果当前章节有内容,则保存
if (currentAiChapterEntity.Length > 0)
{
chapters.Add(new AiChapterEntity
{
Title = currentAiChapterEntityTitle,
Guid = Guid.NewGuid().ToString(),
Content = currentAiChapterEntity.ToString(),
ChapterIndex = chapterCount
});
currentAiChapterEntity.Clear();
chapterCount++;
}
// 更新章节标题
currentAiChapterEntityTitle = line.Trim();
// 添加章节标题
currentAiChapterEntity.AppendLine(line.Trim());
// 添加一个空行作为分隔,确保章节标题和正文内容分离
currentAiChapterEntity.AppendLine();
}
else
{
// 添加到当前章节,确保每行独立
currentAiChapterEntity.AppendLine(line);
}
}
// 添加最后一个章节
if (currentAiChapterEntity.Length > 0)
{
chapters.Add(new AiChapterEntity
{
Title = currentAiChapterEntityTitle,
Content = currentAiChapterEntity.ToString(),
Guid = Guid.NewGuid().ToString(),
ChapterIndex = chapterCount
});
}
return chapters;
}
/// <summary>
/// 检查是否为章节标题
/// </summary>
private bool IsChapterTitle(string line, string cptPattern)
{
// 去除首尾空白字符
var trimmedLine = line.Trim();
// 常见的章节标题模式
var chapterPatterns = new[]
{
@"^第\s*[\u4e00-\u9fa50-9]+\s*章", // 第X章, 第XX章等
@"^第\s*[0-9]+\s*章", // 第1章, 第2章等
};
if (!string.IsNullOrEmpty(cptPattern))
{
Array.Resize(ref chapterPatterns, chapterPatterns.Length + 1);
chapterPatterns[chapterPatterns.Length - 1] = cptPattern;
}
foreach (var pattern in chapterPatterns)
{
if (Regex.IsMatch(trimmedLine, pattern, RegexOptions.IgnoreCase))
{
//第一章内容说明了什么,请描述一下:
if (trimmedLine.Length > 20) return false;
return true;
}
}
return false;
}
/// <summary>
/// 保存章节到单独的文件
/// </summary>
public void SaveChaptersToFiles(List<AiChapterEntity> chapters, string outputDirectory)
{
// 确保输出目录存在
if (!Directory.Exists(outputDirectory))
{
Directory.CreateDirectory(outputDirectory);
}
foreach (var chapter in chapters)
{
// 使用章节标题作为文件名,清理非法字符
var fileName = CleanFileName(chapter.Title) + ".txt";
var filePath = Path.Combine(outputDirectory, fileName);
File.WriteAllText(filePath, chapter.Content, Encoding.UTF8);
}
}
/// <summary>
/// 清理文件名中的非法字符
/// </summary>
private string CleanFileName(string fileName)
{
var invalidChars = Path.GetInvalidFileNameChars();
foreach (var invalidChar in invalidChars)
{
fileName = fileName.Replace(invalidChar, '_');
}
// 限制文件名长度
if (fileName.Length > 100)
{
fileName = fileName.Substring(0, 100);
}
return fileName;
}
}
}
@@ -0,0 +1,88 @@
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;
}
}
}
@@ -0,0 +1,197 @@
using CloudBuilder.AI.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace CloudBuilder.AI.Service.Utility
{
/// <summary>
/// 智能句子分割器
/// </summary>
public class SentenceSplitter
{
// 成对引号配置数组(可自由扩展,格式:[左引号, 右引号])
private readonly List<(string LeftQuote, string RightQuote)> _quotePairs;
// 动态生成的正则表达式(匹配所有配置的引号类型)
private Regex _dynamicQuotePattern;
// 引号内必须包含的结尾标点集合
private readonly string _endPunctuations = "?!。,;:、……~·";
/// <summary>
/// 默认构造函数:初始化常用成对引号(中文、英文、直角引号)
/// </summary>
public SentenceSplitter()
{
_quotePairs = new List<(string, string)>
{
("“", "”"), // 中文弯引号
("\"", "\""), // 英文双引号(需转义)
("〝", "〞"), // 中文直角引号
("「", "」") // 中文方头引号(可选扩展)
};
InitDynamicRegex();
}
/// <summary>
/// 自定义构造函数:允许外部传入任意成对引号配置
/// </summary>
/// <param name="customQuotePairs">自定义成对引号列表</param>
public SentenceSplitter(List<(string LeftQuote, string RightQuote)> customQuotePairs)
{
_quotePairs = customQuotePairs ?? new List<(string, string)>();
InitDynamicRegex();
}
/// <summary>
/// 初始化动态正则:根据配置的成对引号生成匹配规则
/// </summary>
private void InitDynamicRegex()
{
if (_quotePairs.Count == 0)
{
// 空配置时生成空正则,避免匹配任何内容
_dynamicQuotePattern = new Regex(@"^$", RegexOptions.Compiled);
return;
}
// 步骤1:转义所有引号(避免正则特殊字符冲突),拼接左/右引号匹配组
string escapedLeftQuotes = string.Join("|", _quotePairs.Select(p => Regex.Escape(p.LeftQuote)));
string escapedRightQuotes = string.Join("|", _quotePairs.Select(p => Regex.Escape(p.RightQuote)));
// 步骤2:转义标点集合,避免正则解析错误
string escapedPunctuations = Regex.Escape(_endPunctuations);
// 步骤3:构建动态正则表达式
// 分组说明:
// ?<left>:匹配配置的任意左引号
// ?<content>:匹配引号内的内容(必须以指定标点结尾)
// ?<right>:匹配配置的任意右引号
string regexPattern = $@"(?<left>{escapedLeftQuotes})(?<content>.+?[{escapedPunctuations}])(?<right>{escapedRightQuotes})";
_dynamicQuotePattern = new Regex(regexPattern, RegexOptions.Compiled | RegexOptions.Singleline);
}
/// <summary>
/// 核心分割方法:按配置的成对引号分割文本,结果无引号字符
/// </summary>
/// <param name="input">待分割的文本(如:“其他标点符号,如?”紫茜的秀目 或 "其他标点符号,如?"紫茜的秀目)</param>
/// <returns>分割后的纯文本列表</returns>
public List<AiSentenceEntity> SplitByConfigurableQuotes(AiParagraphEntity aiParagraphEntity)
{
AiSentenceEntity aiSentence;
int sentenceIndex = 1;
// 空值/空白校验
if (string.IsNullOrWhiteSpace(aiParagraphEntity.Content))
{
return new List<AiSentenceEntity>();
}
List<AiSentenceEntity> result = new List<AiSentenceEntity>();
string remainingText = aiParagraphEntity.Content.Trim();
int lastMatchEnd = 0;
// 遍历所有匹配的「引号+内容+标点+引号」文本块
foreach (Match match in _dynamicQuotePattern.Matches(remainingText))
{
// 1. 提取匹配的左/右引号,校验是否为成对配置
string matchedLeft = match.Groups["left"].Value;
string matchedRight = match.Groups["right"].Value;
bool isPairMatched = _quotePairs.Any(p => p.LeftQuote == matchedLeft && p.RightQuote == matchedRight);
if (!isPairMatched)
{
continue; // 非成对引号,跳过该匹配(避免“xxx"这类混合匹配)
}
// 2. 提取引号前的文本(过滤空值)
string prefixText = remainingText.Substring(lastMatchEnd, match.Index - lastMatchEnd).Trim();
if (!string.IsNullOrEmpty(prefixText))
{
aiSentence = new AiSentenceEntity();
aiSentence.Guid = Guid.NewGuid().ToString();
aiSentence.Content = prefixText;
aiSentence.DialogueIndc = YesNoPolicy.NO;
aiSentence.SentenceIndex = sentenceIndex++;
aiSentence.ParagraphGuid = aiParagraphEntity.Guid;
aiSentence.Status = "G";
//D-【待确认对话】p-【待确认人物】A-【等待分配角色】G-【生成中】C-【完成】
result.Add(aiSentence);
}
// 3. 提取引号内的纯内容(无引号字符)
string quotedContent = match.Groups["content"].Value.Trim();
if (!string.IsNullOrEmpty(quotedContent))
{
aiSentence = new AiSentenceEntity();
aiSentence.Guid = Guid.NewGuid().ToString();
aiSentence.Content = quotedContent;
aiSentence.DialogueIndc = YesNoPolicy.YES;
aiSentence.SentenceIndex = sentenceIndex++;
aiSentence.ParagraphGuid = aiParagraphEntity.Guid;
aiSentence.Status = "P";
//D-【待确认对话】p-【待确认人物】A-【等待分配角色】G-【生成中】C-【完成】
result.Add(aiSentence);
}
// 更新最后匹配位置
lastMatchEnd = match.Index + match.Length;
}
// 4. 提取最后一个匹配项后的剩余文本
string suffixText = remainingText.Substring(lastMatchEnd).Trim();
if (!string.IsNullOrEmpty(suffixText))
{
aiSentence = new AiSentenceEntity();
aiSentence.Guid = Guid.NewGuid().ToString();
aiSentence.Content = suffixText;
aiSentence.DialogueIndc = YesNoPolicy.NO;
aiSentence.SentenceIndex = sentenceIndex++;
aiSentence.ParagraphGuid = aiParagraphEntity.Guid;
aiSentence.Status = "G";
//D-【待确认对话】p-【待确认人物】A-【等待分配角色】G-【生成中】C-【完成】
result.Add(aiSentence);
}
return result;
}
/// <summary>
/// 新增:判断文本是否包含对话内容
/// </summary>
/// <param name="text">待判断的文本(句子/段落)</param>
/// <returns>true=包含对话,false=纯旁白/空文本</returns>
public bool IsDialogue(string text)
{
// 1. 处理空文本/NULL,直接返回false
if (string.IsNullOrWhiteSpace(text))
return false;
// 2. 文本预处理:去空格、统一全角/半角引号
string cleanedText = text.Trim();
// 替换全角空格为半角,避免干扰判断
cleanedText = Regex.Replace(cleanedText, @" ", " ");
// 统一引号格式(中文弯引号、直引号 → 标准双引号)
cleanedText = cleanedText.Replace("“", "\"")
.Replace("”", "\"")
.Replace("〝", "\"")
.Replace("〞", "\"")
.Replace("「", "\"")
.Replace("」", "\"")
.Replace("‘", "'")
.Replace("’", "'");
// 3. 核心判断规则:
// - 包含成对/单个双引号(排除仅含引号无内容的情况)
// - 包含冒号+引号(如:"XXX说:"XXX"" 场景)
var hasQuote = Regex.IsMatch(cleanedText, @""".+?""") || // 成对双引号(含内容)
Regex.IsMatch(cleanedText, @":\s*"""); // 冒号后接引号(说话人+对话)
var hasSingleQuote = Regex.IsMatch(cleanedText, @"'[^']+'"); // 成对单引号(英文对话)
// 4. 排除仅含引号符号的无效情况(如 ""、''、“”)
var onlyQuote = Regex.IsMatch(cleanedText, @"^\s*[""''“”]{1,2}\s*$");
// 最终判断:包含有效引号且不是仅含引号 → 判定为对话
return (hasQuote || hasSingleQuote) && !onlyQuote;
}
}
}