I am having some problem with Serialization
Here goes my code for writing mClassifier object to a file:
FileOutputStream fileOut = new FileOutputStream("C:\\polarity.model");
ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
mClassifier.compileTo(objOut);
objOut.close();
It works fine and writes stuff to the file.
But there is a catch: myClassifier object is of type DynamicLMClassifier. compileTo method above however returns an instance of LMClassifier (superclass)
Here goes my code for reading the object:
FileInputStream in = new FileInputStream("C:\\polarity.model");
ObjectInputStream ois = new ObjectInputStream(in);
mClassifier = (DynamicLMClassifier)(ois.readObject());
ois.close();
When I read the object I typecast it to DynamicLMClassifier and it too works fine but I dont get the output I desired. While reading the object again should it not be typecasted to LMClassifier rather than DynamicLMClassifier. However if I do that, compiler complains that it should be of type DynamicLMClassifier.
Can the above be problems or am I doing something wrong some where else. I mean the code without serialization is working perfectly fine and I get the desired output, I mean when the object is in memory.
EDIT: Here is the complete code (just remove the serialization part in the train() and getSentiments() method and it works as intended), Also note that in (1) With Serialization I am not calling getSentiments() and I am just training i.e. calling the train () method (2) Now i have a serialized model after (1) and I am not calling the train() method just calling getSentiment() by just commenting out appropriate code in main:
public class PolarityBasic{
File mPolarityDir;
String[] mCategories;
DynamicLMClassifier<NGramProcessLM> mClassifier,readClassifier;
PolarityBasic(String[] args) {
System.out.println("\nBASIC POLARITY DEMO");
mPolarityDir = new File("C:\\review_polarity","txt_sentoken");
System.out.println("\nData Directory=" + mPolarityDir);
mCategories = mPolarityDir.list();
int nGram = 8;
mClassifier
= DynamicLMClassifier
.createNGramProcess(mCategories,nGram);
}
void run() throws ClassNotFoundException, IOException {
train();
}
boolean isTrainingFile(File file) {
return file.getName().charAt(2) != '9'; // test on fold 9
}
void train() throws IOException {
int numTrainingCases = 0;
int numTrainingChars = 0;
System.out.println("\nTraining.");
for (int i = 0; i < mCategories.length; ++i) {
String category = mCategories[i];
Classification classification
= new Classification(category);
File file = new File(mPolarityDir,mCategories[i]);
File[] trainFiles = file.listFiles();
for (int j = 0; j < trainFiles.length; ++j) {
File trainFile = trainFiles[j];
if (isTrainingFile(trainFile)) {
++numTrainingCases;
String review = Files.readFromFile(trainFile,"ISO-8859-1");
numTrainingChars += review.length();
Classified<CharSequence> classified
= new Classified<CharSequence>(review,classification);
mClassifier.handle(classified);
}
}
}
FileOutputStream fileOut = new FileOutputStream("C:\\review_polarity/polarity.model");
ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
mClassifier.compileTo(objOut);
objOut.close();
System.out.println(" # Training Cases=" + numTrainingCases);
System.out.println(" # Training Chars=" + numTrainingChars);
}
String getSentiment(String text) {
try{
FileInputStream in = new FileInputStream("C:\\review_polarity/polarity.model");
ObjectInputStream ois = new ObjectInputStream(in);
mClassifier = (DynamicLMClassifier)(ois.readObject());
ois.close();
}
catch(Exception e){}
Classification classification = null;
classification = readClassifier.classify(text);
System.out.println("classification: " + classification);
return (classification.bestCategory());
}
public static void main(String[] args) {
try {
PolarityBasic pB = new PolarityBasic(args);
pB.run();
String text = null;
text = "It was awesome !";
System.out.println("The text \"" + text + "\" is "
+ pB.getSentiment(text));
} catch (Throwable t) {
System.out.println("Thrown: " + t);
t.printStackTrace(System.out);
}
}
}
Solved the problem. Here goes the code for reading the object back (instead of one given in the question):