This commit is contained in:
owenchen committed 2026-05-29 16:33:25 +08:00
1 parent 527205520b
commit 91feed7cbb
4 files changed
+890 -48

No files matched your search

+123 -44
View File
@@ -26,50 +26,21 @@ namespace CloudBuilder.Topshelf.Utility
SpeakerAnalysisHelper speakerFeatureLearner = new SpeakerAnalysisHelper();
return GetPerson(hanlp, depRoot, ners, srls, cons);
}
List<string> ps = new List<string>();
public string GetPerson(Hanlp2 hanlp, string depRoot, List<NamedEntity> ners, List<List<MeaningRepresentationParsingEntity>> srls, List<HanlpConstituencyNode> cons)
{
string arg0 = string.Empty;
string arg1 = string.Empty;
if (!string.IsNullOrEmpty(depRoot) && srls != null && srls.Count() > 0)
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)
{
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 = GetPerson(hanlp, mrp.Text);
}
else
{
return mrp.Text;
}
}
}
foreach (var mrp in srl)
{
if (mrp.Type.ToUpper() != "ARG0") continue;
arg0 = mrp.Text.Length > 6 ? mrp.Text.Substring(mrp.Text.Length - 6, 6) : mrp.Text;
}
}
if (ners.Where(x => x.Text.Contains(person)).Any()) ps.Add(person);
}
//如果前面的不一样,那么加入人名
if (ners != null && ners.Where(x => x.Type.Contains("PERSON")).Count() > 0)
{
List<string> ps = new List<string>();
foreach (string namedEntity in ners.Where(x => x.Type.Contains("PERSON")).Select(x=>x.Text).Distinct().ToArray())
foreach (string namedEntity in ners.Where(x => x.Type.Contains("PERSON")).Select(x => x.Text).Distinct().ToArray())
{
if (namedEntity.Length > 1)
{
@@ -90,18 +61,126 @@ namespace CloudBuilder.Topshelf.Utility
foreach (HanlpConstituencyNode node in nodes)
{
nodef = HanlpResultHelper.GetNodeTopLabelById(cons, node.FatherId, nps);
if (nodef == null) continue;
feature = HanlpResultHelper.GetNodeBottomLabelById(cons, nodef.ItemId, nps);
if (string.IsNullOrEmpty(feature)) continue;
if (feature.Contains(namedEntity) && !ps.Contains(namedEntity)) ps.Add(feature);
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<NamedEntity> ners = hanlp.GetHanlpNer(text);
List<List<MeaningRepresentationParsingEntity>> srls = hanlp.GetHanlpSrl(text);
List<HanlpConstituencyNode> cons = hanlp.GetHanlpCon(text);
SpeakerAnalysisHelper speakerFeatureLearner = new SpeakerAnalysisHelper();
List<string> ps = new List<string>();
string arg1 = string.Empty;
return GetArg0(hanlp, depRoot, ners, srls, cons, ref arg1);
}
public string GetArg0(Hanlp2 hanlp, string depRoot, List<NamedEntity> ners, List<List<MeaningRepresentationParsingEntity>> srls, List<HanlpConstituencyNode> 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 QuotedString.ToDelimiteredList(ps.ToArray());
}
return arg0;
}