如何在Eclipse Luna的Java项目中使用Apache OpenNLP?

时间:2015-09-15 05:03:45

标签: java apache nlp opennlp

我从opennlp下载了Apache opennlp,并在解压缩后在引用的库中添加了两个.jar文件。写一个简单的代码:

    modelIn = new FileInputStream("en-sent.bin");

输出:

java.io.FileNotFoundException: en-sent.bin (The system cannot find the file specified)
 at java.io.FileInputStream.open0(Native Method)
 at java.io.FileInputStream.open(Unknown Source)
 at java.io.FileInputStream.<init>(Unknown Source)
 at java.io.FileInputStream.<init>(Unknown Source)
 at Home.main(Home.java:16)

1 个答案:

答案 0 :(得分:0)

我建议将Maven用于那些依赖项。

要将Sentence Detection API集成到Java项目中,您需要从http://opennlp.sourceforge.net/models-1.5/下载预先训练的模型en-sent.bin并将其添加到类路径或指向文件系统的完整路径

然后只需遵循Apache OpenNLP文档:

InputStream modelIn = new FileInputStream("en-sent.bin");

try {
  SentenceModel model = new SentenceModel(modelIn);
}
catch (IOException e) {
  e.printStackTrace();
}
finally {
  if (modelIn != null) {
    try {
      modelIn.close();
    }
    catch (IOException e) {
    }
  }
}

我真的建议您在此处详细阅读:http://blog.dpdearing.com/2011/05/opennlp-1-5-0-basics-sentence-detection-and-tokenizing/并在此处:http://www.javaworld.com/article/2077352/java-se/smartly-load-your-properties.html