sergey-tihon / Stanford.NLP.NET

Stanford NLP for .NET
http://sergey-tihon.github.io/Stanford.NLP.NET/
MIT License
598 stars 123 forks source link

Object reference not set to an instance of an object issues (Dependency Parser) #46

Closed hendyharf closed 8 years ago

hendyharf commented 8 years ago

Currently I'm building a web application to extract the subjects, predicates, and objects of the sentences with Stanford CoreNLP (Dependency Parser). My solution so far contains two projects, NLP (Console Application) and the WebApp (C# ASP.NET-MVC EF) itself.

I have a View called Create that contains an input text box and a button.

...
@using (Html.BeginForm("Create", "Text", FormMethod.Post))
{
     @Html.AntiForgeryToken()
     @Html.ValidationSummary(true)

     @Html.TextBoxFor(Model => Model.Sentence, new)
     @Html.ValidationMessageFor(Model => Model.Sentence)

     <input type="submit" value="Find">
}
..

It will parse the sentence to the Create function inside the TextController. Then the sentence will be passed to a function FindPOS in a class named DependencyParser.cs in the NLP project.

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create (string Sentence)
{
     if (ModelState.IsValid)
     {
          DependencyParser dependencyParser = new DependencyParser();

          string[][] result;
          result = dependencyParser.FindPOS(Sentence);
     }
}

This FindPOS function will return an array of string of a processed sentence (Subject, Predicate, Object). This code is a purely from your code Stanford NLP Parser for .NET, so I just copy-paste it to this function.

public string[][] FindPOS(string Sentence)
{
     var jarRoot = @"stanford-corenlp-full-2015-12-09\models";
     var modelsDirectory = jarRoot + @"\edu\stanford\nlp\models";

     // Loading english PCFG parser from file
     var lp = LexicalizedParser.loadModel(modelsDirectory + @"\lexparser\englishPCFG.ser.gz");

     // This option shows loading and using an explicit tokenizer
     var tokenizerFactory = PTBTokenizer.factory(new CoreLabelTokenFactory(), "");
     var sentReader = new java.io.StringReader(Sentence);
     var rawWords = tokenizerFactory.getTokenizer(sentReader).tokenize();
     sentReader.close();
     var tree = lp.apply(rawWords);

     // Extract dependencies from lexical tree
     var tlp = new PennTreebankLanguagePack();
     var gsf = tlp.grammaticalStructureFactory();
     var gs = gsf.newGrammaticalStructure(tree);
     var tdl = gs.typedDependenciesCCprocessed();

     //.. my logic to find Subject Predicate Object below here ..//
}

After I compile the solution, then I input sentence to the textbox (i.e. "My mother told me not to drink too much sugar water") and click the Find button in the page Create View, I got this following error: "Object reference not set to an instance of an object" in the line var tree = lp.apply(rawWords);

integratingissue

Why is this happened? I've already installed the NuGet package to the NLP project "Stanford CoreNLP" and downloaded the database of the Stanford CoreNLP from the official website and put it in the NLP project /bin/Debug. I've also already Added the Reference of the NLP project to the WebApp project and write the using NLP; in the TextController and I've already Debug if the sentence is passed or not to the FindPOS function (It's passed).

I've already tried to make the NLP project as the startup project then compile it as a Console Application, It works very well, nothing problem. It executes the jarRoot in the directory file bin/Debug.

Why does the jarRoot not executed when compiling the solution as the WebApp startup project? Or is it something wrong with the .NET Framework version? Both projects are in the same version, 4.5. Are there any solutions for this?

Thank you.

sergey-tihon commented 8 years ago

I think that it is stack size issue:

Please try to switch to 64bit version of IISExpress - https://github.com/sergey-tihon/Stanford.NLP.NET/issues/43#issuecomment-190186401

Or deploy to real IIS - https://github.com/sergey-tihon/Stanford.NLP.NET/issues/15#issuecomment-77526331

hendyharf commented 8 years ago

I've just changed to 64 bit. I don't know if it's working or not, but I get another issue:

Could not load file or assembly 'slf4j-api, Version=1.7.2.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

error here: var lp = LexicalizedParser.loadModel(modelsDirectory + @"\lexparser\englishPCFG.ser.gz");

What's wrong?

sergey-tihon commented 8 years ago

Not sure about the reason, this dll is in NuGet package. What NuGet packages you referenced? Your code from the sample for Stanford.NLP.Parser but you use it with Stanford.NLP.CoreNLP. You definitely should not refernce both packages from NuGet.

hendyharf commented 8 years ago

I'm using only Stanford.NLP.CoreNLP. But anyway this is already working! But why it takes so much times to me, like 23seconds? A lot of exception A first chance exception of type 'java.lang.NoSuchFieldException' occurred in IKVM.OpenJDK.Core.dll and A first chance exception of type 'java.lang.NoSuchMethodException' occurred in IKVM.OpenJDK.Core.dll. Is there anything wrong with it?

sergey-tihon commented 8 years ago

Model initialization if long operation and you probably should not do it on each call.