2016-02-04 132 views
2

我试图使用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的图片,但我不知道在这里使用打印屏幕按钮):

enter image description here

整个错误是:

`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

回答

2

你需要把路径更改为

.getResourceAsStream("en-sent.bin"); 

由于阅读的getResourceAsStream一个包,并将这些文件(.bin文件)在你的源文件夹。

0

变化

.getResourceAsStream("Tokenizer/models/en-sent.bin"); 

.getResourceAsStream("models/en-sent.bin"); 

你有“标记生成器”,其中是您的项目,这是无关紧要的,所以你只是删除该位名称的路径! :)

+0

@FumnleWumble我试过但我得到了同样的错误 – Suri

+0

@Suri你能告诉我从控制台的整个错误?只需复制粘贴即可。我想知道它说的是什么。 –

+0

@FumnleWumble的代码行导致从控制台异常和整体错误添加在我的问题。 – Suri

0

由于您使用的是getClassLoader().getResourceAsStream(),该文件必须位于您的类路径中。右键单击eclipse“build path” - >“用作源文件夹”中的“models”文件夹。然后确保您的路径与文件夹结构匹配。如果您保留照片中的内容,那就是“models/en-sent.bin”。

如果您希望这些.bin文件通常驻留在建.jar文件外,你应该使用构造FileInputStream,而不是可以采取绝对的文件系统路径。

+0

我试过了你的建议,但我得到了同样的错误 – Suri