【发布时间】:2018-01-31 14:39:36
【问题描述】:
我正在尝试将斯坦福 NLP 用于 .NET。我对此很陌生,我在加载模型时遇到了麻烦。
我在https://sergey-tihon.github.io/Stanford.NLP.NET//faq.html 和Stanford.NLP for .NET not loading models 中阅读过相同类型的问题。但我不知道他们将“stanford-corenlp-full-2016-10-31”文件夹保存在哪里。
这是我有 C# 代码的地方。 C:\Users\Kabi\source\repos\Search\Search stanford-corenlp-full-2017-06-09 文件夹在这里 - C:\Users\Kabi\source\repos\Search。
我在 C:\Users\Kabi\source\repos\Search\stanford-corenlp-full-2017- 中提取了 stanford-corenlp-3.8.0-models.jar 06-09
这是我的 C# 代码。
浏览.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.IO;
using java.util;
using java.io;
using edu.stanford.nlp.pipeline;
using Console = System.Console;
namespace Search
{
class Browse
{
public void StanfordNLP()
{
// Path to the folder with models extracted from `stanford-corenlp-3.8.0-models.jar`
var jarRoot = @"..\stanford-corenlp-full-2017-06-09";
// 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, ner, parse, 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();
}
}
}
}
如何正确加载模型?
【问题讨论】:
标签: c# .net stanford-nlp