Serialize a non-serializable Java object?

I am experimenting with the Stanford CoreNLP library, and I want to serialize the main object of the StanfordCoreNLP pipeline, although it throws a java.io.NotSerializableException.

Full story: Whenever I run my implementation, it takes ~ 15 seconds to load annotators and classifiers into memory. The final process is about 600 MB in memory (it is small enough to be saved in my case). I want to save this pipeline after creating it for the first time, so I can just read it in memory afterwards.

However, it throws a NotSerializableException. I tried to make a trivial subclass that implements Serializable, but StanfordCoreNLP has annotator and classifier properties that do not implement this interface, and I cannot create subclasses for all of them.

Is there any Java library that will serialize an object that does not implement Serializable? I believe that he will have to go through his properties and do the same for any such 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());
    }
}
+5
source share
2 answers

, , (Tree, LexicalizedParser ..), , , (StanfordCoreNLP, LexicalizedParserQuery, CRFClassifier), . , , , .

, , . , StanfordCoreNLP 15 , Java- . NER Java-. ( , , POS, .) , Java ... Java , . , , . ( , , , , , . )

, , , .

+2

, , , , Serializable, , . , Jackson XStream.

, , Serializable, , , , . !

+1

All Articles