CRFClassifier:从流加载模型会给出异常“无效的流头:1F8B0800”

时间:2018-07-18 09:31:32

标签: stanford-nlp

我正在尝试从文件中加载CRFClassifier模型。这种方式有效:

// this works
classifier = CRFClassifier.getClassifier("../res/stanford-ner-2018-02-27/classifiers/english.all.3class.distsim.crf.ser.gz");

但是,当我想使用流媒体时,出现invalid stream header: 1F8B0800异常:

// this throws an exception
String modelResourcePath = "../res/stanford-ner-2018-02-27/classifiers/english.all.3class.distsim.crf.ser.gz";
BufferedInputStream stream = new BufferedInputStream(new FileInputStream(modelResourcePath));
classifier = CRFClassifier.getClassifier(stream);

例外:

Exception in thread "main" java.io.StreamCorruptedException: invalid stream header: 1F8B0800
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:866)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:358)
at edu.stanford.nlp.ie.AbstractSequenceClassifier.loadClassifier(AbstractSequenceClassifier.java:1473)
at edu.stanford.nlp.ie.AbstractSequenceClassifier.loadClassifier(AbstractSequenceClassifier.java:1456)
at edu.stanford.nlp.ie.crf.CRFClassifier.getClassifier(CRFClassifier.java:2890)
at com.sv.research.ner.stanford.StanfordEntityExtractor.<init>(StanfordEntityExtractor.java:34)
at com.sv.research.ner.stanford.StanfordEntityExtractor.main(StanfordEntityExtractor.java:59)

我希望两种方法都一样。我之所以要通过流进行加载,是因为最终我想使用以下方法从JAR资源中加载模型:

stream = ClassLoader.getSystemClassLoader().getResourceAsStream(modelResourcePath));

1 个答案:

答案 0 :(得分:0)

据我所知,通过GZIPInputStream对要使用的分类器进行了序列化。

所以您可以尝试反序列化它们的序列化方式吗,像这样:

BufferedInputStream stream = new BufferedInputStream(new GZIPInputStream(new FileInputStream(modelResourcePath)));

欢呼