斯坦福POS标记器在servlet中不起作用

时间:2015-03-15 03:08:40

标签: servlets stanford-nlp tomcat8 pos-tagger

我正在尝试在Java servlet中使用Stanford POS Tagger。我在J2EE中创建了动态网页项目,并按照本教程包含了jar文件的构建路径。 http://www.galalaly.me/index.php/2011/05/tagging-text-with-stanford-pos-tagger-in-java-applications/

如果我创建一个单独的java文件,我可以让POS Tagger工作,如下所示。

public static String tagger(String sample) throws IOException, ClassNotFoundException {

 // Initialize the tagger
 MaxentTagger tagger = new MaxentTagger("models/english-left3words-distsim.tagger");

 // The tagged string
 String tagged = tagger.tagString(sample);

 // Output the result
 return tagged;
 }

public static void main(String[] args){
    try {
        System.out.println(tagger("This is a sample."));
    } catch (ClassNotFoundException | IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

这意味着POS tagger在简单Java类的main方法中工作,我将其命名为Taggers.java。但是当我尝试在servlet中实例化Tagger.java类的对象时,我无法使它工作。以下代码是一个servlet,我试图在其中实例化Tagger.java的对象。

/**
 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
 */
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException  {
    PrintWriter out = response.getWriter(); 
    try {
        out.println(Tagger.tagger("This is a sample sentence."));
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

当我在tomcat-8上运行上面的servlet时,它会显示以下异常。

  

SEVERE:servlet [com.exp.pkg.ExpServlet]的Servlet.service()在路径[/ webProj1]的上下文中引发了异常[Servlet执行引发异常]的根本原因   java.lang.ClassNotFoundException:edu.stanford.nlp.tagger.maxent.MaxentTagger       在org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1305)       在org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1157)       在com.exp.pkg.Tagger.tagger(Tagger.java:17)       在com.exp.pkg.ExpServlet.service(ExpServlet.java:34)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:725)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)       在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)       在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)       在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)       在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)       在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)       at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)       在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)       在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)       在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086)       at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:659)       在org.apache.coyote.http11.Http11NioProtocol $ Http11ConnectionHandler.process(Http11NioProtocol.java:223)       在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1558)       在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1515)       at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)       at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source)       at org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)       在java.lang.Thread.run(未知来源)

设置classpath ="。; ..; C:\ Users \ Faisal \ workspace_EE \ webProj1 \ tagger \ english-left3words-distsim.ta gger; C:\ stanford-postagger-2015-01 -29 \斯坦福-postagger-2015年1月30日\斯坦福-交阿格; C:\ Apache的Tomcat的8 \ Apache的Tomcat的8.0.20 \ lib中\ servlet的API"

堆栈跟踪如下:

SEVERE:带有路径[/ webProj1]的上下文中servlet [com.exp.pkg.ExpServlet]的Servlet.service()抛出异常edu.stanford.nlp.io.RuntimeIOException:在edu上加载标记器模型时出现无法恢复的错误.stanford.nlp.tagger.maxent.MaxentTagger.readModelAndInit(MaxentTagger.java:769)在edu.stanford.nlp.tagger.maxent.MaxentTagger。(MaxentTagger.java:297)在edu.stanford.nlp.tagger.maxent。 MaxentTagger。(MaxentTagger.java:262)位于com.exp.pkg.Tagger.tagger(Tagger.java:17)的com.exp.pkg.ExpServlet.service(ExpServlet.java:34),位于javax.servlet.http。位于org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)的HttpServlet.service(HttpServlet.java:725)org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)at org .apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)在org.apache.catalina.core.Applica forFilterChain.doFilter(ApplicationFilterChain.java:206)org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)at org .apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve)的.java:79)在org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)在org.apache.catalina。 connector.CoyoteAdapter.service(CoyoteAdapter.java:516)在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086)在org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:659)在org.apache.coyote.http11.Http11NioProtocol $ Http11ConnectionHandl er.process(Http11NioProtocol.java:223)org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1558)org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run( NioEndpoint.java:1515)java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source)at org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable .run(TaskThread.java:61)at java.lang.Thread.run(Unknown Source)引起:java.io.IOException:无法解析" tagger / english-bidirectional-distsim.tagger"如在edu.stanford.nlp.tagger.maxent.MaxentTagger.readModelAndInit(MaxentTagger.java:764)任一类路径,文件名或URL在edu.stanford.nlp.io.IOUtils.getInputStreamFromURLOrClasspathOrFileSystem(IOUtils.java:463).. .27更多

2 个答案:

答案 0 :(得分:0)

您需要确保您的类路径正确。

从您更新的帖子中,您似乎没有在类路径上拥有正确的模型路径。您不应该将.tagger文件直接添加到类路径中;添加包含tagger文件夹的目录。如果您的文件设置如下所示:

C:\mystuff
    tagger\
        english-left3words-distsim.tagger
        english-bidirectional-distsim.tagger

然后你应该把目录C:\mystuff放在你的类路径上。

答案 1 :(得分:0)

  

您需要确保您的类路径正确。

     

从您更新的帖子中,您似乎没有合适的型号   类路径上的路径。您不应该直接添加.tagger文件   你的班级路径;添加包含tagger文件夹的目录。如果   您的文件设置如下所示:

     

C:\的MyStuff       恶搞\           英语left3words-distsim.tagger           英语 - 双向-distsim.tagger然后你应该把   你的类路径上的目录C:\ mystuff。

根据我之前发布的link的说明,我在项目的根文件夹中创建了名为tagger的文件夹,例如(C:\ Users \ Faisal \ workspace_EE \ webProj1 \ tagger),我把模型放在哪里" english-left3words-distsim.tagger"使用它的教程链接指示的.props文件。 现在,因为您建议只将文件夹添加到类路径中,所以我的类路径变为" C:\ Users \ Faisal \ workspace_EE \ webProj1 \; C:\ stanford-postagger-2015-01-29 \ stanford-postagger-2015 -01-30 \斯坦福-postagger; C:\ Apache的Tomcat的8 \ Apache的Tomcat的8.0.20 \ lib中\ servlet的API"

这一次,我得到了以下堆栈跟踪:

SEVERE: Servlet.service() for servlet [com.exp.pkg.ExpServlet] in context with path [/webProj1] threw exception
edu.stanford.nlp.io.RuntimeIOException: Unrecoverable error while loading a tagger model
    at edu.stanford.nlp.tagger.maxent.MaxentTagger.readModelAndInit(MaxentTagger.java:769)
    at edu.stanford.nlp.tagger.maxent.MaxentTagger.<init>(MaxentTagger.java:297)
    at edu.stanford.nlp.tagger.maxent.MaxentTagger.<init>(MaxentTagger.java:262)
    at com.exp.pkg.Tagger.tagger(Tagger.java:17)
    at com.exp.pkg.ExpServlet.service(ExpServlet.java:34)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659)
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.io.IOException: Unable to resolve "tagger/english-bidirectional-distsim.tagger" as either class path, filename or URL
    at edu.stanford.nlp.io.IOUtils.getInputStreamFromURLOrClasspathOrFileSystem(IOUtils.java:463)
    at edu.stanford.nlp.tagger.maxent.MaxentTagger.readModelAndInit(MaxentTagger.java:764)
    ... 27 more