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

本文关键字:可恢复 错误 模型 加载 nugetpackages 使用 | 更新日期: 2023-09-27 18:10:23

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

然而,在查找一些初学者代码开始后,我发现了以下代码props.setProperty("annotators" tokenize " ssplit, pos, lemma, ner, parse, dcoref");(链接到代码示例:http://sergey-tihon.github.io/Stanford.NLP.NET/StanfordCoreNLP.html)

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

我想知道我是否缺少任何nugetpackages,或者是否有额外的设置我必须经历。(注意。任何时候我都试图添加postagger nuget包,然后得到一个错误,说类Annotation在两个dll中被引用。

我发现,如果我删除一些属性,应用程序将正常运行,所以新的行看起来像这样props.setProperty("annotators" tokenize ");

任何帮助过去运行时错误,所以我可以继续进一步分析示例文本将不胜感激。谢谢你。

附图供参考。(显然我需要更多的声誉才能发布一张照片,但当我可以的时候,我会立即这样做:)编辑我现在已经添加了图片:)

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

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()

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

问题是您还没有下载模型。Nuget包本身不能工作。你必须在这里下载最新版本post - tagger

完成后,将其放入项目目录。然后将代码指向它。在"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));
            }
        }
    }
}