我正在尝试运行示例代码这里为Stanford.NLP for .NET提供。
我通过Nuget安装了这个包,下载了CoreNLP压缩文件,提取了stanford-corenlp-3.7.0-models.jar。提取后,我在stanford-corenlp-full-2016-10-31\edu\stanford\nlp\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();
}
}
运行代码时会出现以下错误:
类型'java.lang.RuntimeException‘的第一次异常发生在斯坦福-corenlp-3.6.0.dll-java.lang.RuntimeException类型的未处理异常发生在斯坦福-corenlp-3.6.0.dll附加信息: edu.stanford.nlp.io.RuntimeIOException:在加载标记模型时出错(可能缺少模型文件)
我做错了什么?我真的很想让它发挥作用。:(
发布于 2017-05-02 03:32:51
米克尔·克里斯滕森的答案是正确的。stanfrod-corenlp-ful-*.zip
归档包含带有模型的文件stanford-corenlp-3.7.0-models.jar
(这是一个zip归档)。在Java中,您可以在类路径上添加这个jar
,它会自动解析模型在归档文件中的位置。
CoreNLP有一个文件DefaultPaths.java,它指定模型文件的路径。因此,当您使用不指定模型位置的StanfordCoreNLP
对象实例化Properties
时,您应该确保默认路径(与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);
发布于 2017-01-09 15:39:41
我也有同样的问题。要修复它,请改用stanford-corenlp-3.6.0-models.jar。( Nuget包的版本必须与CoreNLP库的版本完全相同。实际上是3.6.0)。
发布于 2017-02-03 20:36:20
我想你搞错了模型的道路。它应该指向jar根文件夹。
试一试这条路径:
var jarRoot = @"..\..\..\stanford-corenlp-full-2016-10-31"
https://stackoverflow.com/questions/40814503
复制相似问题