using CloudBuilder.AI.Entity; using CloudBuilder.Topshelf.Python; using System; using System.Collections.Generic; using System.Diagnostics.Eventing.Reader; using System.Linq; using System.Reflection.Metadata; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using static System.Net.Mime.MediaTypeNames; namespace CloudBuilder.Topshelf.Utility { public class SpeakerAnalysisHelper { public string GetPerson(Hanlp2 hanlp, string text) { string depRoot = hanlp.GetDepRoot(text); List ners = hanlp.GetHanlpNer(text); List> srls = hanlp.GetHanlpSrl(text); List cons = hanlp.GetHanlpCon(text); SpeakerAnalysisHelper speakerFeatureLearner = new SpeakerAnalysisHelper(); List ps = new List(); string arg1 = string.Empty; string person = GetArg0(hanlp, depRoot, ners, srls, cons, ref arg1); //同边同时有的,放在最前面 if (!string.IsNullOrEmpty(person) && ners != null && ners.Where(x => x.Type.Contains("PERSON")).Count() > 0) { if (ners.Where(x => x.Text.Contains(person)).Any()) ps.Add(person); } //如果前面的不一样,那么加入人名 if (ners != null && ners.Where(x => x.Type.Contains("PERSON")).Count() > 0) { foreach (string namedEntity in ners.Where(x => x.Type.Contains("PERSON")).Select(x => x.Text).Distinct().ToArray()) { if (namedEntity.Length > 1) { if (!ps.Contains(namedEntity)) ps.Add(namedEntity); } else { HanlpConstituencyNode[] nodes; HanlpConstituencyNode nodef; string feature = null; nodes = HanlpResultHelper.GetNodeByLabel(cons, namedEntity); if (nodes == null || nodes.Length == 0) continue; string[] nps = new string[] { "NP", "DNP" }; //如果存在,找到父节点上的NP foreach (HanlpConstituencyNode node in nodes) { nodef = HanlpResultHelper.GetNodeTopLabelById(cons, node.FatherId, nps); if (nodef != null) { feature = HanlpResultHelper.GetNodeBottomLabelById(cons, nodef.ItemId, nps); if (string.IsNullOrEmpty(feature)) continue; } else if (string.IsNullOrEmpty(feature)) { feature = node.Label; } //如果下一个是“NP、DNP”,可以串起来,跟上面的GetNodeBottomLabelById类似 if (feature.Length == 1) { HanlpConstituencyNode[] nodesSon; nodef = HanlpResultHelper.GetNodeById(cons, node.ItemId + 1); if (nps.Contains(nodef.Label)) { nodesSon = HanlpResultHelper.GetNodeSonById(cons, nodef.ItemId); if (nodesSon != null && nodesSon.Where(x => x.Children == 0).Any()) { feature += string.Concat(nodesSon.Select(x => x.Label)); } } } if (feature.Length == 1) { var bs = HanlpResultHelper.GetNodeBrothersById(cons, node.ItemId); if (bs != null && bs.Where(x => x.Children == 0).Any()) { feature += string.Concat(bs.Select(x => x.Label)); } } if (feature.Contains(namedEntity) && !ps.Contains(feature)) ps.Add(feature); } } } } //如果主语==动作发出者 //if (!string.IsNullOrEmpty(person) && !ps.Contains(person) && !ps.Contains(string.Format("[{0}]", person))) ps.Add(string.Format("[{0}]", person)); //动作接受者 //if (!string.IsNullOrEmpty(arg1) && !ps.Contains(arg1)) ps.Add(arg1); person = QuotedString.ToDelimiteredList(ps.ToArray()); return person; } public string GetAgr0(Hanlp2 hanlp, string text) { string depRoot = hanlp.GetDepRoot(text); List ners = hanlp.GetHanlpNer(text); List> srls = hanlp.GetHanlpSrl(text); List cons = hanlp.GetHanlpCon(text); SpeakerAnalysisHelper speakerFeatureLearner = new SpeakerAnalysisHelper(); List ps = new List(); string arg1 = string.Empty; return GetArg0(hanlp, depRoot, ners, srls, cons, ref arg1); } public string GetArg0(Hanlp2 hanlp, string depRoot, List ners, List> srls, List cons, ref string arg1) { string arg0 = string.Empty; if (!string.IsNullOrEmpty(depRoot) && srls != null && srls.Count() > 0) { foreach (var srl in srls) { if (srl.Where(x => x.Text == depRoot).Any()) { foreach (var mrp in srl) { if (mrp.Type.ToUpper() != "ARG0") continue; if (ners != null && ners.Where(x => mrp.Text.Contains(x.Text)).Any() && ners.Where(x => mrp.Text.Contains(x.Text)).FirstOrDefault().Text.Length > 1) return ners.Where(x => mrp.Text.Contains(x.Text)).FirstOrDefault().Text; if (mrp.Text.Length > 4) { arg0 = GetAgr0(hanlp, mrp.Text); } else if (mrp.Text.Length > 1) { if (ners == null || ners.Where(x => x.Type.Contains("PERSON")).Count() == 0) return mrp.Text; } } } foreach (var mrp in srl) { if (mrp.Type.ToUpper() != "ARG0") continue; if (mrp.Text.Length > 4) { var temp = GetAgr0(hanlp, mrp.Text); if (!string.IsNullOrEmpty(temp)) { arg1 = temp; return arg0; } } else { arg1 = mrp.Text; } } } } return arg0; } //public AiSentenceEntity[] AiParagraphs { get; set; } public AiSentenceEntity[] AiSentences { get; set; } public AiChapterPersonEntity[] AiChapterPersons { get; set; } } } /* “文本解析→人物提取→上下文匹配→结果输出” graph TD A[对话行] --> B{规则1:文本特征匹配}; B -->|匹配成功| C[直接赋值说话者]; B -->|匹配失败| D{规则2:紧邻旁白匹配}; D -->|匹配成功| C; D -->|匹配失败| E{规则3:段落内最近人物}; E -->|匹配成功| C; E -->|匹配失败| F[赋值默认值+标记异常]; 规则 1:文本特征精准匹配(最高优先级) 基于对话内容中的 “专属特征” 直接匹配说话者,避免上下文干扰: 示例规则: 对话内容特征 匹配的说话者 包含 “第二元婴” 韩立 包含 “银月所化” 银月 包含 “本座 / 本尊” 反派角色 落地:维护一个 “特征 - 人物” 映射字典,匹配时优先使用: 规则 2:紧邻旁白匹配(次高优先级)只找对话行 “紧邻的前 1-2 行旁白”,而非整个段落的旁白,避免跨越多个人物导致匹配错误: 规则:仅检查对话行的前 1 行、前 2 行旁白(最多前 3 行),超过则停止; 落地:修改向前查找的逻辑,限制查找范围 规则 3:排除无效人物(避免干扰)从旁白中提取人物时,过滤掉 “非核心人物”(如 “店小二”“路人”),只保留主要人物: 落地:维护 “核心人物白名单”,提取人物时只保留白名单内的名称 3. 增加异常处理和人工校验(兜底保障) 即使逻辑再完善,也会有边缘场景匹配错误,需通过 “异常标记 + 人工复核” 兜底: 步骤 1:标记可疑匹配结果对以下情况标记为 “可疑”,存入日志或单独字段: 匹配到的人物在旁白中出现次数≥2(比如同一段落有韩立和银月,无法确定); 对话行前后 3 行都没有找到人物,最终赋值为 “未知人物”; 对话内容无任何特征,且段落内人物数量≥3; 落地:为实体增加扩展字段(如IsSuspected),或生成异常日志 步骤 2:人工复核可疑数据 规则:所有标记为 “可疑” 的匹配结果,必须经过人工复核后再更新到数据库; 落地:生成 “可疑数据清单”(包含Guid、Content、PersonName),供运营 / 编辑人员校验,修正错误后重新填充。 收集错误案例:记录每次匹配错误的场景(如 “银月的对话匹配给韩立”),分析错误原因; 迭代特征字典:将错误案例转化为新的特征规则(如新增 “乌云”→银月,“元婴”→韩立); A/B 测试:对优化后的规则做小范围测试,对比匹配准确率,确认有效后全量上线。 */