加载持久化的分词器模型报空指针问题

何博士您好,
我在进行实体识别,调用已经训练好的分词器模型、词性标注模型时候,报NullPointerException。
我浏览了github相关issue何论坛,进行了可能解决方案的试错,但是不理想。后来我跟源码,发现LinearModel类中的ByteArray经过处理后的大小没有最终的特征权重parameter的元素多,导致的后边继续获取元素为空。。。。。。我觉得我没找到问题的根本原因,所以特来请教,麻烦您了。
下边是我的代码:

    以下是异常信息:

Exception in thread “main” java.lang.NullPointerException
at com.hankcs.hanlp.corpus.io.ByteArrayFileStream.ensureAvailableBytes(ByteArrayFileStream.java:82)
at com.hankcs.hanlp.corpus.io.ByteArrayStream.nextInt(ByteArrayStream.java:56)
at com.hankcs.hanlp.model.perceptron.feature.FeatureMap.loadTagSet(FeatureMap.java:99)
at com.hankcs.hanlp.model.perceptron.feature.ImmutableFeatureMDatMap.load(ImmutableFeatureMDatMap.java:92)
at com.hankcs.hanlp.model.perceptron.model.LinearModel.load(LinearModel.java:421)
at com.hankcs.hanlp.model.crf.LogLinearModel.load(LogLinearModel.java:58)
at com.hankcs.hanlp.model.perceptron.model.LinearModel.load(LinearModel.java:388)
at com.hankcs.hanlp.model.crf.LogLinearModel.(LogLinearModel.java:83)
at com.hankcs.hanlp.model.crf.CRFTagger.(CRFTagger.java:41)
at com.hankcs.hanlp.model.crf.CRFSegmenter.(CRFSegmenter.java:47)
at com.wf.ner.Demo.loadCrfModel(Demo.java:80)
at com.wf.ner.Demo.main(Demo.java:51)

Process finished with exit code 1

这得看你是怎么训练出来的,crf模型只兼容txt格式。

何博士,训练CRF模型以及CRF分词器、词性标注器模型,我如何把模型保存为txt格式呢,我的代码如下,但是reload模型的时候报txt模型文件中数据格式不对:

reload模型的时候报如下错误:
Exception in thread “main” java.lang.NumberFormatException: For input string: “@”
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at com.hankcs.hanlp.model.crf.LogLinearModel.convert(LogLinearModel.java:124)
at com.hankcs.hanlp.model.crf.LogLinearModel.(LogLinearModel.java:101)
at com.hankcs.hanlp.model.crf.CRFTagger.(CRFTagger.java:41)
at com.hankcs.hanlp.model.crf.CRFSegmenter.(CRFSegmenter.java:47)
at com.wf.ner.Demo.loadCrfModel(Demo.java:81)
at com.wf.ner.Demo.main(Demo.java:51)

Process finished with exit code 1


我尝试了,训练模型的时候就将模型保存为txt格式,而不是bin格式,但是报相同的错误。具体代码如下:
LinearModel cwsModel = new CWSTrainer().train(trainPath, modelPath.replace(“model.bin”, “cws.txt”)).getModel();

参考