001 /** 002 * LTAG-spinal API, an interface to the treebank format introduced by Libin Shen. 003 * Copyright (C) 2007 Lucas Champollion 004 * 005 * This program is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU General Public License as published by 007 * the Free Software Foundation, either version 3 of the License, or 008 * (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU General Public License for more details. 014 * 015 * You should have received a copy of the GNU General Public License 016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 017 * 018 */ 019 package edu.upenn.cis.spinal; 020 021 import java.io.*; 022 023 /** 024 * Walks through a treebank, reads it in, and prints it to <code>stdout</code> 025 * unchanged. This class is provided as a simple example of an implementation 026 * of {@link AbstractWalker}. It can be used for debugging purposes when testing 027 * the <code>toString()</code> methods. This class can also print 028 * to file if that file is given as a second argument. 029 * 030 * @author Lucas Champollion 031 */ 032 public class EchoWalker extends AbstractWalker { 033 034 int numSentences=0, numMultirooted=0; 035 036 BufferedWriter out; 037 038 /** Creates a new instance of <code>EchoWalker</code>. */ 039 public EchoWalker() { 040 041 } 042 043 protected void init() { 044 if (args.length == 0) { 045 System.err.println("Please supply" + 046 " at least one argument."); 047 return; 048 } 049 if (args.length == 1) { 050 this.out = new BufferedWriter 051 (new OutputStreamWriter(System.out)); 052 } 053 if (args.length == 2) { 054 try { 055 this.out = new BufferedWriter 056 (new FileWriter(args[1])); 057 } catch (IOException ex) { 058 ex.printStackTrace(); 059 } 060 } 061 062 } 063 064 public void forEachSentence(Sentence s) { 065 try { 066 this.out.write(s.toString()); 067 } catch (IOException ex) { 068 ex.printStackTrace(); 069 } 070 } 071 072 protected void wrapUp() { 073 try { 074 out.close(); 075 } catch (IOException ex) { 076 ex.printStackTrace(); 077 } 078 } 079 080 protected void printUsage() { 081 System.out.println("Walks through a treebank, reads it in, and prints it " + 082 "to stdout unchanged. Alternatively, prints to file if that " + 083 "file is given as a second argument.\n" + 084 "Syntax: java edu.upenn.cis.spinal.EchoWalker <infile> [<outfile>]"); 085 } 086 087 /** 088 * Main method, call from command line. 089 * @param argv the command line arguments 090 */ 091 public static void main(String argv[]) { 092 new EchoWalker().process(argv); 093 } 094 } 095