I am experimenting with the Stanford CoreNLP library, and I want to serialize the main StanfordCoreNLP pipeline object, even though it throws a java.io.NotSerializableException.
Full story: Whenever I run my implementation, it takes ~15 seconds to load the pipeline annotators and classifiers into memory. The end process is about 600MB in memory (easily small enough to be stored in my case). I'd like to save this pipeline after creating it the first time, so I can just read it into memory afterwards.
However it throws a NotSerializableException. I tried making a trivial subclass that implements Serializable, but StanfordCoreNLP has annotator and classifier properties that don't implement this interface, and I can't make subclasses for all of them.
Is there any Java library that will serialize an object that doesn't implement Serializable? I suppose it would have to recurse through it's properties and do the same for any similar object.
The serialization code I tried:
static StanfordCoreNLP pipeline;
static String file = "/Users/ME/Desktop/pipeline.sav";
static StanfordCoreNLP pipeline() {
if (pipeline == null) {
try {
FileInputStream saveFile = new FileInputStream(file);
ObjectInputStream read = new ObjectInputStream(saveFile);
pipeline = (StanfordCoreNLP) read.readObject();
System.out.println("Pipeline loaded from file.");
read.close();
} catch (FileNotFoundException e) {
System.out.println("Cached pipeline not found. Creating new pipeline...");
Properties props = new Properties();
props.put("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
pipeline = new StanfordCoreNLP(props);
savePipeline(pipeline);
} catch (IOException e) {
System.err.println(e.getLocalizedMessage());
} catch (Exception e) {
System.err.println(e.getLocalizedMessage());
}
}
return pipeline;
}
static void savePipeline(StanfordCoreNLP pipeline) {
try {
FileOutputStream saveFile = new FileOutputStream(file);
ObjectOutputStream save = new ObjectOutputStream(saveFile);
save.writeObject(pipeline);
System.out.println("Pipeline saved to file.");
save.close();
} catch (FileNotFoundException e) {
System.out.println("Pipeline file not found during save.");
} catch (IOException e) {
System.err.println(e.getLocalizedMessage());
}
}