我试图使用OpenNLP库来使用它的sentencedetector,我尝试写下面的代码,但我得到了与此地址有关的异常en-sent.bin文件但我不知道如何解决此文件。线程“main”中的异常java.lang.IllegalArgumentException:中不能为null
import java.io.*;
import java.net.URL;
import opennlp.tools.sentdetect.SentenceDetectorME;
import opennlp.tools.sentdetect.SentenceModel;
public class SentenceDetect
{
private SentenceDetectorME sentenceDetector;
public void init()
{
/** Load and initialize the sentence detection model */
InputStream modelIn = null;
SentenceModel model = null;
try {
modelIn = SentenceDetect.class.getClassLoader().getResourceAsStream("Tokenizer/models/en-sent.bin");
model = new SentenceModel(modelIn); //*<- line 36*
}
catch (IOException e)
{
e.printStackTrace();
}
finally {
if (modelIn != null) {
try {
modelIn.close();
}
catch (IOException e) {}
}
}
sentenceDetector = new SentenceDetectorME(model);
}
public String[] getSentences(String longSentence)
{
return sentenceDetector.sentDetect(longSentence);
}
}
主类:
public static void main(String[] args)
{
SentenceDetect d = new SentenceDetect();
d.init(); ///*<- line 10*
String[] s = d.getSentences("This is sentence #1. This is Sentence #2");
System.out.println(s[0]); // Should be the first sentence
System.out.println(s[1]); // Should be the second sentence
}
下图显示了分层我的项目(抱歉我使用Ubuntu的图片,但我不知道在这里使用打印屏幕按钮):
整个错误是:
`Exception in thread "main" java.lang.IllegalArgumentException: in must not be null!
at opennlp.tools.util.model.BaseModel.<init>(BaseModel.java:179)
at opennlp.tools.sentdetect.SentenceModel.<init>(SentenceModel.java:95)
at SentenceDetect.init(SentenceDetect.java:36)
at Main.main(Main.java:10)`
我尝试了这些路径,但我得到了同样的错误:
- /Tokenizer/models/en-sent.bin
- /models/en-sent.bin
- 型号/ EN-发送的.bin
- /home/suri/workspace/2/Tokenizer/models/en-sent.bin
@FumnleWumble我试过但我得到了同样的错误 – Suri
@Suri你能告诉我从控制台的整个错误?只需复制粘贴即可。我想知道它说的是什么。 –
@FumnleWumble的代码行导致从控制台异常和整体错误添加在我的问题。 – Suri