Stanford.NLP for .NET没有加载模型

我正在尝试运行此处为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(); } } 

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

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

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

Mikael Kristensen的回答是正确的。 stanfrod-corenlp-ful-*.zip存档包含stanford-corenlp-3.7.0-models.jar ,里面有模型(这是一个zip存档)。 在Java世界中,您可以在类路径中添加此jar ,它会自动解析模型在存档中的位置。

CoreNLP有一个文件DefaultPaths.java ,它指定模型文件的路径。 因此,当您使用未指定模型位置的Properties对象实例化StanfordCoreNLP时,您应该保证可以通过默认路径找到模型(与Environment.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); 

另一种方法是指定管道所需的所有模型的路径(实际上取决于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); 

我认为你已经弄错了模型的路径。 它应该指向jar根文件夹。

请尝试此路径:

 var jarRoot = @"..\..\..\stanford-corenlp-full-2016-10-31" 

我有同样的问题。 要修复它,请改用stanford-corenlp-3.6.0-models.jar 。 ( Nuget包的版本必须与CoreNLP库的版本完全相同。实际上是3.6.0 )。