Java ApacheOpenNLP bug它没有';t加载en-pos-maxent.bin

Java ApacheOpenNLP bug它没有';t加载en-pos-maxent.bin,java,apache,opennlp,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 p

我试图使用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();
public String[]语句检测(String qwe)抛出IOException
{
POSModel model=new POSModelLoader().load(新文件(“/home/jebard/chabacano/Chabacano1/src/en pos maxent.bin”);
PerformanceMonitor perfMon=新的PerformanceMonitor(System.err,“已发送”);
POSTaggerME tagger=新的POSTaggerME(型号);
String input=“嗨,你好吗?我是迈克。”;
ObjectStream lineStream=新的明文ByLineStream(
新的StringReader(输入));
perfMon.start();
弦线;
而((line=lineStream.read())!=null){
字符串whitespaceTokenizerLine[]=WhitespaceTokenizer.INSTANCE
.标记化(行);
String[]tags=tagger.tag(whitespaceTokenizerLine);
POSSample sample=新的POSSample(whitespaceTokenizerLine,tags);
System.out.println(sample.toString());
perfMon.incrementCounter();
}
perfMon.stop和printfinalResult();
这一行有错误

.load(新文件(“/home/jebard/chabacano/Chabacano1/src/en pos maxent.bin”)


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

这实际上不是OpenNLP中的错误。这是代码中的错误,因为您从包(aka namespace)
org.apache.tomcat.jni.File
加载类
文件

但是,请求您使用标准JDK包
java.io
中的类
文件
,也就是说,您应该导入

一般来说,这会解决您的问题

重要提示

您应该迁移代码,因为不应该通过
POSModelLoader

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

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

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


此外,类
POSModelLoader
仅出现在OpenNLP的早期版本中(版本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) {}