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

The*_*eil 1 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();
                }
Run Code Online (Sandbox Code Playgroud)

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(
Run Code Online (Sandbox Code Playgroud)

Nig*_*888 5

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

所以你的选择是:

  1. 目标 .NET Framework 或 Mono。
  2. Stanford.NLP.CoreNLP Java 项目移植到 .NET Standard。
  3. 将 IKVM.NET 项目移植到 .NET Standard(最好是将其开源,以便其他需要在 .NET Standard 中快速运行 Java 代码的人可以这样做)。请注意,目前正在进行一项名为ikvm-revived 的工作
  4. 使用 Pinvoke 和普通 C 包装器调用 Java 作为中间人,使用 JNI 加载 JVM 并公开使用 JNI 调用 Java 的函数,如此处所述
  5. 使用 Java 或Stanford.NLP.CoreNLP 支持的其他语言实现您的应用程序。