Search code examples
c#stanford-nlpjava-ioobjectinputstream

Stanford NLP Lexparser loadModel()


I'm currently using the below C# code to create my Lexparser with success:

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

But due to deployment reasons I would rather embed the 'englishPCFG.ser.gz' file as some sort of resource either into the assembly or as a Resource.resx.

So I try to read my byte[] file as so:

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

But I get the below error:

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

Is there another way to load this rather than from a file path or am I doing a silly?


Solution

  • 1F8B0800 is the GZIP header, which makes sense, given the name of the file you're trying to read. So you need to put java.util.zip.GZIPInputStream between the ByteArrayInputStream and ObjectInputStream:

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