001/** 
002 * Copyright (c) 2007-2008, Regents of the University of Colorado 
003 * All rights reserved.
004 * 
005 * Redistribution and use in source and binary forms, with or without
006 * modification, are permitted provided that the following conditions are met:
007 * 
008 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
009 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 
010 * Neither the name of the University of Colorado at Boulder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
011 * 
012 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
013 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
014 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
015 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
016 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
017 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
018 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
019 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
020 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
021 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
022 * POSSIBILITY OF SUCH DAMAGE. 
023 */
024package org.cleartk.util.treebank;
025
026import java.util.ArrayList;
027import java.util.Collections;
028import java.util.List;
029
030/**
031 * <br>
032 * Copyright (c) 2007-2008, Regents of the University of Colorado <br>
033 * All rights reserved.
034 * 
035 * 
036 * @author Philip Ogren
037 * 
038 */
039public class TreebankNode implements TreebankObject {
040  private TreebankNode parent = null;
041
042  private List<TreebankNode> children;
043
044  private TopTreebankNode topNode = null;
045
046  public TopTreebankNode getTopNode() {
047    return topNode;
048  }
049
050  public void setTopNode(TopTreebankNode top) {
051    this.topNode = top;
052  }
053
054  public String getTreebankParse() {
055    return getTopNode().getTreebankParse().substring(parseBegin, parseEnd);
056  }
057
058  private int parseBegin;
059
060  private int parseEnd;
061
062  private int textBegin;
063
064  private int textEnd;
065
066  private String type;
067
068  private String value;
069
070  private String text;
071
072  private String[] tags;
073
074  private boolean leaf;
075
076  public boolean isLeaf() {
077    return leaf;
078  }
079
080  public void setLeaf(boolean leafNode) {
081    this.leaf = leafNode;
082  }
083
084  public TreebankNode() {
085    children = new ArrayList<TreebankNode>();
086  }
087
088  public List<TreebankNode> getChildren() {
089    return Collections.unmodifiableList(children);
090  }
091
092  public void setChildren(List<TreebankNode> children) {
093    this.children.clear();
094    if (children != null)
095      this.children.addAll(children);
096  }
097
098  public void addChild(TreebankNode child) {
099    children.add(0, child);
100  }
101
102  public TreebankNode getParent() {
103    return parent;
104  }
105
106  public TreebankNode getAncestor(int levels) {
107    if (levels < 0)
108      return null;
109    TreebankNode returnNode = this;
110    for (int i = 0; i < levels; i++) {
111      returnNode = returnNode.getParent();
112      if (returnNode == null)
113        break;
114    }
115    return returnNode;
116  }
117
118  public void setParent(TreebankNode parent) {
119    this.parent = parent;
120  }
121
122  /**
123   * @return the offset of the parse text for this node with respect to the sentence parse. For
124   *         example, if the entire sentence parse was "(NP-LOC (NNP Calif.) )" and this node
125   *         corresponded to only "(NNP Calif.)", then this method would return 8.
126   */
127  public int getParseBegin() {
128    return parseBegin;
129  }
130
131  public int getParseEnd() {
132    return parseEnd;
133  }
134
135  public void setParseBegin(int begin) {
136    this.parseBegin = begin;
137  }
138
139  public void setParseEnd(int end) {
140    this.parseEnd = end;
141  }
142
143  public String getText() {
144    return text;
145  }
146
147  public void setText(String text) {
148    this.text = text;
149  }
150
151  public String getType() {
152    return type;
153  }
154
155  public void setType(String type) {
156    this.type = type;
157  }
158
159  public String getValue() {
160    return value;
161  }
162
163  public void setValue(String value) {
164    this.value = value;
165  }
166
167  /**
168   * @return a "pretty print" of this node that may be useful for e.g. debugging.
169   */
170  public String displayText() {
171    return displayText(0);
172  }
173
174  private String displayText(int tabs) {
175    StringBuffer returnValue = new StringBuffer();
176    String tabString = getTabs(tabs);
177    returnValue.append(tabString + getType());
178    if (getValue() != null)
179      returnValue.append(":" + getValue() + "\n");
180    else {
181      returnValue.append(":" + getText() + "\n");
182    }
183    if (getChildren().size() > 0) {
184      for (TreebankNode child : getChildren()) {
185        returnValue.append(child.displayText(tabs + 1));
186      }
187    }
188    return returnValue.toString();
189  }
190
191  private String getTabs(int tabs) {
192    StringBuffer returnValue = new StringBuffer();
193    for (int i = 0; i < tabs; i++) {
194      returnValue.append("  ");
195    }
196    return returnValue.toString();
197  }
198
199  public String[] getTags() {
200    return tags;
201  }
202
203  public void setTags(String[] tags) {
204    this.tags = tags;
205  }
206
207  public int getTextBegin() {
208    return textBegin;
209  }
210
211  /**
212   * the offset of the plain text for this node with respect to the sentence text adjusted by the
213   * textOffset passed into the parse method. For example, if the entire sentence parse was
214   * "(NP-LOC (NNP Calif.) )" and this node corresponded to only "(NNP Calif.)", then
215   * {@link #getText()} would return "Calif." and this method would return 0 plus the textOffset
216   * given by TreebankFormatParser.parse(String, String, int).
217   */
218  public void setTextBegin(int textBegin) {
219    this.textBegin = textBegin;
220  }
221
222  public int getTextEnd() {
223    return textEnd;
224  }
225
226  public void setTextEnd(int textEnd) {
227    this.textEnd = textEnd;
228  }
229
230}