Gil*_*Gat 11 c# tree nlp stanford-nlp s-expression
我正在开发一个自然语言处理(NLP)项目,在该项目中,我使用语法分析器从给定的句子中创建一个语法分析树.
示例输入:我遇到Joe和Jill然后我们去购物
示例输出: [TOP [S [S [NP [PRP I]] [VP [VBD run] [PP [IN into] [NP [NNP Joe] [CC和[NNP Jill]]]]] [CC和] [S [ADVP [RB然后]] [NP [PRP我们]] [VP [VBD去] [NP [NN购物]]]]]]
我正在寻找一个允许我执行复杂查询的C#实用程序:
这是一个Java实用程序,我正在寻找一个C#等价物.
任何帮助将非常感激.
我们已经使用了
一种选择是将输出解析为 C# 代码,然后将其编码为 XML,使每个节点递归地放入所有子节点string.Format("<{0}>", this.Name);
并string.Format("</{0}>", this._name);
在中间放置。
完成此操作后,我将使用查询 XML/HTML 的工具来解析树。成千上万的人已经使用查询选择器和 jQuery 根据节点之间的关系来解析树状结构。我认为这远远优于 TRegex 或其他过时且未维护的 java 实用程序。
例如,这是回答你的第一个例子:
var xml = CQ.Create(d.ToXml());
//this can be simpler with CSS selectors but I chose Linq since you'll probably find it easier
//Find joe, in our case the node that has the text 'Joe'
var joe = xml["*"].First(x => x.InnerHTML.Equals("Joe"));
//Find the last (deepest) element that answers the critiria that it has "Joe" in it, and has a VBD in it
//in our case the VP
var closestToVbd = xml["*"].Last(x => x.Cq().Has(joe).Has("VBD").Any());
Console.WriteLine("Closest node to VPD:\n " +closestToVbd.OuterHTML);
//If we want the VBD itself we can just find the VBD in that element
Console.WriteLine("\n\n VBD itself is " + closestToVbd.Cq().Find("VBD")[0].OuterHTML);
Run Code Online (Sandbox Code Playgroud)
这是你的第二个例子
//Now for NP closest to 'Shopping', find the element with the text 'shopping' and find it's closest NP
var closest = xml["*"].First(x => x.InnerHTML.Equals("shopping")).Cq()
.Closest("NP")[0].OuterHTML;
Console.WriteLine("\n\n NP closest to shopping is: " + closest);
Run Code Online (Sandbox Code Playgroud)