2016-02-04 85 views
2

我正在嘗試執行我已爲我的自定義名稱進行培訓的OpenNLP模型。我執行下面的命令進行定製的模型:在OpenNLP中使用自定義NER模型時如何處理'ExtensionNotLoadedException'?

opennlp TokenNameFinderTrainer -encoding UTF-8琅恩-data C:\用戶\ nshah \桌面\ EN-NER-person.train -model EN-ner- personName.bin

我能夠成功地創建一個名爲EN-NER-personName.bin模型。

我現在想執行如下我的程序:

import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 

import opennlp.tools.namefind.NameFinderME; 
import opennlp.tools.namefind.TokenNameFinderModel; 
import opennlp.tools.util.InvalidFormatException; 
import opennlp.tools.util.Span; 

public class NameFinder { 

    public String nameFind(String inputText){ 

     String finalNames = ""; 
     try { 

      TokenNameFinderModel tokenNameFinderModel = new TokenNameFinderModel(new FileInputStream("D://NISUM_OFFICIAL_WORKSPACE//opennlp//src//models//en-ner-personName.bin")); 
      NameFinderME nameFinderME = new NameFinderME(tokenNameFinderModel); 
      Tokenization tokenize = new Tokenization(); 
      String[] tokens = tokenize.tokenization(inputText); 
      Span drugSp[] = nameFinderME.find(tokens); 
      String [] sp = Span.spansToStrings(drugSp, tokens); 
      StringBuilder string = new StringBuilder(); 
      if(sp.length!=0){ 
       for(int i=0;i<=sp.length;i++){ 
        string = string.append(sp[i]+"\n"); 
       } 
       finalNames = string.toString(); 
      } 
     } catch (InvalidFormatException e) { 
      e.printStackTrace(); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return finalNames; 
    } 

} 

但上述程序執行後,我收到的控制檯下面例外提到:

Could not instantiate the opennlp.tools.namefind.TokenNameFinderFactory. The initialization throw an exception. 
opennlp.tools.util.ext.ExtensionNotLoadedException: Unable to find implementation for opennlp.tools.util.BaseToolFactory, the class or service opennlp.tools.namefind.TokenNameFinderFactory could not be located! 
    at opennlp.tools.util.ext.ExtensionLoader.instantiateExtension(ExtensionLoader.java:97) 
    at opennlp.tools.util.BaseToolFactory.create(BaseToolFactory.java:106) 
    at opennlp.tools.util.model.BaseModel.initializeFactory(BaseModel.java:254) 
    at opennlp.tools.util.model.BaseModel.loadModel(BaseModel.java:237) 
    at opennlp.tools.util.model.BaseModel.<init>(BaseModel.java:181) 
    at opennlp.tools.namefind.TokenNameFinderModel.<init>(TokenNameFinderModel.java:110) 
    at com.naimesh.opennlp.DrugNameFinder.drugNameFind(DrugNameFinder.java:19) 
    at com.naimesh.opennlp.TextFinder.main(TextFinder.java:18) 
Exception in thread "main" java.lang.IllegalArgumentException: opennlp.tools.util.InvalidFormatException: Could not instantiate the opennlp.tools.namefind.TokenNameFinderFactory. The initialization throw an exception. 
    at opennlp.tools.util.model.BaseModel.initializeFactory(BaseModel.java:256) 
    at opennlp.tools.util.model.BaseModel.loadModel(BaseModel.java:237) 
    at opennlp.tools.util.model.BaseModel.<init>(BaseModel.java:181) 
    at opennlp.tools.namefind.TokenNameFinderModel.<init>(TokenNameFinderModel.java:110) 
    at com.naimesh.opennlp.DrugNameFinder.drugNameFind(DrugNameFinder.java:19) 
    at com.naimesh.opennlp.TextFinder.main(TextFinder.java:18) 
Caused by: opennlp.tools.util.InvalidFormatException: Could not instantiate the opennlp.tools.namefind.TokenNameFinderFactory. The initialization throw an exception. 
    at opennlp.tools.util.BaseToolFactory.create(BaseToolFactory.java:117) 
    at opennlp.tools.util.model.BaseModel.initializeFactory(BaseModel.java:254) 
    ... 5 more 
Caused by: opennlp.tools.util.ext.ExtensionNotLoadedException: Unable to find implementation for opennlp.tools.util.BaseToolFactory, the class or service opennlp.tools.namefind.TokenNameFinderFactory could not be located! 
    at opennlp.tools.util.ext.ExtensionLoader.instantiateExtension(ExtensionLoader.java:97) 
    at opennlp.tools.util.BaseToolFactory.create(BaseToolFactory.java:106) 
    ... 6 more 

請有人可以幫助我在這個問題上,我嘗試了很多事情並閱讀博客,但找不到有用的東西。如果有人能提供有用的解決方案,那將會非常感激。

回答

1

這是因爲您已經使用不同版本的opennlp say(1.6.0)訓練並創建了模型model.bin,並且您在代碼中使用了1.5.3,因爲使用了模型訓練使用不同的版本。檢查你在代碼中使用哪個版本的opennlp(檢查.classpath)

希望這有助於!乾杯!

相關問題