0

May I know how to write the output to the text file same as output shown by show() function. For example when I executed this code:

    p.show();

output:

    (TOP (S (NP (PRP$ My) (NN name)) (VP (VBZ is) (NP (NNP David.))))

when I executed this code:

     System.out.println(p.toString());

output:

    My name is David.

So, when I tried to write this output to text file by using this code:

    fout.write((p.toString()+newline).getBytes());

output in text file same as the output shown by "System.out.println(p.toString());".

So, how to write same output as shown by show() function to the text file?

Full Codes:

    package com.mycompany.app;

    import java.io.BufferedReader; 
    import java.io.DataInputStream;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import opennlp.tools.cmdline.parser.ParserTool;
    import opennlp.tools.parser.Parse;
    import opennlp.tools.parser.Parser;
    import opennlp.tools.parser.ParserFactory;
    import opennlp.tools.parser.ParserModel;
    import opennlp.tools.sentdetect.SentenceDetectorME;
    import opennlp.tools.sentdetect.SentenceModel;

    public class ChunkParser {
    public static void main(String[] args) throws IOException {

    InputStream modelIn = new FileInputStream("D:/NetBeansProjects/my-app/src/main/resources/en-parser-chunking.zip");
    FileInputStream fin=new FileInputStream("D:/NetBeansProjects/my-app/textfile.txt");
    DataInputStream in = new DataInputStream(fin);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine=br.readLine();
    System.out.println("Full Text: "+"\n"+strLine+"\n");

    try {
    ParserModel pmodel = new ParserModel(modelIn);
    Parser parserc = ParserFactory.create(pmodel);
    Parse topParses[] = ParserTool.parseLine(strLine, parserc, 1);

    FileOutputStream fout=new FileOutputStream("D:/NetBeansProjects/my-app/ChunkParser.txt");
    String newline = System.getProperty("line.separator");
    for (Parse p : topParses){
            p.show();
            System.out.println(p.toString());
            fout.write((p.toString()+newline).getBytes());


    }

    fout.close();
    }
    catch (IOException e) {
    e.printStackTrace();
    }
    finally {
    if (modelIn != null) {
    try {
    modelIn.close();
    }
    catch (IOException e) {
    }
    }
    fin.close();
    }
    }
    }
xera
  • 133
  • 1
  • 1
  • 10
  • See how the `show()` method is printing that output. You can also post the code for it here. – Bhesh Gurung Jun 07 '13 at 06:07
  • I think the show method directly uses System.out for output. If this is the case, then it might help to redirect System.out. see http://stackoverflow.com/questions/14715748/redirect-system-out-and-system-err for a description. But if there are better solutions, don't use the redirection since it probably will add other problems. – mschenk74 Jun 07 '13 at 06:13
  • ok..I had put full codes up there – xera Jun 07 '13 at 06:27

1 Answers1

1

Instead of calling p.show() use p.show(sb) where sb is a StringBuffer. Then you can fetch the text from the StringBuffer. Have a deeper look in the documentation of opennlp next time.

mschenk74
  • 3,561
  • 1
  • 21
  • 34