0

I read a text file containing list of words with their tags and put them as an ArrayList in an a wrapping ArrayList (ArrayList).

[[1, that, that, that, DT, DT], [2, table, table, table, NN, NN]]

Now I want to write the in a text file in a same format as follows:

1    that    that     that    DT    DT  
2    table   table    table   NN    NN

each of the above rows is an ArrayList with 6 columns.

the following code return a file with Ԁ inside.

public void setPPOSOfWordInDevelopmentList(ArrayList<ArrayList> trainingList){
try{
    FileOutputStream streamFile = new FileOutputStream("developmentFile.txt");
    ObjectOutputStream streamFileWriter = new ObjectOutputStream(streamFile);
for(ArrayList word: developmentWordsList){
    String inputWord = (String)word.get(1);
    extractTag(inputWord,trainingList);
    String extractedPPOSofWord =(String)findMaxTag().get(1);
    word.set(5, extractedPPOSofWord);

}
streamFileWriter.close();
System.out.println(developmentWordsList);
}
catch(Exception e){
    System.out.println("Something went wrong, check the code");
}
}

this code is coupled with some others so it is not easy to change the format of objects returned by the functions.

msc87
  • 943
  • 3
  • 17
  • 39

3 Answers3

1

What you want sounds eerily like a standard CSV file. This stackoverflow thread will set you straight on how to parse that sort of content. I would strongly recommend that you refactor along the lines of a CSV file instead of using the ObjectInput/OutputStreams. It'll be easier to maintain and you'll be able to use tools like Excel and OpenOffice Calc to view your files when debugging.

Community
  • 1
  • 1
Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83
  • As the input file is read, some processes like tagging (POS)will be performed on it. then the output should be exactly the same as the input, a text with rows with 6 tab-separated columns for each word. If I use System.out.println(output) the arraylist would be written as an arrayList on the console. now I want to print it in a file and format it in a desired way. – msc87 Apr 15 '14 at 14:48
1

If you want to write a simple text file, would be better if you use a BufferedWriter. For your content, you can format it in a StringBuffer or a StringBuilder if it is too long. Here in this post, I replied to a question related with the kind of formatting you're trying to make. But you should need to adapt it according to your format and the logic of using a wrapping array.

Export array values to csv file java

I think, the loop or "enhanced for" statement should be used as something like:

for (ArrayList<String> innerArray: wrapperArray) {
    for (String word : innerArray) {
        //Adapt to your required format using a StringBuilder
    }
}
//Here at the end save the content of your StringBuilder or StringBuffer using the BufferedWriter.

Hope you can get an idea on how to achieve this. Best regards :)

Community
  • 1
  • 1
Marcelo Tataje
  • 3,849
  • 1
  • 26
  • 51
  • I'm looking for more straight way which accepts the arrayList as an input, but unfortunately seems there is no such a thing. – msc87 Apr 16 '14 at 09:24
1

If you are certain to use custom format file you can use formatted printing and add padding accordingly. It's pretty easy:

for (ArrayList<String> list : trainingList) {
    writeToStream(
        String.format(
            %s, %-5s, %-5s, %-5s, 
            list.getAt(0),list.getAt(1),list.getAt(2),list.getAt(3)
        );
    }
}

This should work if your strings aren't longer than five characters. Just keep in mind that blank characters are bad demiliters and you will face indentation problems if you use other than monospaced fonts.

t-my
  • 113
  • 1
  • 2
  • 10