【问题标题】:Is there a C# utility for matching patterns in (syntactic parse) trees?是否有用于匹配(句法解析)树中的模式的 C# 实用程序?
【发布时间】:2013-02-03 15:11:05
【问题描述】:

我正在从事一个自然语言处理 (NLP) 项目,在该项目中我使用句法分析器从给定的句子中创建句法分析树。

示例输入:我遇到了 Joe 和 Jill,然后我们去购物
示例输出: [顶部 [S [S [NP [PRP I]] [VP [VBD 跑] [PP [IN 进入] [NP [NNP 乔] [CC 和] [NNP 吉尔]]]]] [CC 和] [S [ADVP [RB then]] [NP [PRP we]] [VP [VBD go] [NP [NN shopping]]]]]]

我正在寻找可以让我执行复杂查询的 C# 实用程序,例如:

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

这是一个 Java utility,我正在寻找一个 C# 等价物。
任何帮助将不胜感激。

【问题讨论】:

  • 反对投票者,您介意解释一下原因吗?此问题明确遵守网站常见问题解答。

标签: c# tree nlp stanford-nlp s-expression


【解决方案1】:

至少有两个 NLP 框架,即

  • SharpNLP(注意:项目自 2006 年起处于非活动状态)
  • Proxem

您可以在这里找到在 .NET 中使用 java NLP 的说明:

此页面是关于使用 java OpenNLP,但可以适用于您在帖子中提到的 java 库

或按照以下准则使用 NLTK:

【讨论】:

    【解决方案2】:

    我们已经使用了

    一种选择是parse the output into C# code,然后将其编码为XML,使每个节点成为string.Format("<{0}>", this.Name);string.Format("</{0}>", this._name);在中间递归地放置所有子节点。

    完成此操作后,我将使用 a tool for querying 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);
    

    【讨论】:

      猜你喜欢
      • 2010-11-27
      • 2011-08-20
      • 1970-01-01
      • 2018-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多