Is there a C# utility for matching patterns in (sy

2019-04-05 02:35发布

I'm working on a Natural Language Processing (NLP) project in which I use a syntactic parser to create a syntactic parse tree out of a given sentence.

Example Input: I ran into Joe and Jill and then we went shopping
Example Output: [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

I'm looking for a C# utility that will let me do complex queries like:

  • Get the first VBD related to 'Joe'
  • Get the NP closest to 'Shopping'

Here's a Java utility that does this, I'm looking for a C# equivalent.
Any help would be much appreciated.

2条回答
再贱就再见
2楼-- · 2019-04-05 03:18

We already use

One option would be to parse the output into C# code and then encoding it to XML making every node into string.Format("<{0}>", this.Name); and string.Format("</{0}>", this._name); in the middle put all the child nodes recursively.

After you do this, I would use a tool for querying XML/HTML to parse the tree. Thousands of people already use query selectors and jQuery to parse tree-like structure based on the relation between nodes. I think this is far superior to TRegex or other outdated and un-maintained java utilities.

For example, this is to answer your first example:

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);

Here is your second example

//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);
查看更多
别忘想泡老子
3楼-- · 2019-04-05 03:24

There are at least two NLP frameworks, i.e.

And here you can find instructions to use a java NLP in .NET:

This page is about using java OpenNLP, but could apply to the java library you've mentioned in your post

Or use NLTK following this guidelines:

查看更多
登录 后发表回答