This commit is contained in:
owenchen committed 2026-06-04 16:02:01 +08:00
1 parent cb3fdca3e1
commit ce37c632d3
6 files changed
+89 -67

No files matched your search

+37 -18
View File
@@ -48,27 +48,44 @@ namespace CloudBuilder.AI.Utility
/// <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);
//}
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})";
// ✅ 修复:移除必须以标点结尾的限制,匹配任意引号内容
string regexPattern = $@"(?<left>{escapedLeftQuotes})(?<content>.+?)(?<right>{escapedRightQuotes})";
_dynamicQuotePattern = new Regex(regexPattern, RegexOptions.Compiled | RegexOptions.Singleline);
}
@@ -108,6 +125,7 @@ namespace CloudBuilder.AI.Utility
if (!string.IsNullOrEmpty(prefixText))
{
aiSentence = new AiSentenceEntity();
aiSentence.BookId = aiParagraphEntity.BookId;
aiSentence.ChapterId = aiParagraphEntity.ChapterId;
aiSentence.Content = prefixText;
aiSentence.DialogueIndc = YesNoPolicy.NO;
@@ -123,6 +141,7 @@ namespace CloudBuilder.AI.Utility
if (!string.IsNullOrEmpty(quotedContent))
{
aiSentence = new AiSentenceEntity();
aiSentence.BookId = aiParagraphEntity.BookId;
aiSentence.ChapterId = aiParagraphEntity.ChapterId;
aiSentence.Content = quotedContent;
aiSentence.DialogueIndc = YesNoPolicy.YES;
@@ -142,6 +161,7 @@ namespace CloudBuilder.AI.Utility
if (!string.IsNullOrEmpty(suffixText))
{
aiSentence = new AiSentenceEntity();
aiSentence.BookId = aiParagraphEntity.BookId;
aiSentence.ChapterId = aiParagraphEntity.ChapterId;
aiSentence.Content = suffixText;
aiSentence.DialogueIndc = YesNoPolicy.NO;
@@ -181,16 +201,15 @@ namespace CloudBuilder.AI.Utility
.Replace("’", "'");
// 3. 核心判断规则:
// - 包含成对/单个双引号(排除仅含引号无内容的情况)
// - 包含冒号+引号(如:"XXX说:"XXX"" 场景)
var hasQuote = Regex.IsMatch(cleanedText, @""".+?""") || // 成对双引号(含内容)
Regex.IsMatch(cleanedText, @":\s*"""); // 冒号后接引号(说话人+对话)
var hasSingleQuote = Regex.IsMatch(cleanedText, @"'[^']+'"); // 成对单引号(英文对话)
var hasQuote = Regex.IsMatch(cleanedText, @""".+?""") ||
Regex.IsMatch(cleanedText, @"[::,,]\s*"""); // 冒号/逗号+引号(说话标记)
// 4. 排除仅含引号符号的无效情况(如 ""、''、“”)
var onlyQuote = Regex.IsMatch(cleanedText, @"^\s*[""''“”]{1,2}\s*$");
var hasSingleQuote = Regex.IsMatch(cleanedText, @"'[^']+'");
// 最终判断:包含有效引号且不是仅含引号 → 判定为对话
// 4. 排除仅含引号符号的无效情况(修复:只匹配替换后的符号)
var onlyQuote = Regex.IsMatch(cleanedText, @"^\s*[""']{1,2}\s*$");
// 最终判断
return (hasQuote || hasSingleQuote) && !onlyQuote;
}
}