Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Serialization Conundrum
    text
    copied!<p>I am having some problem with Serialization</p> <p>Here goes my code for writing mClassifier object to a file:</p> <pre><code>FileOutputStream fileOut = new FileOutputStream("C:\\polarity.model"); ObjectOutputStream objOut = new ObjectOutputStream(fileOut); mClassifier.compileTo(objOut); objOut.close(); </code></pre> <p>It works fine and writes stuff to the file.</p> <p>But there is a catch: <code>myClassifier</code> object is of type <code>DynamicLMClassifier</code>. <code>compileTo</code> method above however returns an instance of LMClassifier (superclass)</p> <p>Here goes my code for reading the object:</p> <pre><code>FileInputStream in = new FileInputStream("C:\\polarity.model"); ObjectInputStream ois = new ObjectInputStream(in); mClassifier = (DynamicLMClassifier)(ois.readObject()); ois.close(); </code></pre> <p>When I read the object I typecast it to <code>DynamicLMClassifier</code> and it too works fine but I dont get the output I desired. While reading the object again should it not be typecasted to <code>LMClassifier</code> rather than <code>DynamicLMClassifier</code>. However if I do that, compiler complains that it should be of type <code>DynamicLMClassifier</code>.</p> <p>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.</p> <p>EDIT: Here is the complete code (just remove the serialization part in the <code>train()</code> and <code>getSentiments()</code> method and it works as intended), Also note that in (1) With Serialization I am not calling <code>getSentiments()</code> 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 <code>train()</code> method just calling <code>getSentiment()</code> by just commenting out appropriate code in main:</p> <pre><code>public class PolarityBasic{ File mPolarityDir; String[] mCategories; DynamicLMClassifier&lt;NGramProcessLM&gt; 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 &lt; 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 &lt; trainFiles.length; ++j) { File trainFile = trainFiles[j]; if (isTrainingFile(trainFile)) { ++numTrainingCases; String review = Files.readFromFile(trainFile,"ISO-8859-1"); numTrainingChars += review.length(); Classified&lt;CharSequence&gt; classified = new Classified&lt;CharSequence&gt;(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); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload