All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.as.text_understanding.tree_util.TreeUtilities Maven / Gradle / Ivy

The newest version!
package com.as.text_understanding.tree_util;

import static com.as.text_understanding.common.TextUnderstandingUtilities.each;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import com.as.text_understanding.representation.tree.Terminal;
import com.as.text_understanding.representation.tree.Tree;
import com.as.text_understanding.representation.tree.TreeNode;
import com.as.text_understanding.tree_util.head.HeadFinder;

/**
 * Commons utilities (including utilities to print some human readable representation) for constituency parse trees.
 * 
 * 
* Date: 28 February 2016 * @author Asher Stern * */ public class TreeUtilities { public static void treeToYield(TreeNode node, LinkedList terminals) { if (node.getItem().isTerminal()) { terminals.add(node.getItem().getTerminal()); } for (TreeNode child : each(node.getChildren())) { treeToYield(child, terminals); } } public static LinkedList treeToYield(TreeNode tree) { LinkedList terminals = new LinkedList<>(); treeToYield(tree,terminals); return terminals; } public static LinkedList treeToYield(final Tree tree) { return treeToYield(tree.getRoot()); } public static String yieldToString(List yield) { StringBuilder sb = new StringBuilder(); boolean firstIteration = true; for (Terminal terminal : yield) { if (firstIteration) {firstIteration=false;} else { sb.append(" "); } sb.append(terminal.getToken()); } return sb.toString(); } public static String treeToString(final Tree tree) { return treeToString(tree.getRoot()); } public static String treeToString(final TreeNode node) { return treeToString(node, 0); } public static String treeToElegantString(final TreeNode node, boolean printHead) { return treeToElegantString(node, new boolean[0], printHead?true:null); } private static String treeToString(final TreeNode node, final int indentation) { StringBuilder sb = new StringBuilder(); char[] spaces = new char[indentation]; Arrays.fill(spaces, ' '); sb.append(new String(spaces)); if (node.getItem().isTerminal()) { sb.append(node.getItem().getTerminal().getToken()).append("/").append(node.getItem().getTerminal().getTag()).append("\n"); } else { sb.append(node.getItem().getSymbol()).append("\n"); for (TreeNode child : node.getChildren()) { sb.append(treeToString(child, indentation+1)); } } return sb.toString(); } private static String treeToElegantString(final TreeNode node, boolean[] ancestors, Boolean head) { StringBuilder sb = new StringBuilder(); for (int index=0; index




© 2015 - 2024 Weber Informatics LLC | Privacy Policy