java - Apache OpenNLP 错误它不加载 en-pos-maxent.bin

标签 java apache opennlp

我正在尝试使用 Apache OpenNLP POSTagger 示例代码,但出现错误,下面是代码

public String[] SentenceDetect(String qwe) throws IOException 
 {

POSModel model = new POSModelLoader().load(new File("/home/jebard/chabacano/Chabacano1/src/en-pos-maxent.bin"));
PerformanceMonitor perfMon = new PerformanceMonitor(System.err, "sent");
POSTaggerME tagger = new POSTaggerME(model);

String input = "Hi. How are you? This is Mike.";
ObjectStream<String> lineStream = new PlainTextByLineStream(
        new StringReader(input));
perfMon.start();
String line;
while ((line = lineStream.read()) != null) {

    String whitespaceTokenizerLine[] = WhitespaceTokenizer.INSTANCE
            .tokenize(line);
    String[] tags = tagger.tag(whitespaceTokenizerLine);

    POSSample sample = new POSSample(whitespaceTokenizerLine, tags);
    System.out.println(sample.toString());

    perfMon.incrementCounter();
}
perfMon.stopAndPrintFinalResult();

这行错误

.load(new File("/home/jebard/chabacano/Chabacano1/src/en-pos-maxent.bin")

ModelLoader 类型中的方法加载(java.io.File)不适用于参数(org.apache.tomcat.jni.File)

最佳答案

这实际上不是 OpenNLP 中的错误。这是您代码中的错误,因为您从包(又名命名空间)org.apache.tomcat.jni.File 加载类 File

然而,API of OpenNLP要求您使用标准 JDK 包 java.io 中的类 File,即您应该导入 java.io.File相反。

一般来说,这应该可以解决您的问题。

重要提示

您应该迁移您的代码,因为不应通过 POSModelLoader 加载模型

Loads a POS Tagger Model for the command line tools.

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

相反,您可以使用构造函数 POSModel(InputStream in) 通过引用实际模型文件的 InputStream 加载您的模型文件。

此外,POSModelLoader 类仅出现在以前的 OpenNLP 版本中(版本 <= 1.5.x)。在最新的 OpenNLP 版本 1.6.0 中,它被完全删除了。相反,您现在可以而且应该使用 POSModel class 的构造函数加载/初始化您需要的模型。

关于java - Apache OpenNLP 错误它不加载 en-pos-maxent.bin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28314118/

相关文章:

linux - 如何在 ubuntu 上停止 apache 服务器

java - 无法从android中的tomcat服务器访问我的WSDL文件

java - 上传图片时出现问题

java - java中输入文本、输入密码、单选按钮、复选框、文本区域等

java - 如何调整这3个线程的控制流程?

elasticsearch - Elasticsearch中的命名实体提取

java - Android 中的 OpenNLP 在尝试初始化 posModel 时出现 FileNotFoundException

java - Java 中的 executeUpdate 方法是线程安全的吗?

linux - 安装期间 Google Sitemap 生成器错误 'is not a supported Apache binary or control script'

OpenNLP NameFinder 训练, "Found unexpected annotation"