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