I am using ObjectOutputStream to save object, but when I use .writeObject(this) to save it as a file, the material cannot be saved. The class I defined is already serializable.
public class LanguageModel implements Serializable {
private static LanguageModel lm_;
/* ******************************* */
//word -> count(w)
public static Dictionary unigramDict = new Dictionary();
//word_pair -> count(wi,wi+1)
public static Dictionary bigramDict = new Dictionary();
private static int wordIdCounter = 0;
/* ***************************** */
// Do not call constructor directly since this is a Singleton
private LanguageModel(String corpusFilePath) throws Exception {
constructDictionaries(corpusFilePath);
}
public void constructDictionaries(String corpusFilePath)
throws Exception {
...
}
// Saves the object (and all associated data) to disk
public void save() throws Exception{
FileOutputStream saveFile = new FileOutputStream(Config.languageModelFile);
ObjectOutputStream save = new ObjectOutputStream(saveFile);
save.writeObject(this);
save.close();
}
// Creates a new lm object from a corpus
public static LanguageModel create(String corpusFilePath) throws Exception {
if(lm_ == null ){
lm_ = new LanguageModel(corpusFilePath);
}
return lm_;
}
}
The class I defined is as follows:
import java.io.Serializable;
import java.util.HashMap;
public class Dictionary implements Serializable {
private int termCount;
private HashMap<String, Integer> map;
public int termCount() {
return termCount;
}
public Dictionary() {
termCount = 0;
map = new HashMap<String, Integer>();
}
...
}
When I try save.writeObject(unigramDict), it can save this variable properly. Since it is a large variable, I can simply check the size of the file. It is 5MB. When I switch to save.writeObject(this), the size of the file is only 53 Bytes.
save.writeObject(unigramDict)gives you 5MB, then you said thatsave.writeObject(unigramDict)gives you 53B. Which is it? By the way, you could use a debugger to check that theDictionaryobjects are actually being populated correctly before you save.unigramDictmay bethis, in the first case he save theMap, in the second case he save theLanguageModelobject without the static field :)staticmodifiers. Whoops.