尝试在WebAPI C#Project

时间:2015-09-09 04:10:10

标签: asp.net .net asp.net-web-api owin stanford-nlp

我一直在尝试加载我从建议的ZIP文件中提取的默认模型。通常,我将注释加载到应用程序级别的单例中,以便可以跨所有会话共享资源。 (在WebAPI OWIN Startup中,这是从startup.cs调用的)。

尝试使用相对路径引用的其他方法,我收到了这个错误:

  

无法解决   " EDU /斯坦福/ NLP /模型/ POS-标注器/英语 - left3words /英语 - left3words-distsim.tagger"   作为类路径,文件名或URL

我不确定我是否接近或远离解决方案。 这是我的ASP.NET WebAPI项目的根目录:

image

但是,我收到错误:

  

未处理的执行错误

     

描述:执行期间发生了未处理的异常   当前的网络请求。请查看堆栈跟踪了解更多信息   有关错误的信息以及它在代码中的起源。

     

异常详细信息:java.lang.reflect.InvocationTargetException:

     

来源错误:

     

第43行://我们应该更改当前目录,所以   StanfordCoreNLP可以自动找到所有模型文件第44行:   Directory.SetCurrentDirectory(HostingEnvironment.MapPath(ModelLocation));   第45行:管道=新的StanfordCoreNLP(道具);线   46:}第47行:最后

     

源文件:D:\ xxx \ xxx \ xxx \ NLP.cs行:45

     

堆栈追踪:

     

[InvocationTargetException] __(Object [])+444
  FastConstructorAccessorImpl.newInstance(Object [] args)+28
  java.lang.reflect.Constructor.newInstance(Object [] initargs,CallerID   )+133 edu.stanford.nlp.util.ClassFactory.createInstance(Object []   参数)+108

     

[ClassCreationException:MetaClass无法创建公共   edu.stanford.nlp.time.TimeExpressionExtractorImpl(java.lang.String中,java.util.Properties)   与args [sutime,{}]]
  edu.stanford.nlp.util.ClassFactory.createInstance(Object [] params)   +372 edu.stanford.nlp.util.MetaClass.createInstance(Object [] objects)+34
  edu.stanford.nlp.util.ReflectionLoading.loadByReflection(字符串   className,Object [] arguments)+71

     

[ReflectionLoadingException:创建错误   edu.stanford.nlp.time.TimeExpressionExtractorImpl]
  edu.stanford.nlp.util.ReflectionLoading.loadByReflection(字符串   className,Object []参数)+232
  edu.stanford.nlp.time.TimeExpressionExtractorFactory.create(字符串   className,String name,Properties props)+80
  edu.stanford.nlp.time.TimeExpressionExtractorFactory.createExtractor(字符串   名称,属性道具)+34
  edu.stanford.nlp.ie.regexp.NumberSequenceClassifier..ctor(属性   props,Boolean useSUTime,Properties sutimeProps)+57
  edu.stanford.nlp.ie.NERClassifierCombiner..ctor(布尔   applyNumericClassifiers,Boolean useSUTime,Properties nscProps,   String [] loadPaths)+129
  edu.stanford.nlp.pipeline.AnnotatorImplementations.ner(属性   属性)+454 edu.stanford.nlp.pipeline.6.create()+46
  edu.stanford.nlp.pipeline.AnnotatorPool.get(String name)+163
  edu.stanford.nlp.pipeline.StanfordCoreNLP.construct(属性,   Boolean,AnnotatorImplementations)+555
  edu.stanford.nlp.pipeline.StanfordCoreNLP..ctor(属性道具,   Boolean enforceRequirements)+55
  edu.stanford.nlp.pipeline.StanfordCoreNLP..ctor(属性道具)+76   XXX.XXX.NLP.Start(String modelLocation)in   d:\ XXX \ XXX \ XXX \ NLP.cs:45
  XXX.XXX.Startup.Configuration(IAppBuilder app)中   d:\ XXX \ XXX \ XXX \ Startup.cs:16

     

[TargetInvocationException:目标抛出了异常   aninvocation。] System.RuntimeMethodHandle.InvokeMethod(Object   target,Object []参数,签名sig,布尔构造函数)+0
  System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj,   Object []参数,Object []参数)+128
  System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags   invokeAttr,Binder binder,Object []参数,CultureInfo文化)   +146 Owin.Loader。<> c__DisplayClass12.b__b(IAppBuilder builder)+93
  Owin.Loader<> c__DisplayClass1.b__0(IAppBuilder   建设者)+209
  Microsoft.Owin.Host.SystemWeb.OwinAppContext.Initialize(Action 1   启动)+843
  Microsoft.Owin.Host.SystemWeb.OwinBuilder.Build(Action 1 startup)+51   Microsoft.Owin.Host.SystemWeb.OwinHttpModule.InitializeBlueprint()   +101 System.Threading.LazyInitializer.EnsureInitializedCore(T& target,Boolean& initialized,Object& syncLock,Func 1 valueFactory)   +141 Microsoft.Owin.Host.SystemWeb.OwinHttpModule.Init(HttpApplication)   上下文)+172
  System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr的   appContext,HttpContext上下文,MethodInfo []处理程序)+618
  System.Web.HttpApplication.InitSpecial(HttpApplicationState状态,   MethodInfo [] handlers,IntPtr appContext,HttpContext context)+172
  System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr的   appContext,HttpContext context)+419
  System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr的   appContext)+343

     

[HttpException(0x80004005):目标抛出了异常   一个调用。]   System.Web.HttpRuntime.FirstRequestInit(HttpContext context)+579
  System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context)   +120 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest)   wr,HttpContext context)+712

这是我的代码(在startup.cs中调用NLP.Start()):

public static class NLP
{
    private static string _modelLocation = @"~\NLPModels";
    public static string ModelLocation
    {
        set
        {
            NLP.Start(value);
        }
        get
        {
            return _modelLocation;
        }
    }
    private static StanfordCoreNLP pipeline;
    public static void Start(string modelLocation = null)
    {
        var curDir = Environment.CurrentDirectory;
        if (!string.IsNullOrEmpty(modelLocation))
        {
            _modelLocation = modelLocation;
        }
        try
        {
            // Annotation pipeline configuration
            var props = new Properties();
            props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
            props.setProperty("sutime.binders", "0");
            // We should change current directory, so StanfordCoreNLP could find all the model files automatically
            Directory.SetCurrentDirectory(HostingEnvironment.MapPath(ModelLocation));
            pipeline = new StanfordCoreNLP(props);
        }
        finally
        {
            Directory.SetCurrentDirectory(curDir);
        }
    }

    public static JObject ProcessText(string text)
    {
        var annotation = new Annotation(text);
        using (java.io.StringWriter writer = new java.io.StringWriter())
        {
            pipeline.jsonPrint(annotation, writer);
            return JObject.Parse(writer.toString());
        }
    }
}

1 个答案:

答案 0 :(得分:0)

稍微捅了一下之后,我找到了解决同样问题的方法。

https://github.com/sergey-tihon/Stanford.NLP.NET/issues/11

如果您不愿意阅读该主题,这是基本答案。修改此行代码

props.setProperty("sutime.binders", "0");

props.setProperty("ner.useSUTime", "0");