Search code examples
c#parsingnlpsharpnlp

Parsing a sentence with SharpNL & en-parser-chunking.bin


Using SharpNL and OpenNLP's en-parser-chunking.bin, I'm attempting to parse a sentence into a tree. One of SharpNL's tests shows that, given a model, you can parse a sentence as follows:

var model = SharpNL.Parser.TreeInsert.Parser.Train("en", parseSamples, headRules, 100, 0);

var parser = ParserFactory.Create(model);

// Tests parsing to make sure the code does not has
// a bug which fails always with a runtime exception
var p = parser.Parse(Parse.ParseParse("She was just another freighter from the " +
        "States and she seemed as commonplace as her name ."));

So I downloaded the en-parser-chunking.bin file, created a model from it as well as a parser and attempted to parse the same input:

var parserModelStream = new FileStream(@"en-parser-chunking.bin", FileMode.Open, FileAccess.Read);
var parserModel = new ParserModel(parserModelStream);
var parser = ParserFactory.Create(parserModel);

var p = parser.Parse(Parse.ParseParse("She was just another freighter from the " +
        "States and she seemed as commonplace as her name ."));

This code runs, but when I analyze p in the debugger, it has a Head of TOP and no children. Is this an issue with what model I'm using? Or how I'm using it?


Solution

  • Instead of this:

    var p = parser.Parse(Parse.ParseParse("..."));
    

    I simply needed to use this:

    var p = ParserTool.ParseLine("...", parser, 1);