C# 使用nugetpackages加载标记器模型时出现不可恢复的错误

C# 使用nugetpackages加载标记器模型时出现不可恢复的错误,c#,stanford-nlp,nuget-package,C#,Stanford Nlp,Nuget Package,我一直在尝试使用stanford-corenlp-3.5.2 nugetpackage创建和运行一个简单的程序 然而,在查找了一些初学者代码之后,我发现了以下代码支柱;(链接到代码示例:) 但每当控制台应用程序加载pos时,它就会触发一个运行时错误,指出它无法加载标记器 我想知道我是否丢失了任何nugetpackages,或者是否需要进行额外的设置。(注意,每当我尝试添加postagger nuget包时,我就会收到一个错误,表示类注释在两个DLL中引用。) 我发现,如果删除一些属性,应用程序将

我一直在尝试使用stanford-corenlp-3.5.2 nugetpackage创建和运行一个简单的程序

然而,在查找了一些初学者代码之后,我发现了以下代码支柱;(链接到代码示例:)

但每当控制台应用程序加载pos时,它就会触发一个运行时错误,指出它无法加载标记器

我想知道我是否丢失了任何nugetpackages,或者是否需要进行额外的设置。(注意,每当我尝试添加postagger nuget包时,我就会收到一个错误,表示类注释在两个DLL中引用。)

我发现,如果删除一些属性,应用程序将正常运行,因此新行如下所示 props.setProperty(“注释器”、“标记化、ssplit”)

如果您能帮助我克服运行时错误,以便我能够继续进一步分析示例文本,我们将不胜感激。谢谢

附件图片供参考。(显然,我需要更多的声誉,以便张贴图片,但当我可以,我会立即这样做:)编辑我已经添加了图片现在:)

行异常处的堆栈跟踪如下所示:

at edu.stanford.nlp.pipeline.AnnotatorFactories.4.create()
at edu.stanford.nlp.pipeline.AnnotatorPool.get(String name)
at edu.stanford.nlp.pipeline.StanfordCoreNLP.construct(Properties , Boolean , AnnotatorImplementations )
at edu.stanford.nlp.pipeline.StanfordCoreNLP..ctor(Properties props, Boolean enforceRequirements)
at edu.stanford.nlp.pipeline.StanfordCoreNLP..ctor(Properties props)
at ConApplicationSabrinaNLP.TestClass.donlp() in c:\Users\Justin\Documents\Visual Studio 2013\Projects\ConApplicationSabrinaNLP\ConApplicationSabrinaNLP\TestClass.cs:line 28
at ConApplicationSabrinaNLP.Program.Main(String[] args) in c:\Users\Justin\Documents\Visual Studio 2013\Projects\ConApplicationSabrinaNLP\ConApplicationSabrinaNLP\Program.cs:line 20
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

问题是您尚未下载模型。Nuget软件包本身无法工作。 您必须在此下载最新版本

完成后,将其放在项目目录中。然后将代码指向它。在“3.6.0 2015-12-09更新兼容性”版本中,标记器有一个不同的名称,例如“english bidirectional distsim.tagger”。 确保您将代码指向正确的文件夹和文件,它将正常工作

下面是我的windows窗体项目中的一个工作示例

using java.io;
using java.util;
using edu.stanford.nlp.ling;
using edu.stanford.nlp.tagger.maxent;
using Console = System.Console;
using System.IO;
using System.Windows.Forms;

namespace Stanford.NLP.POSTagger.CSharp
{
    class PosTagger

    {
        // get the base folder for the project
        public static string GetAppFolder()
        {
            return Path.GetDirectoryName(Application.ExecutablePath).Replace(@"*your project directory here*\bin\Debug", string.Empty);
        }

        public void testTagger()
        {
            var jarRoot = Path.Combine(GetAppFolder(), @"packages\stanford-postagger-2015-12-09");
            Console.WriteLine(jarRoot.ToString());
            var modelsDirectory = jarRoot + @"\models";

            // Loading POS Tagger
            var tagger = new MaxentTagger(modelsDirectory + @"\english-bidirectional-distsim.tagger");

            // Text for tagging
            var text = "A Part-Of-Speech Tagger (POS Tagger) is a piece of software that reads text"
                       + "in some language and assigns parts of speech to each word (and other token),"
                       + " such as noun, verb, adjective, etc., although generally computational "
                       + "applications use more fine-grained POS tags like 'noun-plural'.";

            var sentences = MaxentTagger.tokenizeText(new java.io.StringReader(text)).toArray();
            foreach (ArrayList sentence in sentences)
            {
                var taggedSentence = tagger.tagSentence(sentence);
                Console.WriteLine(Sentence.listToString(taggedSentence, false));
            }
        }
    }
}

我不知道有多少.NET可以帮助修复错误,但根本原因是Java类路径中缺少corenlp模型。这可以在名为
stanford-corenlp-3.5.2-models.jar的*.jar文件中找到。当“注释器试图从这个罐子加载它的模型。我明白了。既然我使用了nuget软件包,我该如何添加丢失的.jar文件?和或模型到nuget包?或者我必须等待支持斯坦福nlp的人更新其nuget软件包吗?看看你链接的C#代码,罪魁祸首可能是第15行:
var jarRoot=@“C:\models\stanford-corenlp-full-2015-01-30\stanford-corenlp-3.5.2-models\”--这应该指向模型jarAh yes,而不是手动将文件加载到我使用.net中的nuget添加的项目中,这样所有内容(至少理论上)都已经被正确引用了。我将尝试手动添加它们,以查看是否得到不同的结果。最近我还贴了一张我的代码的图片,希望能帮助回答更多的问题如果我尝试运行你的代码,我会得到以下异常。“edu.stanford.nlp.io.IOUtils的类型初始值设定项引发了一个异常”当它遇到行new MaxentTagger(…)时,我已确保modelsDirectory对于我的项目是正确的。请提供错误的更多详细信息。获取异常的详细信息。这可能是一个依赖错误。我为延迟道歉。我忙于工作。我会尽我最大的努力尽快回复你。再次为延误道歉,我只是想让你知道我没有忘记,我会尽快回复你。我在两个项目中完成了这项工作。它实际上只是在nuget上安装它,然后从提供的链接下载最新的软件包。将它存储在您的项目文件夹中,您可以随时访问它并启动它。