Search code examples
c#stanford-nlpnuget-package

Unrecoverable error while loading a tagger model using nugetpackages


I have been trying to create and run a simple program using the stanford-corenlp-3.5.2 nugetpackage.

However after looking up some beginner code to start I have found the following code props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");(Link to code example : http://sergey-tihon.github.io/Stanford.NLP.NET/StanfordCoreNLP.html)

But whenever the console app loads pos it fires off a runtime error stating that it could not load a tagger.

I am wondering if i am missing any nugetpackages or if there is additional setup I have to go through. (Note. any time i have tried to add say the postagger nuget package i then get an error saying that the class Annotation is referenced in two dlls.)

I have found that if i remove some of the properties the application will run correctly so the new line looks like this "props.setProperty("annotators", "tokenize, ssplit");

Any help to get past the runtime error so I can continue further analyse of the sample text would be greatly appreciated. Thank You.

Attached picture for reference.(apparently I need more reputation in order to post a pic but when I can I will do so immediately :) Edit I have added the picture now :)

stack trace at line exception is as follows:

at edu.stanford.nlp.pipeline.AnnotatorFactories.4.create()
at edu.stanford.nlp.pipeline.AnnotatorPool.get(String name)
at edu.stanford.nlp.pipeline.StanfordCoreNLP.construct(Properties , Boolean , AnnotatorImplementations )
at edu.stanford.nlp.pipeline.StanfordCoreNLP..ctor(Properties props, Boolean enforceRequirements)
at edu.stanford.nlp.pipeline.StanfordCoreNLP..ctor(Properties props)
at ConApplicationSabrinaNLP.TestClass.donlp() in c:\Users\Justin\Documents\Visual Studio 2013\Projects\ConApplicationSabrinaNLP\ConApplicationSabrinaNLP\TestClass.cs:line 28
at ConApplicationSabrinaNLP.Program.Main(String[] args) in c:\Users\Justin\Documents\Visual Studio 2013\Projects\ConApplicationSabrinaNLP\ConApplicationSabrinaNLP\Program.cs:line 20
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

Solution

  • The problem is that you haven't downloaded the models. The Nuget package doesn't work by itself. You must download lastest version here POS-Tagger

    Once you have done that put it in your project directory. Then point the code to it. In the "3.6.0 2015-12-09 Updated for compatibility" version the tagger has a different name, "english-bidirectional-distsim.tagger" for example. Make sure you point the code to the correct folder and files and it will work.

    The following is a working example from my windows forms project.

    using java.io;
    using java.util;
    using edu.stanford.nlp.ling;
    using edu.stanford.nlp.tagger.maxent;
    using Console = System.Console;
    using System.IO;
    using System.Windows.Forms;
    
    namespace Stanford.NLP.POSTagger.CSharp
    {
        class PosTagger
    
        {
            // get the base folder for the project
            public static string GetAppFolder()
            {
                return Path.GetDirectoryName(Application.ExecutablePath).Replace(@"*your project directory here*\bin\Debug", string.Empty);
            }
    
            public void testTagger()
            {
                var jarRoot = Path.Combine(GetAppFolder(), @"packages\stanford-postagger-2015-12-09");
                Console.WriteLine(jarRoot.ToString());
                var modelsDirectory = jarRoot + @"\models";
    
                // Loading POS Tagger
                var tagger = new MaxentTagger(modelsDirectory + @"\english-bidirectional-distsim.tagger");
    
                // Text for tagging
                var text = "A Part-Of-Speech Tagger (POS Tagger) is a piece of software that reads text"
                           + "in some language and assigns parts of speech to each word (and other token),"
                           + " such as noun, verb, adjective, etc., although generally computational "
                           + "applications use more fine-grained POS tags like 'noun-plural'.";
    
                var sentences = MaxentTagger.tokenizeText(new java.io.StringReader(text)).toArray();
                foreach (ArrayList sentence in sentences)
                {
                    var taggedSentence = tagger.tagSentence(sentence);
                    Console.WriteLine(Sentence.listToString(taggedSentence, false));
                }
            }
        }
    }