【问题标题】:Unrecoverable error while loading a tagger model using nugetpackages使用 nugetpackages 加载标记器模型时出现不可恢复的错误
【发布时间】:2015-08-30 19:18:09
【问题描述】:

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

但是在查找一些初学者代码开始后,我发现了以下代码 props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");(链接到代码示例:@ 987654321@)

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

我想知道我是否遗漏了任何 nugetpackages,或者我是否需要进行其他设置。 (注意。每当我尝试添加 postagger nuget 包时,我都会收到一个错误,指出在两个 dll 中引用了类注释。)

我发现如果我删除一些属性,应用程序将正确运行,所以新行看起来像这样 "props.setProperty("annotators", "tokenize, 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()

【问题讨论】:

  • 我没有足够的 .NET 知识来帮助修复错误,但根本原因是 Java 类路径中缺少 corenlp 模型。这可以在名为 stanford-corenlp-3.5.2-models.jar 的 *.jar 文件中找到。当“pos”注释器尝试从此 jar 加载其模型时,就会出现错误。
  • 我明白了。由于我使用了 nuget 包,我将如何添加丢失的 .jar 文件?和或模型到 nuget 包?还是我必须等待支持 stanford-nlp 的人更新其 nuget 包?
  • 查看你链接的 C# 代码,罪魁祸首可能是第 15 行:var jarRoot = @"c:\models\stanford-corenlp-full-2015-01-30\stanford-corenlp-3.5.2-models\"; -- 这应该指向模型 jar
  • 啊,是的,我没有手动将文件加载到项目中,而是使用 .net 中的 nuget 添加它们,因此所有内容(至少在理论上)都已正确引用。我将尝试手动添加它们以查看是否得到不同的结果。最近我还发布了一张我的代码图片,希望能帮助回答任何进一步的问题。 :)

标签: c# stanford-nlp nuget-package


【解决方案1】:

问题是您还没有下载模型。 Nuget 包本身不起作用。 你必须在这里下载最新版本POS-Tagger

完成后,将其放入项目目录中。然后将代码指向它。在“3.6.0 2015-12-09 更新兼容性”版本中,标注器具有不同的名称,例如“english-bidirectional-distim.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));
            }
        }
    }
}

【讨论】:

  • 如果我尝试运行您的代码,我会收到以下异常。 “edu.stanford.nlp.io.IOUtils 的类型初始化程序引发了异常”,当它到达 new MaxentTagger(...) 行时,我已确保 modelsDirectory 对我的项目是正确的。
  • 请详细说明错误。获取异常的详细信息。可能是依赖错误。
  • 我对延误表示歉意。我赶上了工作。我会尽快回复您。
  • 再次抱歉耽搁了只是想让您知道我没有忘记,我会尽快回复您。
  • 我在两个项目中完成了这项工作。这实际上只是在 nuget 上安装它然后从提供的链接下载最新包的情况。将其存储在您的项目文件夹中,您可以随时访问它并启动它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-02
  • 1970-01-01
  • 2022-06-21
  • 1970-01-01
  • 2012-03-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多