Results 1 to 7 of 7
  1. #1
    er.raj is offline Member
    Join Date
    Mar 2012
    Posts
    15
    Rep Power
    0

    Default Problem with Object Stream

    Hi. I'l be thankful for your patience in reading this out.

    I am trying to create a training model on Document Categorization using the Apache opennlp library in eclipse.
    In their documentation, the code given is this:

    Java Code:
    BufferedReader input =  new BufferedReader(new FileReader(myFile);
    String line = null; 
    while (( line = input.readLine()) != null){
    	myDocSamples.add(new DocumentSample(line.split("::")[1], line.split("::")[0]));
    }
    input.close();
    ObjectStream<DocumentSample> os = ObjectStreamUtils.createObjectStream(myDocSamples);
    DoccatModel m = DocumentCategorizerME.train("en", os);
    We have a text file (say myFile) which has 2 sentences in each line separated by a "::".
    myDocSamples is to be an object of class DocumentSample and the 2 sentences in a line of the file are to be passed to its constructor.
    This code is clearly wrong because "myDocSamples" belongs to a class which has no such method such as "add".

    But the idea here is clear. We have to create an object stream with each object storing the 2 sentences in a line. So 'n' lines will give 'n' objects I suppose. The line of code inside while loop can be modified as

    Java Code:
    DocumentSample myDocSamples= new DocumentSample(line.split("::")[1], line.split("::")[0]);
    But now how should I go on and create the object stream ?

    Please help.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default Re: Problem with Object Stream

    myDocSamples is not of type DocumentSample.
    It looks to me like a Collection of DocumentSample (probably a List), which makes sense.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    er.raj is offline Member
    Join Date
    Mar 2012
    Posts
    15
    Rep Power
    0

    Default Re: Problem with Object Stream

    Quote Originally Posted by Tolls View Post
    myDocSamples is not of type DocumentSample.
    It looks to me like a Collection of DocumentSample (probably a List), which makes sense.
    I suppose you are right. I must have made a wrong inference before. I've added a line
    Java Code:
    ArrayList myDocSamples = new ArrayList();
    This seems to fix the issue and makes sense.

    However, now I am getting an error on the line
    Java Code:
    ObjectStream<DocumentSample> os = ObjectStreamUtils.createObjectStream(myDocSamples);
    "Syntax error, parameterized types are only available if source level is 1.5"

    When I right click on my project->properties->Java Compiler
    The option "Enable Project specific settings" is checked.

    Is there a problem with the line of code or something else ?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default Re: Problem with Object Stream

    Are you on a 1.5+ JDK?
    Is Eclipse set to compile to 1.5+?

    And that first bit of code should be:
    Java Code:
    ArrayList<DocumentSample> myDocSamples = new ArrayList<DocumentSample>();
    Saves you having to cast back and forth.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    er.raj is offline Member
    Join Date
    Mar 2012
    Posts
    15
    Rep Power
    0

    Default Re: Problem with Object Stream

    Quote Originally Posted by Tolls View Post
    Are you on a 1.5+ JDK?
    Is Eclipse set to compile to 1.5+?
    Well when I give the command "java -version" on cmd, I get:

    java version "1.7.0"
    Java(TM) SE Runtime Environment (build 1.7.0-b147)
    Java HotSpot(TM) Client VM (build 21.0-b17, mixed mode, sharing)

    And the "Enable Project specific settings" being checked in Eclipse, I suppose it should be using this java version i.e 1.7.0 right ?
    I am not sure how to check or change this.
    PS I am new to the eclipse IDE.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,252
    Rep Power
    19

    Default Re: Problem with Object Stream

    Quote Originally Posted by er.raj View Post
    I am not sure how to check or change this.
    PS I am new to the eclipse IDE.
    Don't be such a baby. I don't even use Eclipse, but I fired it up and found the setting inside of a minute. Then I opened the online Help and typed in "compiler level" and found an informative help topic too.

    Show some initiative!

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    er.raj is offline Member
    Join Date
    Mar 2012
    Posts
    15
    Rep Power
    0

    Default Re: Problem with Object Stream

    Haha...I am sorry. I was just doing that myself and didn't post back.

    Okay the problem seems to be fixed for now.
    These are the steps I followed to add my own JDK/JRE:

    1. Goto Windows -> Preferences menu.
    2. On Preferences window, in the left drop-down, select, Java -> Installed JREs.
    3. Click on the Add bitton (on the right), select Standard VM, then a dialog box will appear with title Add JRE.
    4. On JRE home: field, click on directory button and browse to you JRE/JDK root folder. Once selected, it will auto-complete and click finish.
    5. Once you're done, go back to the Preferences window, and tick the radio button of your added JRE/JDK to make it default.

    (mentioning these for anyone who is facing similar problems and reaches this thread)

    I am not getting the errors after following these steps.

    Thank you (@Tolls) for your guidance in developing a better understanding of this program.
    Last edited by er.raj; 03-13-2012 at 03:47 PM.

Similar Threads

  1. Input Stream as an Object?
    By sehudson in forum New To Java
    Replies: 1
    Last Post: 03-12-2011, 12:37 AM
  2. Object Input Stream EOFException
    By FlyNn in forum New To Java
    Replies: 1
    Last Post: 12-18-2010, 12:33 PM
  3. Replies: 3
    Last Post: 09-07-2009, 08:27 AM
  4. Replies: 0
    Last Post: 07-08-2009, 08:10 AM
  5. Problem in using Object datatype
    By mfaizan24 in forum New To Java
    Replies: 6
    Last Post: 05-05-2009, 11:51 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •