斯坦福NLP Lexparser loadModel()

时间:2017-05-25 09:48:37

标签: c# stanford-nlp java-io objectinputstream

我目前正在使用以下C#代码成功创建我的Lexparser:

return LexicalizedParser.loadModel(projectDir + @"StanfordResources/lexparser/englishPCFG.ser.gz");

但由于部署原因,我宁愿将'englishPCFG.ser.gz'文件作为某种资源嵌入到程序集中或作为Resource.resx。

所以我尝试读取我的byte []文件:

ObjectInputStream stream = new ObjectInputStream(new ByteArrayInputStream(Resource.englishPCFG_ser));
        return LexicalizedParser.loadModel(stream);

但我收到以下错误:

java.io.StreamCorruptedException: invalid stream header: 1F8B0800

是否有另一种方法来加载它而不是从文件路径加载或者我做的很傻?

1 个答案:

答案 0 :(得分:1)

1F8B0800是GZIP标头,考虑到您尝试阅读的文件的名称,这是有意义的。因此,您需要将java.util.zip.GZIPInputStream放在ByteArrayInputStreamObjectInputStream之间:

new ObjectInputStream(new GZIPInputStream(new ByteArrayInputStream(Resource.englishPCFG_ser)))