0

I have a file called my-training.train. (.train is the extension for the training files in open-nlp). The file is already populated with some data:

Refund What is the refund status for my order #342? Can I place a refund request for electronics?
NewOffers Are there any new offers for your products? Is there any new offer on buying worth 5000?  
Refund Can I place a refund request for electronics?
NewOffers Is there any new offer on buying worth 5000?  
Refund What is the refund status for my order #343? Can I place a refund request for electronics?
NewOffers Are there any new offers for your products? Is there any new offer on buying worth 5000?  
Refund Can I place a refund request for electronics?
NewOffers Are there any new offers? 

I want to append a sentence to it on the very next line, programmatically. This is the code that I have written:

     Writer output;
     output = new BufferedWriter(new FileWriter("my-training.train",true));  
     output.append("NewOffers Please inform me of the new offers");
     output.close();

the sentence gets inserted as intended. Showing the last three lines of the training file:

Refund Can I place a refund request for electronics?
NewOffers Are there any new offers?  
NewOffers Please inform me of the new offers

But when I run the code again, the sentence gets inserted to the next of the previous sentence instead of the new line. (The last three lines):

    Refund Can I place a refund request for electronics?
    NewOffers Are there any new offers?  
    NewOffers Please inform me of the new offersNewOffers Please inform me of the new offers

In the above code, I replace the line output.append("NewOffers Please inform me of the new offers"); with output.append("\nNewOffers Please inform me of the new offers");, but using that it creates a problem for the very first time append.

Getting the initial file my-training.train, this is how the last three lines become:

Refund Can I place a refund request for electronics?
NewOffers Are there any new offers?  

NewOffers Please inform me of the new offers

The empty line gets inserted. This makes the training file invalid. There should be no empty line, and each sentence should be inserted in the new line.

Why is this discriminating behavior between the first line and the others.? What wrong am I doing?

inquisitive
  • 3,738
  • 6
  • 30
  • 56
  • Do you have a trailing newline? I presume so. – Boris the Spider May 12 '15 at 11:47
  • No, I dont have a trailing new line in the training file. In fact this is not valid also, to have a trailing new line. My model would have failed the very first time. but it runs successfully. So no trailing newline in the first place. I also tried to draw spaces and return in gedit, but that also does not show any newline character at the end – inquisitive May 12 '15 at 11:49
  • maybe try \r instead. http://stackoverflow.com/questions/1761051/difference-between-n-and-r (or Java 7 now has a System.lineSeparator() method. ) – W vd L May 12 '15 at 11:52
  • Looks like the original "my-training.train" file has a `new-line` at the end so that there is a discrimination for the the 1st append of the line. BufferedWriter --> Append will not insert new-line. – Raghava Rudrakanth P V May 12 '15 at 11:53
  • aye.. check the file in a hex editor to make sure thats not the fault – W vd L May 12 '15 at 11:59

1 Answers1

0

The second line as inserts a new line at the end. The close() inhibits the new line from being placed at the end of "Please Inform me of new offers" by closing before the Buffer is completely writen.

The best way to fix this is to insert a flush() between the append() and the close(). This way the new line gets written to the file before it is closed.

I am not 100% familiar with the API. I am not certain if Writer has a flush method, so you may either have to change the type of output to BufferedWriter or cast output back to BufferedWriter before calling flush.

If flush does not change anything, then append() does not send a new line to the file so changing the text to "Please Inform me of new offers\n" will do the job.

Steve
  • 623
  • 4
  • 11
  • the flush method did not help. I also thought to use `\n` in the end of the text to append, but then it is not as per the format of the training file. There should be no empty line at the end. But still, I tried now and due to this weird behavior of discrimination `\n` at the end works. Marking your answer as correct. – inquisitive May 12 '15 at 12:34