在c#中运行Stanford.NLP.CoreNLP 3.8示例时出现TypeInitializationException

时间:2018-01-14 21:37:56

标签: c# stanford-nlp

我意识到这个问题是通过包含的来观察和解决的 props.setProperty(" ner.useSUTime"," 0"); 但是,错误仍然存​​在。我通过nuget添加了corenlp 3.8,我在NetStandard2.0配置中使用c#visual studio 2017。

我的代码:  `

public Dictionary<int, List<Word>> GetPOSFromStandforNLP(string sent)
        {
            var pos = new Dictionary<int, List<Word>>();
            var jarRoot = @"../vendor/stanford-corenlp-3.8.0-models";

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

            try
            {
                CultureInfo ci = new CultureInfo("en-US");
                Thread.CurrentThread.CurrentCulture = ci;
                Thread.CurrentThread.CurrentUICulture = ci;

                var curDir = Environment.CurrentDirectory;
                Directory.SetCurrentDirectory(jarRoot);   
                var pipeline = new StanfordCoreNLP(props);
                Directory.SetCurrentDirectory(curDir);
                // Annotation
                var annotation = new Annotation(text);
                pipeline.annotate(annotation);
                // Result - Pretty Print
                using (var stream = new ByteArrayOutputStream())
                {
                    pipeline.prettyPrint(annotation, new PrintWriter(stream));
                    Console.WriteLine(stream.toString());
                    stream.close();
                }

The error:

System.TypeInitializationException: The type initializer for 'edu.stanford.nlp.pipeline.AnnotationPipeline' threw an exception. ---> System.TypeInitializationException: The type initializer for 'edu.stanford.nlp.util.logging.Redwood' threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'System.Configuration.ConfigurationManager, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.     at IKVM.Internal.AssemblyClassLoader.LoadCustomClassLoaderRedirects()    at IKVM.Internal.AssemblyClassLoader.GetCustomClassLoaderType()    at IKVM.Internal.AssemblyClassLoader.GetJavaClassLoader()    at java.lang.Class.getClassLoader(CallerID )    at java.lang.Class.desiredAssertionStatus()    at edu.stanford.nlp.util.logging.Redwood..cctor(

1 个答案:

答案 0 :(得分:3)

Stanford.NLP.NET是一个IKVM项目。 IKVM是.NET中的now defunkt Java虚拟机实现。它不支持.NET Standard 2.0,只支持.NET Framework和Mono。

所以你的选择是:

  1. 目标.NET Framework或Mono
  2. Stanford.NLP.CoreNLP Java project移植到.NET Standard
  3. 将IKVM.NET项目移植到.NET Standard(最好是开源的,以便其他需要快速浏览.NET Standard中的Java代码的人可以这样做)
  4. 使用Pinvoke和普通的C-wrapper调用Java作为中间人,使用JNI加载JVM并公开使用JNI调用Java的函数,如described here
  5. 使用Java或another language that Stanford.NLP.CoreNLP supports
  6. 实施您的应用程序