是否有一个C#实用程序用于匹配(语法分析)树中的模式?
我正在开发一个自然语言处理(NLP)项目,我在其中使用语法分析器从给定的句子中创建一个语法分析树。
示例输入:我遇到乔和吉尔,然后我们去购物
示例输出: [TOP [S [S [NP [PRP I]] [VP [VBD运行] [PP [IN进入] [NP [NNP Joe] [CC和] [NNP Jill]]]]] [CC和] [S [ADVP [RB then]] [NP [PRP we]] [VP [VBD去] [NP [NN shopping]]]]]]
我正在寻找一个允许我执行复杂查询的C#实用程序:
- 获得与’Joe’相关的第一个VBD
- 让NP最接近’购物’
这是一个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);
这是你的第二个例子
//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);
至少有两个NLP框架,即
- SharpNLP (注:自2006年以来项目无效)
- Proxem
在这里,您可以找到在.NET中使用java NLP的说明:
- 在.NET项目中使用OpenNLP
这个页面是关于使用java OpenNLP,但可以应用于你在post中提到的java库
或者按照此指南使用NLTK:
- 使用NLTK在C#3.5中开源NLP