Apache OpenNLP bug 它不会加载 en-pos-maxent.bin



我正在尝试使用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 类型的方法 load(java.io.File) 不适用于参数(org.apache.tomcat.jni.File)

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

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

通常,这应该可以解决您的问题。

重要提示

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

加载命令行工具的 POS 标记器模型。

注意:请勿使用此类,仅限内部使用

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

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

XML 解析存在一些问题。试试这个,它对我有用。

    System.setProperty("org.xml.sax.driver", "org.xmlpull.v1.sax2.Driver");
    try {
        AssetFileDescriptor fileDescriptor = 
        context.getAssets().openFd("en_pos_maxent.bin");
        FileInputStream inputStream = fileDescriptor.createInputStream();
        POSModel posModel = new POSModel(inputStream);
        posTaggerME = new POSTaggerME(posModel);
    } catch (Exception e) {}

最新更新