为什么在 WEB-INF 文件夹中无法加载 POSModel 文件? [英] Why does the loading of a POSModel file not work from inside the WEB-INF folder?

查看:25
本文介绍了为什么在 WEB-INF 文件夹中无法加载 POSModel 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 Spring MVC 用于我的 Web 项目.我把模型文件放在WEB-INF目录下

I'm using Spring MVC for my web project. I placed the model files inside the WEB-INF directory

String taggerModelPath = "/WEB-INF/lib/en-pos-maxent.bin";
String chunkerModelPath = "/WEB-INF/lib/en-chunker.bin";

POSModel model = new POSModelLoader()
.load(new File(servletContext.getResource(taggerModelPath).toURI().getPath()));

这个工作的 Windows 环境.但是,当我将它部署到远程 Linux 服务器上时,出现错误

This worked Windows environment. However when I deployed it on my remote Linux server I got an error

HTTP 状态 500 - 请求处理失败;嵌套异常是 opennlp.tools.cmdline.TerminateToolException:POS Tagger 模型文件不存在!路径:/localhost/nlp/WEB-INF/lib/en-pos-maxent.bin

HTTP Status 500 - Request processing failed; nested exception is opennlp.tools.cmdline.TerminateToolException: The POS Tagger model file does not exist! Path: /localhost/nlp/WEB-INF/lib/en-pos-maxent.bin

访问文件资源的最佳方式是什么?谢谢

What is the best way to access file resources? Thanks

推荐答案

假设您使用的是 OpenNLP 1.5.3,那么您应该使用另一种加载资源文件的方式,该方式不使用硬"通过 URI 转换的路径引用.

Let's assume you're using OpenNLP 1.5.3, then you should use another way of loading resource files, which uses no "hard" path references via URI conversion.

假设在目录 WEB-INF 中存在另一个包含 OpenNLP 模型文件的目录 resources 的环境,您的代码片段应编写如下:

Given an environment in which within the directory WEB-INF another directory resources exists that contains your OpenNLP model files, you're code fragment should be written as follows:

String taggerModelPath = "/WEB-INF/resources/en-pos-maxent.bin";
String chunkerModelPath= "/WEB-INF/resources/en-chunker.bin";

POSModel model = new POSModelLoader().load(servletContext.getResourceAsStream(taggerModelPath));

有关 ServletContext#getResourceAsStream 和这个 StackOverflow 帖子.

重要提示

遗憾的是,您的代码还有其他问题.OpenNLP 类 POSModelLoader 仅供内部使用,请参阅官方 Javadoc for POSModelLoader:

Sadly, there are other issues with your code. The OpenNLP class POSModelLoader is only for internal use, see official Javadoc for POSModelLoader:

为命令行工具加载 POS 标记器模型.

Loads a POS Tagger Model for the command line tools.

注意:不要使用这个类,仅供内部使用!

Note: Do not use this class, internal use only!

因此,在 Web 上下文中加载 POSModel 应该以不同的方式完成:通过一个可用的 该类的构造函数.您可以像这样重新编写上面的代码片段:

Therefore, loading a POSModel in a Web context should be done differently: Via one of the available constructors of that class. You can reformulate the above code fragment like so:

try {
    InputStream in = servletContext.getResourceAsStream(taggerModelPath);
    POSModel posModel;
    if(in != null) {
        posModel = new POSModel(in);
        
        // from here, <posModel> is initialized and you can start playing with it...
        // ...
    }
    else {
        // resource file not found - whatever you want to do in this case
    }
}
catch (IOException | InvalidFormatException ex) {
    // proper exception handling here... cause: getResourcesAsStream or OpenNLP...
} 

这样,您就符合 OpenNLP API 的要求,同时您也可以进行适当的异常处理.此外,您现在可以使用调试器,以防您的模型文件的资源路径引用仍然不清楚.

This way, you're compliant with the OpenNLP API and at the same time you do proper exception handling. Moreover, you can now use a Debugger in case something remains unclear with the resource path references of your model files.

希望有帮助.

这篇关于为什么在 WEB-INF 文件夹中无法加载 POSModel 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆