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

IllegalMonitorState in c# implementation #91

Closed ghost closed 2 years ago

ghost commented 5 years ago

I have the below code using the sample I saw in https://github.com/sergey-tihon/Stanford.NLP.NET/issues/83.

The code will return all of the names in the text but will always end in

Exception in thread "Thread-0" java.lang.IllegalMonitorStateException at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(ReentrantLock.java:151) at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1259) at java.util.concurrent.locks.ReentrantLock.unlock(ReentrantLock.java:457) at edu.stanford.nlp.pipeline.StanfordCoreNLPClient$BackendScheduler.run(StanfordCoreNLPClient.java:173)

       readonly static java.lang.Class sentencesAnnotationClass =
           new CoreAnnotations.SentencesAnnotation().getClass();
        readonly static java.lang.Class tokensAnnotationClass =
            new CoreAnnotations.TokensAnnotation().getClass();
        readonly static java.lang.Class textAnnotationClass =
            new CoreAnnotations.TextAnnotation().getClass();
        readonly static java.lang.Class partOfSpeechAnnotationClass =
            new CoreAnnotations.PartOfSpeechAnnotation().getClass();
        readonly static java.lang.Class namedEntityTagAnnotationClass =
            new CoreAnnotations.NamedEntityTagAnnotation().getClass();
        readonly static java.lang.Class normalizedNamedEntityTagAnnotation =
            new CoreAnnotations.NormalizedNamedEntityTagAnnotation().getClass();

        public static StanfordCoreNLPClient CoreNLP()
        {
            var props = new java.util.Properties();
            props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner");
            StanfordCoreNLPClient pipeline = new StanfordCoreNLPClient(props, "http://localhost", 9000, 2);
            return pipeline;
        }

        public static IEnumerable<string> Names(StanfordCoreNLPClient pipeline, string input){

            Annotation document = new Annotation(input);
            pipeline.annotate(document);

            var sentences = document.get(sentencesAnnotationClass) as java.util.AbstractList;
            foreach (CoreMap sentence in sentences)
            {
                var tokens = sentence.get(tokensAnnotationClass) as java.util.AbstractList;
                foreach (CoreLabel token in tokens)
                {
                    var word = token.get(textAnnotationClass);
                    var ner = token.get(namedEntityTagAnnotationClass);
                    if (ner.Equals("PERSON"))
                    {
                        yield return word.ToString();
                    }
                }
            }

I'm not very familiar with concurrency in Java so any resources that may solve my problem or a simple work around will be greatly appreciated!

sergey-tihon commented 5 years ago

The most latest sample should be here - https://sergey-tihon.github.io/Stanford.NLP.NET//samples/CoreNLP.Server.html

Where did you get stack trace? It looks like JVM exception. You always can repeat question on SO - there are much more Java experts.

Take a look docs about CoreNLP server and check params - https://stanfordnlp.github.io/CoreNLP/corenlp-server.html

java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -port 9000 -timeout 15000

ghost commented 5 years ago

I copied the exact example code from https://sergey-tihon.github.io/Stanford.NLP.NET//samples/CoreNLP.Server.html but still get the exception printed at the end of execution.

Kosgi [pos=NNP; ner=PERSON] Santosh [pos=NNP; ner=PERSON] sent [pos=VBD; ner=O] an [pos=DT; ner=O] email [pos=NN; ner=O] to [pos=TO; ner=O] Stanford [pos=NNP; ner=ORGANIZATION] University [pos=NNP; ner=ORGANIZATION] . [pos=.; ner=O] Exception in thread "Thread-0" java.lang.IllegalMonitorStateException at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(ReentrantLock.java:151) at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1259) at java.util.concurrent.locks.ReentrantLock.unlock(ReentrantLock.java:457) at edu.stanford.nlp.pipeline.StanfordCoreNLPClient$BackendScheduler.run(StanfordCoreNLPClient.java:173) Press any key to continue...

I will ask on SO and hopefully get a simple work around.

sergey-tihon commented 2 years ago

Close as an old issue