我試圖使用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