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(1)); 086 } else if (p.startsWith(",")) { 087 return createBoxes(p.substring(1)); 088 } else if (p.startsWith("[[")) { 089 stack.push(""); 090 return createBoxes(p.substring(1)); 091 } else if (p.startsWith("[]")) { 092 return createBoxes(p.substring(2)); 093 } else if (p.startsWith("[")) { 094 if (showS) { 095 for (String s : sNodes) { 096 if (p.startsWith("[" + s + ",")) { 097 stack.push("</tr></table></td>"); 098 return "<td><table class=\"s\"><tr>" + createBoxes(p.substring(s.length()+2)); 099 } 100 } 101 } 102 if (showVP) { 103 for (String s : vpNodes) { 104 if (p.startsWith("[" + s + ",")) { 105 stack.push("</tr></table></td>"); 106 return "<td><table class=\"vp\"><tr>" + createBoxes(p.substring(s.length()+2)); 107 } 108 } 109 } 110 if (showNP) { 111 for (String s : npNodes) { 112 if (p.startsWith("[" + s + ",")) { 113 stack.push("</tr></table></td>"); 114 return "<td><table class=\"np\"><tr>" + createBoxes(p.substring(s.length()+2)); 115 } 116 } 117 } 118 if (p.indexOf(",") > 0) { 119 stack.push(""); 120 return createBoxes(p.substring(p.indexOf(",")+1)); 121 } 122 } else if (p.startsWith("]")) { 123 String t = stack.pop(); 124 return t + createBoxes(p.substring(1)); 125 } else if (p.indexOf("]") > 0) { 126 int ci = p.indexOf(","); 127 int bi = p.indexOf("]"); 128 int i = bi; 129 if (ci > 0 && ci < bi) i = ci; 130 String a = p.substring(0, i); 131 String b = p.substring(i); 132 return "<td>" + a + "</td> " + createBoxes(b); 133 } 134 return " ERROR(" + p + ")ERROR "; 135 } 136 137 }