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.corpus.penntreebank; 025 026import java.util.ArrayList; 027import java.util.List; 028 029import org.apache.uima.cas.FeatureStructure; 030import org.apache.uima.jcas.JCas; 031import org.apache.uima.jcas.cas.FSArray; 032import org.apache.uima.jcas.cas.StringArray; 033import org.cleartk.syntax.constituent.type.TerminalTreebankNode; 034import org.cleartk.util.treebank.TopTreebankNode; 035import org.cleartk.util.treebank.TreebankNode; 036import org.apache.uima.fit.util.FSCollectionFactory; 037 038import com.google.common.annotations.Beta; 039 040/** 041 * <br> 042 * Copyright (c) 2007-2008, Regents of the University of Colorado <br> 043 * All rights reserved. 044 * 045 * 046 * @author Philip Ogren 047 * 048 */ 049@Beta 050public class TreebankNodeConverter { 051 public static org.cleartk.syntax.constituent.type.TopTreebankNode convert( 052 TopTreebankNode pojoNode, 053 JCas jCas, 054 boolean addToIndexes) { 055 org.cleartk.syntax.constituent.type.TopTreebankNode uimaNode = new org.cleartk.syntax.constituent.type.TopTreebankNode( 056 jCas, 057 pojoNode.getTextBegin(), 058 pojoNode.getTextEnd()); 059 convert(pojoNode, jCas, uimaNode, null, addToIndexes); 060 uimaNode.setTreebankParse(pojoNode.getTreebankParse()); 061 initTerminalNodes(uimaNode, jCas); 062 if (addToIndexes) 063 uimaNode.addToIndexes(); 064 return uimaNode; 065 } 066 067 public static void initTerminalNodes( 068 org.cleartk.syntax.constituent.type.TopTreebankNode uimaNode, 069 JCas jCas) { 070 List<TerminalTreebankNode> terminals = new ArrayList<org.cleartk.syntax.constituent.type.TerminalTreebankNode>(); 071 _initTerminalNodes(uimaNode, terminals); 072 073 for (int i = 0; i < terminals.size(); i++) { 074 TerminalTreebankNode terminal = terminals.get(i); 075 terminal.setIndex(i); 076 } 077 078 FSArray terminalsFSArray = new FSArray(jCas, terminals.size()); 079 terminalsFSArray.copyFromArray( 080 terminals.toArray(new FeatureStructure[terminals.size()]), 081 0, 082 0, 083 terminals.size()); 084 uimaNode.setTerminals(terminalsFSArray); 085 } 086 087 private static void _initTerminalNodes( 088 org.cleartk.syntax.constituent.type.TreebankNode node, 089 List<TerminalTreebankNode> terminals) { 090 FSArray children = node.getChildren(); 091 for (int i = 0; i < children.size(); i++) { 092 org.cleartk.syntax.constituent.type.TreebankNode child = (org.cleartk.syntax.constituent.type.TreebankNode) children.get(i); 093 if (child instanceof TerminalTreebankNode) { 094 terminals.add((TerminalTreebankNode) child); 095 } else 096 _initTerminalNodes(child, terminals); 097 } 098 } 099 100 public static org.cleartk.syntax.constituent.type.TreebankNode convert( 101 TreebankNode pojoNode, 102 JCas jCas, 103 org.cleartk.syntax.constituent.type.TreebankNode uimaNode, 104 org.cleartk.syntax.constituent.type.TreebankNode parentNode, 105 boolean addToIndexes) { 106 uimaNode.setNodeType(pojoNode.getType()); 107 StringArray nodeTags = (StringArray) (FSCollectionFactory.fillArrayFS(new StringArray( 108 jCas, 109 pojoNode.getTags().length), pojoNode.getTags())); 110 uimaNode.setNodeTags(nodeTags); 111 uimaNode.setNodeValue(pojoNode.getValue()); 112 uimaNode.setLeaf(pojoNode.isLeaf()); 113 uimaNode.setParent(parentNode); 114 115 List<org.cleartk.syntax.constituent.type.TreebankNode> uimaChildren = new ArrayList<org.cleartk.syntax.constituent.type.TreebankNode>(); 116 for (TreebankNode child : pojoNode.getChildren()) { 117 org.cleartk.syntax.constituent.type.TreebankNode childNode; 118 if (child.isLeaf()) { 119 childNode = new TerminalTreebankNode(jCas, child.getTextBegin(), child.getTextEnd()); 120 } else { 121 childNode = new org.cleartk.syntax.constituent.type.TreebankNode( 122 jCas, 123 child.getTextBegin(), 124 child.getTextEnd()); 125 } 126 uimaChildren.add(convert(child, jCas, childNode, uimaNode, addToIndexes)); 127 if (addToIndexes) 128 childNode.addToIndexes(); 129 } 130 FSArray uimaChildrenFSArray = new FSArray(jCas, uimaChildren.size()); 131 uimaChildrenFSArray.copyFromArray( 132 uimaChildren.toArray(new FeatureStructure[uimaChildren.size()]), 133 0, 134 0, 135 uimaChildren.size()); 136 uimaNode.setChildren(uimaChildrenFSArray); 137 return uimaNode; 138 } 139}