gpt4 book ai didi

java - 为什么无法从 WEB-INF 文件夹中加载 POSModel 文件?

转载 作者:行者123 更新时间:2023-11-28 22:37:13 25 4
gpt4 key购买 nike

我正在为我的 Web 项目使用 Spring MVC。我将模型文件放在 WEB-INF 目录中

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 服务器上时,出现错误

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

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

最佳答案

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

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

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 的 Javadoc还有这个StackOverflow post .

重要提示

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

Loads a POS Tagger Model for the command line tools.

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

因此,在 Web 上下文中加载 POSModel 应该以不同的方式完成:通过可用的 constructors of that class 之一。 .您可以像这样重新表述上面的代码片段:

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 的要求,同时您可以进行适当的异常处理。此外,如果您的模型文件的资源路径引用仍然不清楚,您现在可以使用调试器。

希望对您有所帮助。

关于java - 为什么无法从 WEB-INF 文件夹中加载 POSModel 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32148328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com