Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9007311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:39:16+00:00 2026-06-16T01:39:16+00:00

I am having some problem with Serialization Here goes my code for writing mClassifier

  • 0

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);
        }
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:39:17+00:00Added an answer on June 16, 2026 at 1:39 am

    Solved the problem. Here goes the code for reading the object back (instead of one given in the question):

    LMClassifier readClassifier;
    FileInputStream in = new FileInputStream("C:\\polarity.model");
    ObjectInputStream ois = new ObjectInputStream(in);
    readClassifier = (LMClassifier)(ois.readObject());
    ois.close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code is having some problem with the jQuery. <script type="text/javascript"> $(window).load(function() {
I am having some problem with jsonp and jquery. This is my code -
I'm building a website and having some problem. if the HTML code is like:
Hi I'm having some problem with a PHP page: I'm writing a little CMS
I have been having some problem with the stringByAddingPercentEscapesUsingEncoding: method. Here's what happens: When
I am having some problem with writing a function to extract strings from a
im having some problem with this code: if (count($_POST)) { $username = mysql_real_escape_string($_POST['username']); $passwd
I'm having some problem with elasticsearch restarts once every 10 min. Here is the
I am having some problem in importing a .csv file into a sqlite table.
I am having some problem with cache registry. Here, how I configured cache resources.cache.frontEnd

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.