001 //This file is part of the Attempto Java Packages. 002 //Copyright 2008-2009, Attempto Group, University of Zurich (see http://attempto.ifi.uzh.ch). 003 004 //The Attempto Java Packages is free software: you can redistribute it and/or modify it under the 005 //terms of the GNU Lesser General Public License as published by the Free Software Foundation, 006 //either version 3 of the License, or (at your option) any later version. 007 008 //The Attempto Java Packages is distributed in the hope that it will be useful, but WITHOUT ANY 009 //WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 010 //PURPOSE. See the GNU Lesser General Public License for more details. 011 012 //You should have received a copy of the GNU Lesser General Public License along with the Attempto 013 //Java Packages. If not, see http://www.gnu.org/licenses/. 014 015 package ch.uzh.ifi.attempto.ape; 016 017 import java.util.Stack; 018 019 /** 020 * This class generates the colored syntax boxes in HTML notation on the basis of the syntax tree 021 * of an ACE sentence. Noun phrases are represented by blue boxes, verb phrases by yellow boxes, 022 * and (subordinated) sentences by gray boxes. 023 * 024 * @author Tobias Kuhn 025 */ 026 public class SyntaxBoxes { 027 028 private static final String[] sNodes = new String[] {"s", "cond_s", "neg_s", "s_coord", "top_s", "question", "command"}; 029 private static final String[] vpNodes = new String[] {"vp", "vp_coord", "neg_vp"}; 030 private static final String[] npNodes = new String[] {"np"}; 031 032 private final boolean showS, showVP, showNP; 033 private Stack<String> stack = new Stack<String>(); 034 035 private SyntaxBoxes(boolean showS, boolean showVP, boolean showNP) { 036 this.showS = showS; 037 this.showVP = showVP; 038 this.showNP = showNP; 039 } 040 041 /** 042 * This method takes the syntax tree of a parsing result and generates the HTML representation of the 043 * syntax boxes showing all three types of boxes. 044 * 045 * @param parserResult The parsing result. 046 * @return The HTML representation of the syntax boxes. 047 */ 048 public static String getBoxesHtml(ACEParserResult parserResult) { 049 return getBoxesHtml(parserResult, true, true, true); 050 } 051 052 /** 053 * This method takes the syntax tree of a parsing result and generates the HTML representation of the 054 * syntax boxes. The three types of boxes can individually be switched on or off. 055 * 056 * @param parserResult The parsing result. 057 * @param showS true if the gray boxes for (subordinated) sentences should be shown. 058 * @param showVP true if the yellow boxes for verb phrases should be shown. 059 * @param showNP true if the blue boxes for noun phrases should be shown. 060 * @return The HTML representation of the syntax boxes. 061 */ 062 public static String getBoxesHtml(ACEParserResult parserResult, boolean showS, boolean showVP, boolean showNP) { 063 String s = new SyntaxBoxes(showS, showVP, showNP).createBoxes(parserResult.get(OutputType.SYNTAX)); 064 String c = s.replace("'", "").replace("{", "").replace("}", "").replace("_that_", "that"); 065 String r = 066 "<table>\n" + 067 "<style type=\"text/css\">\n" + 068 "table.np { padding: 0.1em 0.1em 0.1em 0.1em; border: 1px solid #AAF; background-color: #DDF; white-space: nowrap }\n" + 069 "table.vp { padding: 0.1em 0.1em 0.1em 0.1em; border: 1px solid #CC4; background-color: #FFA; white-space: nowrap }\n" + 070 "table.s { padding: 0.1em 0.1em 0.1em 0.1em; border: 1px solid #CCC; background-color: #EEE; white-space: nowrap }\n" + 071 "</style>\n\n" + 072 "<tr>\n" + 073 c + 074 "</tr>\n" + 075 "</table>"; 076 // This style-element is actually not allowed at this position, but it works in all tested browsers. 077 078 return r; 079 } 080 081 private String createBoxes(String p) { 082 if (p.length() == 0) { 083 return ""; 084 } else if (p.startsWith(", ")) { 085 return createBoxes(p.substring(2)); 086 } else if (p.startsWith("[[")) { 087 stack.push(""); 088 return createBoxes(p.substring(1)); 089 } else if (p.startsWith("[]")) { 090 return createBoxes(p.substring(2)); 091 } else if (p.startsWith("[")) { 092 if (showS) { 093 for (String s : sNodes) { 094 if (p.startsWith("[" + s + ", ")) { 095 stack.push("</tr></table></td>"); 096 return "<td><table class=\"s\"><tr>" + createBoxes(p.substring(s.length()+3)); 097 } 098 } 099 } 100 if (showVP) { 101 for (String s : vpNodes) { 102 if (p.startsWith("[" + s + ", ")) { 103 stack.push("</tr></table></td>"); 104 return "<td><table class=\"vp\"><tr>" + createBoxes(p.substring(s.length()+3)); 105 } 106 } 107 } 108 if (showNP) { 109 110 for (String s : npNodes) { 111 if (p.startsWith("[" + s + ", ")) { 112 stack.push("</tr></table></td>"); 113 return "<td><table class=\"np\"><tr>" + createBoxes(p.substring(s.length()+3)); 114 } 115 } 116 } 117 if (p.indexOf(", ") > 0) { 118 stack.push(""); 119 return createBoxes(p.substring(p.indexOf(", ")+2)); 120 } 121 } else if (p.startsWith("]")) { 122 String t = stack.pop(); 123 return t + createBoxes(p.substring(1)); 124 } else if (p.indexOf("]") > 0) { 125 int ci = p.indexOf(","); 126 int bi = p.indexOf("]"); 127 int i = bi; 128 if (ci > 0 && ci < bi) i = ci; 129 String a = p.substring(0, i); 130 String b = p.substring(i); 131 return "<td>" + a + "</td> " + createBoxes(b); 132 } 133 return " ERROR(" + p + ")ERROR "; 134 } 135 136 }