sergey-tihon / Stanford.NLP.NET

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

An unhandled exception of type 'System.TypeInitializationException' occurred #92

Closed dheeraj2016 closed 5 years ago

dheeraj2016 commented 5 years ago

HI Team,

sorry to report a same bug which has been closed before "Bug#23" i think my situation is little different. Environment : .NET Core Web API 2.1 Language : C# OS : Windows 10 pro IDE: Visual Studio 2017 CoreNLP: 3.7.0

User case : Trying to initialize CoreNLP from static main of program.cs using singleton pattern . it gives me above mentioned error.

but if run same class from Console application it run fine for 9 times out of 10, for remaining one it just crashes with giving any error. My code is as below

bool isInitiliazed = false; if (!string.IsNullOrWhiteSpace(pathToModelFolder)) { string currentPath = string.Empty; try { logger.Info("CoreNLP initialisation started"); // Path to the folder with models extracted from stanford-corenlp-3.7.0-models.jar // var jarRoot = @"........\data\paket-files\nlp.stanford.edu\stanford-corenlp-full-2016-10-31\models"; var jarRoot = pathToModelFolder; // Text for processing

                // Annotation pipeline configuration
                var props = new Properties();
                props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
                props.setProperty("ner.useSUTime", "false");

                // We should change current directory, so StanfordCoreNLP could find all the model files automatically
                currentPath = Environment.CurrentDirectory;
                Directory.SetCurrentDirectory(jarRoot);
                var pipeline = new StanfordCoreNLP(props);

                _instance = new CoreNLPManager();
                _instance._coreNLPPipeline = pipeline;
                Directory.SetCurrentDirectory(currentPath);
                isInitiliazed = true;
                logger.Info("Initialisation ended");

                _instance.CollectTagsAndTheirDescription();
            }
            catch (Exception ex)
            {
                _instance = null;
                isInitiliazed = false;
                logger.Error(ex);
            }
            finally
            {
                if (!currentPath.Equals(Environment.CurrentDirectory))
                {
                    Directory.SetCurrentDirectory(currentPath);
                }
            }

        }
        return isInitiliazed;

Please let me know what is wrong , i am doing

thank you Dheeraj Kumar

dheeraj2016 commented 5 years ago

Sorry my mistake, ASP.NET Core is not supported till now

sergey-tihon commented 5 years ago

You may try to execute CoreNLP Server as separate Java process and then call it REST api:

mehmetilker commented 5 years ago

@dheeraj2016 I have it worked with .Net Standard Library project. Then you can reference it from .Net Core App. But you will need .Net Framework anyway.