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

SimpleTensor ClassNotFoundException - Wrong directory? #96

Closed AscanioB closed 2 years ago

AscanioB commented 5 years ago

I'm trying to conduct sentiment analysis but I'm getting a ClassNotFoundException on SimpleTensor as show here. Upon further investigation of the Object Browser it appears that the SimpleTensor class is in edu.stanford.nlp.neural rathar than the edu.stanford.nlp.neural.rnn that it is searching for. Any ideas if I can move the file or change it's directory?

sergey-tihon commented 5 years ago

Sorry, I cannot open link https://prntscr.com/n5rfid Can you please post code here ? It may depends on what model you are trying to deserialize. Please specify Nuget package that you use (with version) and what model do you use (ideally, URL where you download it from)

AscanioB commented 5 years ago

Code below, error at: pipeline = new StanfordCoreNLP(props);

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using java.util.prefs;
using edu.stanford.nlp.ling;
using edu.stanford.nlp.pipeline;
using edu.stanford.nlp.util;
using edu.stanford.nlp.trees;
using edu.stanford.nlp.sentiment;
using edu.stanford.nlp.neural.rnn;
using edu.stanford.nlp.neural;
using org.ejml.simple;
using System.IO;

namespace Ascanio_Bajada_Thesis
{
    public class SentimentAnalyzer
    {
        StanfordCoreNLP pipeline;

        public void Initialize()
        {
            var jarRoot = @"..\..\..\..\Thesis\packages\stanford-corenlp-full-2018-02-27\models";

            java.util.Properties props = new java.util.Properties();

            props.setProperty("pos.model", "pos-tagger\\english-left3words-distsim.tagger");
            props.setProperty("ner.model", "ner\\classifiers\\english.all.3class.distsim.crf.ser.gz");
            props.setProperty("ner.useSUTime", "false");
            props.setProperty("ner.applyFineGrained", "0");
            props.setProperty("parse.model", "lexparser\\englishPCFG.ser.gz");
            props.setProperty("sentiment.model", "sentiment\\sentiment.ser.gz");
            props.setProperty("dcoref.demonym", "dcoref\\demonyms.txt");
            props.setProperty("dcoref.states", "dcoref\\state-abbreviations.txt");
            props.setProperty("dcoref.animate", "dcoref\\animate.unigrams.txt");
            props.setProperty("dcoref.inanimate", "dcoref\\inanimate.unigrams.txt");
            props.setProperty("dcoref.big.gender.number", "dcoref\\gender.map.ser.gz");
            props.setProperty("dcoref.countries", "dcoref\\countries");
            props.setProperty("dcoref.states.provinces", "dcoref\\statesandprovinces");
            props.setProperty("dcoref.singleton.model", "dcoref\\singleton.predictor.ser");
            props.setProperty("dcoref.language", "en");
            props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, sentiment");
            props.setProperty("dcoref.algorithm", "statistical");

            // build pipeline
            var curDir = Environment.CurrentDirectory;
            Directory.SetCurrentDirectory(jarRoot);

            pipeline = new StanfordCoreNLP(props);

            Directory.SetCurrentDirectory(curDir);
        }

        public SentimentResult GetSentimentResult(string text)
        {
            Initialize();

            SentimentClassification classification = new SentimentClassification();
            SentimentResult sentimentResult = new SentimentResult();

            if (text != null && text.Length > 0)
            {
                Annotation annotation = pipeline.process(text);

                var sentences = annotation.get(new CoreAnnotations.SentencesAnnotation().getClass()) as ArrayList;

                foreach (CoreMap sentence in sentences)
                {
                    Tree tree = (Tree)sentence.get(typeof(SentimentCoreAnnotations.SentimentAnnotatedTree));
                    int sentiment = RNNCoreAnnotations.getPredictedClass(tree);

                    SimpleMatrix simpleMatrix = RNNCoreAnnotations.getPredictions(tree);

                    classification.setVeryNegative((int)Math.Round(simpleMatrix.get(0) * 100d));
                    classification.setNegative((int)Math.Round(simpleMatrix.get(1) * 100d));
                    classification.setNeutral((int)Math.Round(simpleMatrix.get(2) * 100d));
                    classification.setPositive((int)Math.Round(simpleMatrix.get(3) * 100d));
                    classification.setVeryPositive((int)Math.Round(simpleMatrix.get(4) * 100d));

                    string setimentType = (string)sentence.get(typeof(SentimentCoreAnnotations.SentimentClass));

                    sentimentResult.setSentimentType(setimentType);
                    sentimentResult.setSentimentClass(classification);
                    sentimentResult.setSentimentScore(RNNCoreAnnotations.getPredictedClass(tree));
                }
            }
            return sentimentResult;
        }
    }
}

Nuget package: Stanford.NLP.CoreNLP 3.9.1 Model: stanford-english-corenlp-2018-02-27-models (I've also tried with the 2018-10-05, v3.9.2) downloaded from https://stanfordnlp.github.io/CoreNLP/history.html Re-hosted images to imgur

Furthermore I've also tried extracting the stanford-corenlp-3.9.1.jar file, moving the SimpleTensor.class to the correct directory (under edu.stanford.nlp.neural.rnn), archiving into .jar and replacing the old one but the Object Browser still found it under the neural directory.

arcontechnologies commented 5 years ago

Used Nuget package: Stanford.NLP.CoreNLP 3.9.1

Hi , I have the same issue . It throws error at :

var pipeline = new StanfordCoreNLP(props)

I saw that workaround exist in java : https://github.com/stanfordnlp/CoreNLP/issues/715 But how do we tackle "dependencies" in .Net as Maven / POM.xml tool doesn't exist ?

Any idea, insight ?

Thanks for your feedback

sergey-tihon commented 2 years ago

Close as an old issue