尝试在 Web API C# 项目的 Stanford.NLP.CoreNLP.Net 中加载默认 NLP 模型时出错
Error Trying to Load DefaultNLP Model in Stanford.NLP.CoreNLP .Net in WebAPI C# Project
我一直在尝试加载从建议的 ZIP 文件中提取的默认模型。通常,我在应用程序级别将注释加载到单例中,以便可以在所有会话之间共享资源。 (在 WebAPI OWIN Startup 中,这是从 startup.cs 调用的)。
尝试使用相对路径引用的其他方法时,出现此错误:
unable to resolve
"edu/stanford/nlp/models/pos-tagger/english-left3words/english-left3words-distsim.tagger"
as either class path, filename or URL
我不确定我是离解决方案更近了还是更远了。 这是我的 ASP.NET WebAPI 项目的根目录:
但是,我收到错误消息:
Unhandled Execution Error
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.
Exception Details: java.lang.reflect.InvocationTargetException:
Source Error:
Line 43: // We should change current directory, so
StanfordCoreNLP could find all the model files automatically Line 44:
Directory.SetCurrentDirectory(HostingEnvironment.MapPath(ModelLocation));
Line 45: pipeline = new StanfordCoreNLP(props); Line
46: } Line 47: finally
Source File: D:\xxx\xxx\xxx\NLP.cs Line: 45
Stack Trace:
[InvocationTargetException] __(Object[] ) +444
FastConstructorAccessorImpl.newInstance(Object[] args) +28
java.lang.reflect.Constructor.newInstance(Object[] initargs, CallerID
) +133 edu.stanford.nlp.util.ClassFactory.createInstance(Object[]
params) +108
[ClassCreationException: MetaClass couldn't create public
edu.stanford.nlp.time.TimeExpressionExtractorImpl(java.lang.String,java.util.Properties)
with 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(String
className, Object[] arguments) +71
[ReflectionLoadingException: Error creating
edu.stanford.nlp.time.TimeExpressionExtractorImpl]
edu.stanford.nlp.util.ReflectionLoading.loadByReflection(String
className, Object[] arguments) +232
edu.stanford.nlp.time.TimeExpressionExtractorFactory.create(String
className, String name, Properties props) +80
edu.stanford.nlp.time.TimeExpressionExtractorFactory.createExtractor(String
name, Properties props) +34
edu.stanford.nlp.ie.regexp.NumberSequenceClassifier..ctor(Properties
props, Boolean useSUTime, Properties sutimeProps) +57
edu.stanford.nlp.ie.NERClassifierCombiner..ctor(Boolean
applyNumericClassifiers, Boolean useSUTime, Properties nscProps,
String[] loadPaths) +129
edu.stanford.nlp.pipeline.AnnotatorImplementations.ner(Properties
properties) +454 edu.stanford.nlp.pipeline.6.create() +46
edu.stanford.nlp.pipeline.AnnotatorPool.get(String name) +163
edu.stanford.nlp.pipeline.StanfordCoreNLP.construct(Properties ,
Boolean , AnnotatorImplementations ) +555
edu.stanford.nlp.pipeline.StanfordCoreNLP..ctor(Properties props,
Boolean enforceRequirements) +55
edu.stanford.nlp.pipeline.StanfordCoreNLP..ctor(Properties props) +76
XXX.XXX.NLP.Start(String modelLocation) in
D:\xxx\xxx\xxx\NLP.cs:45
XXX.XXX.Startup.Configuration(IAppBuilder app) in
D:\xxx\xxx\xxx\Startup.cs:16
[TargetInvocationException: Exception has been thrown by the target of
aninvocation.] System.RuntimeMethodHandle.InvokeMethod(Object
target, Object[] arguments, Signature sig, Boolean constructor) +0
System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj,
Object[] parameters, Object[] arguments) +128
System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
+146 Owin.Loader.<>c__DisplayClass12.b__b(IAppBuilder builder) +93
Owin.Loader.<>c__DisplayClass1.b__0(IAppBuilder
builder) +209
Microsoft.Owin.Host.SystemWeb.OwinAppContext.Initialize(Action 1
startup) +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
context) +172
System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr
appContext, HttpContext context, MethodInfo[] handlers) +618
System.Web.HttpApplication.InitSpecial(HttpApplicationState state,
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): Exception has been thrown by the target
of an invocation.]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +579
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context)
+120 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest
wr, HttpContext context) +712
这是我的代码(NLP.Start() 在 startup.cs 中被调用):
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());
}
}
}
经过一番摸索,我找到了同样问题的解决方案。
https://github.com/sergey-tihon/Stanford.NLP.NET/issues/11
如果您不愿意通读线程,这里是基本答案。修改这行代码
props.setProperty("sutime.binders", "0");
到
props.setProperty("ner.useSUTime", "0");
我一直在尝试加载从建议的 ZIP 文件中提取的默认模型。通常,我在应用程序级别将注释加载到单例中,以便可以在所有会话之间共享资源。 (在 WebAPI OWIN Startup 中,这是从 startup.cs 调用的)。
尝试使用相对路径引用的其他方法时,出现此错误:
unable to resolve "edu/stanford/nlp/models/pos-tagger/english-left3words/english-left3words-distsim.tagger" as either class path, filename or URL
我不确定我是离解决方案更近了还是更远了。 这是我的 ASP.NET WebAPI 项目的根目录:
但是,我收到错误消息:
Unhandled Execution Error
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: java.lang.reflect.InvocationTargetException:
Source Error:
Line 43: // We should change current directory, so StanfordCoreNLP could find all the model files automatically Line 44: Directory.SetCurrentDirectory(HostingEnvironment.MapPath(ModelLocation)); Line 45: pipeline = new StanfordCoreNLP(props); Line 46: } Line 47: finally
Source File: D:\xxx\xxx\xxx\NLP.cs Line: 45
Stack Trace:
[InvocationTargetException] __(Object[] ) +444
FastConstructorAccessorImpl.newInstance(Object[] args) +28
java.lang.reflect.Constructor.newInstance(Object[] initargs, CallerID ) +133 edu.stanford.nlp.util.ClassFactory.createInstance(Object[] params) +108[ClassCreationException: MetaClass couldn't create public edu.stanford.nlp.time.TimeExpressionExtractorImpl(java.lang.String,java.util.Properties) with 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(String className, Object[] arguments) +71[ReflectionLoadingException: Error creating edu.stanford.nlp.time.TimeExpressionExtractorImpl]
edu.stanford.nlp.util.ReflectionLoading.loadByReflection(String className, Object[] arguments) +232
edu.stanford.nlp.time.TimeExpressionExtractorFactory.create(String className, String name, Properties props) +80
edu.stanford.nlp.time.TimeExpressionExtractorFactory.createExtractor(String name, Properties props) +34
edu.stanford.nlp.ie.regexp.NumberSequenceClassifier..ctor(Properties props, Boolean useSUTime, Properties sutimeProps) +57
edu.stanford.nlp.ie.NERClassifierCombiner..ctor(Boolean applyNumericClassifiers, Boolean useSUTime, Properties nscProps, String[] loadPaths) +129
edu.stanford.nlp.pipeline.AnnotatorImplementations.ner(Properties properties) +454 edu.stanford.nlp.pipeline.6.create() +46
edu.stanford.nlp.pipeline.AnnotatorPool.get(String name) +163
edu.stanford.nlp.pipeline.StanfordCoreNLP.construct(Properties , Boolean , AnnotatorImplementations ) +555
edu.stanford.nlp.pipeline.StanfordCoreNLP..ctor(Properties props, Boolean enforceRequirements) +55
edu.stanford.nlp.pipeline.StanfordCoreNLP..ctor(Properties props) +76 XXX.XXX.NLP.Start(String modelLocation) in D:\xxx\xxx\xxx\NLP.cs:45
XXX.XXX.Startup.Configuration(IAppBuilder app) in D:\xxx\xxx\xxx\Startup.cs:16[TargetInvocationException: Exception has been thrown by the target of aninvocation.] System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) +0
System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) +128
System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +146 Owin.Loader.<>c__DisplayClass12.b__b(IAppBuilder builder) +93
Owin.Loader.<>c__DisplayClass1.b__0(IAppBuilder builder) +209
Microsoft.Owin.Host.SystemWeb.OwinAppContext.Initialize(Action 1 startup) +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 context) +172
System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +618
System.Web.HttpApplication.InitSpecial(HttpApplicationState state, 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): Exception has been thrown by the target of an invocation.]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +579
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +120 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +712
这是我的代码(NLP.Start() 在 startup.cs 中被调用):
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());
}
}
}
经过一番摸索,我找到了同样问题的解决方案。
https://github.com/sergey-tihon/Stanford.NLP.NET/issues/11
如果您不愿意通读线程,这里是基本答案。修改这行代码
props.setProperty("sutime.binders", "0");
到
props.setProperty("ner.useSUTime", "0");