-->

是否有在(句法分析)树匹配模式的C#程序?(Is there a C# utility for ma

2019-08-16 17:53发布

我的工作中,我使用句法分析器创建句法分析树出给定的句子的自然语言处理(NLP)项目。

例如输入:我遇到了乔和吉尔,然后我们去购物
示例输出:[TOP [S [S [NP [PRP I]] [VP [VBD跑] [PP [IN变成] [NP [NNP乔] [CC和] [NNP吉尔]]]]] [CC和] [S [ADVP [RB然后] [NP [PRP我们] [VP [VBD去] [NP [NN购物]]]]]]

我在寻找一个C#程序,可以让我做这样复杂的查询:

  • 获取有关“乔”的第一个VBD
  • 获取NP最接近于“购物”

这里有一个Java实用程序做这个,我正在寻找一个C#等效。
任何帮助将非常感激。

Answer 1:

我们已经使用

一个选择是将解析输出到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);


Answer 2:

有至少两个NLP框架,即

  • SharpNLP (注:项目自2006年以来未激活)
  • Proxem

在这里,你可以找到指令.NET使用Java NLP:

  • 在.NET项目使用OpenNLP

这个网页是关于使用Java OpenNLP,但可以适用于你已经在您的文章中提到的java库

或者使用NLTK以下这个原则:

  • 在C#3.5的开源NLP使用NLTK


文章来源: Is there a C# utility for matching patterns in (syntactic parse) trees?