有没有一个C#工具可以匹配(句法解析)树中的模式?

11

我正在开发一个自然语言处理(NLP)项目,在其中使用句法分析器将给定句子创建成一个句法分析树。

示例输入:我遇到了Joe和Jill,然后我们去购物了
示例输出: [TOP [S [S [NP [PRP I]] [VP [VBD ran] [PP [IN into] [NP [NNP Joe] [CC and] [NNP Jill]]]]] [CC and] [S [ADVP [RB then]] [NP [PRP we]] [VP [VBD went] [NP [NN shopping]]]]]] enter image description here

我正在寻找一个C#实用工具,可以让我执行复杂的查询,例如:

  • 获取与“Joe”相关的第一个VBD
  • 获取最接近“Shopping”的NP

这里有一个Java实用程序可以做到这一点,我正在寻找一个C#等效实用程序。
任何帮助都将不胜感激。


2
给给出负评的人,你介意解释一下为什么吗?这个问题显然遵循了网站的常见问题FAQ。 - Benjamin Gruenbaum
2个回答

3

2
我们已经在使用。
一种选择是将输出解析为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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接