用于.NET的Stanford.NLP未加载模型

Vin*_*dar 3 .net c# nlp stanford-nlp

我正在尝试运行此处为Stanford.NLP for .NET 提供的示例代码。

我通过 Nuget 安装了该软件包,下载了 CoreNLP zip 存档,并提取了 stanford-corenlp-3.7.0-models.jar。解压后,我在stanford-corenlp-full-2016-10-31\edu\stanford\nlp\models中找到了“models”目录。

这是我尝试运行的代码:

 public static void Test1()
    {
        // Path to the folder with models extracted from `stanford-corenlp-3.6.0-models.jar`
        var jarRoot = @"..\..\..\stanford-corenlp-full-2016-10-31\edu\stanford\nlp\models\";

        // Text for processing
        var text = "Kosgi Santosh sent an email to Stanford University. He didn't get a reply.";

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

        // We should change current directory, so StanfordCoreNLP could find all the model files automatically
        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)

当我运行代码时出现以下错误:

stanford-corenlp-3.6.0.dll 中发生了类型“java.lang.RuntimeException”的第一次机会异常 stanford-corenlp-3.6.0.dll 中发生了类型“java.lang.RuntimeException”的未处理异常附加信息: edu.stanford.nlp.io.RuntimeIOException:加载标记器模型时出错(可能缺少模型文件)

我究竟做错了什么?我真的很想让这个工作。:(

Ser*_*hon 5

米凯尔·克里斯滕森的回答是正确的。stanfrod-corenlp-ful-*.zip存档包含内部带有模型的文件stanford-corenlp-3.7.0-models.jar(这是一个 zip 存档)。在 Java 世界中,您将其添加jar到类路径中,它会自动解析模型在存档中的位置。

CoreNLP 有一个文件DefaultPaths.java,它指定模型文件的路径。因此,当您实例化未指定模型位置的对象时,您应该保证可以通过默认路径找到模型(与 相关StanfordCoreNLP)。PropertiesEnvironment.CurrentDirectory

保证路径中存在文件的最简单方法Environment.CurrentDirectory + "edu/stanford/nlp/models/ner/english.all.3class.distsim.crf.ser.gz"是将 jar 存档解压缩到该文件夹​​,并将当前目录临时更改为解压文件夹。

var jarRoot = "nlp.stanford.edu/stanford-corenlp-full-2016-10-31/jar-modules/";
...
var curDir = Environment.CurrentDirectory;
Directory.SetCurrentDirectory(jarRoot);
var pipeline = new StanfordCoreNLP(props);
Directory.SetCurrentDirectory(curDir);
Run Code Online (Sandbox Code Playgroud)

另一种方法是指定管道所需的所有模型的路径(它实际上取决于 的列表annotators)。此选项更复杂,因为您必须找到正确的属性键,并指定所有使用的模型的路径。但如果您想最小化部署包的大小,它可能会很有用。

var props = new Properties();
props.put("annotators", "tokenize, ssplit, pos, lemma, ner, depparse");
props.put("ner.model",
          "edu/stanford/nlp/models/ner/english.all.3class.distsim.crf.ser.gz");
props.put("ner.applyNumericClassifiers", "false");
var pipeline = new StanfordCoreNLP(props);
Run Code Online (Sandbox Code Playgroud)