user2702274
user2702274

Reputation: 55

How to print result of parsed tree to text file using stanford nlp in java

Below is my program to get the parsed tree and dependencies for a single text file using stanford nlp in java

public class Stanford {

public static void demoDP(LexicalizedParser lp, String filename) throws IOException {

    //FileOutputStream fos = new FileOutputStream("C://perl_java//Text-Parsed.txt");
      //ObjectOutputStream oos = new ObjectOutputStream(fos);

    //BufferedWriter bwriter = new BufferedWriter(fwriter);
    TreebankLanguagePack tlp = new PennTreebankLanguagePack();
    System.out.println("in method");
    System.out.println("lp in method"+lp);
    GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
    System.out.println(filename);
    for (List<HasWord> sentence : new DocumentPreprocessor(filename)) {
      Tree parse = lp.apply(sentence);
      parse.pennPrint();
      System.out.println();
      System.out.println("hiiiiii");
      GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
      Collection tdl = gs.typedDependenciesCCprocessed();
      System.out.println(tdl);
      PrintWriter pw = new PrintWriter("C://perl_java//Text-Parsed.txt");
      pw.print(tdl);
      pw.close();
       //oos.write(tdl.toString());
      //bwriter.write(tdl);
      System.out.println();
    }
    //oos.close();
  }


 public static void main(String args[])
 {
 LexicalizedParser lp =       LexicalizedParser.loadModel("edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz");
demoDP(lp,"C://sample.txt")
 }
}

I am getting output in console

how can i write this to a text file i have used bufferwriter, fileoutputstream but failed to write to it, can any one suggest me how to do this

Thanks

Upvotes: 1

Views: 1928

Answers (2)

tintin21
tintin21

Reputation: 255

Try this code:

 File f=new File("Data/tree.txt");
      PrintWriter pw=new PrintWriter(f);
    // This option shows loading and using an explicit tokenizer
    String sent2 = "This is another sentence.";
    TokenizerFactory<CoreLabel> tokenizerFactory =PTBTokenizer.factory(new CoreLabelTokenFactory(), "");
    Tokenizer<CoreLabel> tok =tokenizerFactory.getTokenizer(new StringReader(sent2));
    List<CoreLabel> rawWords2 = tok.tokenize();
   Tree parse = lp.apply(rawWords2);

    TreebankLanguagePack tlp = new PennTreebankLanguagePack();
    GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
    GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
    List<TypedDependency> tdl = gs.typedDependenciesCCprocessed();
  System.out.println(tdl);
    System.out.println();
    // You can also use a TreePrint object to print trees and dependencies
    TreePrint tp = new TreePrint("penn,typedDependenciesCollapsed");
    tp.printTree(parse,pw);

Upvotes: 1

Scary Wombat
Scary Wombat

Reputation: 44834

A FileOutputStream or similar should do the job with construction before the for loop, writing in the for loop and close after the for loop. In the above example your are closing the PrintWriter in the for loop.

Upvotes: 0

Related Questions