繁体   English   中英

opennlp.tools.postag.POSTaggerME.train() java.lang.NullPointerException

[英]opennlp.tools.postag.POSTaggerME.train() java.lang.NullPointerException

有同样的问题! 我得到InputSteram = null ,我使用了 IntelliJ IDEA,OpenNLP 1.9.1。 在 Ubuntu 18.04

    public void makeDataTrainingModel() {
    model = null;
    System.out.println("POS model started");
    //InputStream dataIn = null;
    InputStreamFactory dataIn = null;
    try {
        dataIn = new InputStreamFactory() {
            public InputStream createInputStream() throws IOException {
                return NLPClassifier.class.getResourceAsStream("/home/int/src          
    /main/resources/en-pos.txt");
            }
        };
        //I get null pointer here in dataIn
        ObjectStream<String> lineStream = new PlainTextByLineStream((InputStreamFactory) , "UTF-8");
        ObjectStream<POSSample> sampleStream = new WordTagSampleStream(lineStream);
     **//This train part IS NOT WORK ?**
        model = POSTaggerME.train("en", sampleStream, TrainingParameters.defaultParams(), null);
    } catch (IOException e) {
        // Failed to read or parse training data, training failed
        e.printStackTrace();
    } finally {
        if (dataIn != null) {
           
            System.out.println("InputStreamFactory was not created!");
        }
    }
    System.out.println("POS model done...");
    System.out.println("Success generate model...");
    //write Data model
    OutputStream modelOut = null;
    try {
        String currentDir = new File("").getAbsolutePath();
        modelOut = new BufferedOutputStream(new FileOutputStream(currentDir + "//src//main//resources//example-bad-model.dat"));


        model.serialize(modelOut);
    } catch (IOException e) {
        // Failed to save model
        e.printStackTrace();
    } finally {
        if (modelOut != null) {
            try {
                modelOut.close();
            } catch (IOException e) {
                // Failed to correctly save model.
                // Written model might be invalid.
                e.printStackTrace();
            }
        }
    }
    System.out.println("Model generated and treated successfully...");
}

我在 inputStream 中得到 null 指针和错误... InputStreamFactory 没有创建!

    Exception in thread "main" java.lang.NullPointerException
    at java.io.Reader.<init>(Reader.java:78)
    at java.io.InputStreamReader.<init>(InputStreamReader.java:113)
    at     
    opennlp.tools.util.PlainTextByLineStream.reset(PlainTextByLineStream.java:57)
    at opennlp.tools.util.PlainTextByLineStream.<init>   
    (PlainTextByLineStream.java:48)
    at opennlp.tools.util.PlainTextByLineStream.<init>    
   (PlainTextByLineStream.java:39)

   at NLPClassifier.makeDataTrainingModel(NLPClassifier.java:98)
   at NlpProductClassifier.main(NlpProductClassifier.java:39)

数据如下所示:
利润_利润外壳_环境 384912_CD bucks_currency
工资_利润手指_身体 913964_CD 美元_货币
利润_利润信仰_法律 3726_CD rur_currency
利润_利润游戏_娱乐 897444_CD 美元_货币
got_buy gift_jewelery 534841_CD rub_currency

**为什么线程没有打开并抛出异常?**

如果getResourceAsStream返回null ,则表示未找到资源。

You should check for null and do something else, such as throwing an exception ( IOException or FileNotFoundException in this case, since IOException and subclasses are allowed by the throws declaration) - you shouldn't let it pass the null to the rest of your code .

NLPClassifier.class.getResourceAsStream("/home/int/src/main/resources/en-pos.txt")将不起作用,因为资源与 Java 包具有相同的结构,只是将点替换为斜杠。 它不是文件系统中的路径。

将其更改为: getResourceAsStream("/en-pos.txt") (因为您的文件位于 package 层次结构的根目录)

正如 Erwin Bolwidt 所说,我更改了我的代码:

    /** I commented this part
    return NLPClassifier.class.getResourceAsStream("/home/interceptor/src/main/resources/en-pos.txt");
    */
    /**
     Add this location of my resoures:
     /Project/src/main/resources
    */
    return getClass().getClassLoader().getResourceAsStream("en-pos.txt");

之后,我发现Apache OpenNLP: java.io.FileInputStream 不能转换为 opennlp.tools .但使用 other.Input 方法 @schrieveslaach 说

您需要一个 InputStreamFactory 实例来检索您的 InputStream。 此外, TokenNameFinderFactory 不能是 null ! 像这个posFactory - 不能为空!

    /**
     *  Factory must not be a null. Add posModel.getFactory()
     *  model = POSTaggerME.train("en", sampleStream, TrainingParameters.defaultParams(), null);
     */
     model = POSTaggerME.train("en", sampleStream, TrainingParameters.defaultParams(), posModel.getFactory());

项目的完整代码在 repo https://github.com/AlexTitovWork/NLPclassifier

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM