在OpenNLP中使用自定义NER模型时如何处理'ExtensionNotLoadedException'?

时间:2016-02-04 18:00:01

标签: nlp text-mining opennlp named-entity-recognition

我正在尝试执行OpenNLP模型,我已经为我的自定义名称进行了培训。我执行了以下命令来制作自定义模型:

  

opennlp TokenNameFinderTrainer -encoding UTF-8 -lang en -data c:\ Users \ nshah \ Desktop \ 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 个答案:

答案 0 :(得分:1)

这是因为您已经使用不同版本的opennlp说(1.6.0)训练并创建了您的模型model.bin并且您在代码中使用了说1.5.3,这个错误发生是因为使用不同版本训练的模型。检查您在代码中使用的opennlp版本(检查.classpath)

希望这有帮助!干杯!