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 outputs representations 025 * of the trees in Graphviz format. Alternatively, selects a given sentence (identified by its location) 026 * in the treebank and outputs a Graphviz representation for that sentence. 027 * <pre> 028 * Usage: java edu.upenn.cis.spinal.GraphvizWalker <infile> [<sentence_location>] 029 * Output will be placed in files named <sentence_location>.dot. 030 * If <sentence_location> is not given, then all the sentences in the file will be processed and placed into a separate file each. 031 * </pre> 032 * 033 * @author Lucas Champollion 034 */ 035 public class GraphvizWalker extends AbstractWalker { 036 037 // Some commented-out code indicates how this class can be modified to only 038 // search for trees that fulfill certain criteria. 039 040 // modify these to get different behavior. 041 boolean includeSpans = true; 042 boolean beanPoleStyle = false; 043 boolean showSpines = true; 044 045 int numSentences=0, numMultirooted=0; 046 047 048 String location = null; // the location of the sentence we want to display 049 050 BufferedWriter out; 051 052 /** Creates a new instance of <code>GraphvizWalker</code>. */ 053 public GraphvizWalker() { 054 055 } 056 057 protected void init() { 058 if (args.length == 0) { 059 printUsage(); 060 System.exit(1); 061 } 062 063 if (args.length > 1) { 064 065 // the rest of the args specify a sentence location 066 this.location = ""; 067 for (int i = 1; i < args.length; i++) { 068 069 location += args[i]; 070 location += " "; 071 } 072 location = location.trim(); 073 } 074 075 } 076 077 public void forEachSentence(Sentence s) { 078 079 // The following filter can be used to find short sentences that exemplify both 080 // adjunction and coordination. 081 082 // if (s.isSkipped() || !s.containsAdjunction() || !s.containsCoordination() || s.length() > 18) { 083 // return; 084 // } 085 086 087 if (this.location == null || this.location.equals(s.getLocation())) { 088 try { 089 this.out = new BufferedWriter 090 (new FileWriter(s.getLocation().replaceAll(" ", "_")+".dot")); 091 this.out.write(s.toGraphviz(includeSpans, beanPoleStyle, showSpines)); 092 this.out.close(); 093 } catch (IOException ex) { 094 ex.printStackTrace(); 095 } finally { 096 if (this.location != null && this.location.equals(s.getLocation())) { 097 terminate(); 098 } 099 } 100 } 101 } 102 103 protected void wrapUp() { 104 try { 105 out.close(); 106 } catch (IOException ex) { 107 ex.printStackTrace(); 108 } 109 } 110 111 /** 112 * Prints out the following message: 113 * 114 * <pre> 115 * Usage: java edu.upenn.cis.spinal.GraphvizWalker <infile> [<sentence_location>] 116 * Output will be placed in files named <sentence_location>.dot. 117 * If <sentence_location> is not given, then all the sentences in the file will be processed and placed into a separate file each. 118 * </pre> 119 */ 120 protected void printUsage() { 121 System.out.println("Usage: java edu.upenn.cis.spinal.GraphvizWalker " + 122 "<infile> [<sentence_location>] "); 123 System.out.println("Output will be placed in files named <sentence_location>.dot."); 124 System.out.println("If <sentence_location> is not given, then all the sentences " + 125 "in the file will be processed and placed into a separate file each."); 126 } 127 128 /** 129 * Main method, call from command line. 130 * @param argv the command line arguments 131 */ 132 public static void main(String argv[]) { 133 new GraphvizWalker().process(argv); 134 } 135 } 136