edu.stanford.nlp.trees

## Class Trees

• ```public class Trees
extends Object```
Various static utilities for the `Tree` class.
Author:
Roger Levy, Dan Klein, Aria Haghighi (tree path methods)
• ### Method Summary

All Methods
Modifier and Type Method and Description
`static Tree` ```applyToProjections(TreeVisitor v, Tree head, Tree root, HeadFinder hf)```
`static void` `convertToCoreLabels(Tree tree)`
Converts the tree labels to CoreLabels.
`static Tree` ```getLeaf(Tree tree, int i)```
Gets the ith leaf of a tree from the left.
`static Tree` ```getLowestCommonAncestor(List<Tree> nodes, Tree root)```
Get lowest common ancestor of all the nodes in the list with the tree rooted at root
`static Tree` ```getLowestCommonAncestor(Tree t1, Tree t2, Tree root)```
returns the node of a tree which represents the lowest common ancestor of nodes t1 and t2 dominated by root.
`static Tree` ```getPreTerminal(Tree tree, int n)```
gets the `n`th preterminal in `tree`.
`static Tree` ```getTerminal(Tree tree, int n)```
gets the `n`th terminal in `tree`.
`static List<Label>` `leafLabels(Tree t)`
returns the labels of the leaves in a Tree in the order that they're found.
`static List<Tree>` `leaves(Tree t)`
returns the leaves in a Tree in the order that they're found.
`static int` ```leftEdge(Tree t, Tree root)```
Returns the positional index of the left edge of a tree t within a given root, as defined by the size of the yield of all material preceding t.
`static int` ```leftEdgeUnsafe(Tree t, Tree root)```
Returns the positional index of the left edge of a tree t within a given root, as defined by the size of the yield of all material preceding t.
`static Tree` ```lexicalize(Tree t, HeadFinder hf)```
Returns a lexicalized Tree whose Labels are CategoryWordTag instances, all corresponds to the input tree.
`static List<String>` `localTreeAsCatList(Tree t)`
returns the syntactic category of the tree as a list of the syntactic categories of the mother and the daughters
`static void` `main(String[] args)`
`static Tree` ```maximalProjection(Tree head, Tree root, HeadFinder hf)```
returns the maximal projection of `head` in `root` given a `HeadFinder`
`static Tree` ```normalizeTree(Tree tree, TreeNormalizer tn, TreeFactory tf)```
`static int` ```objectEqualityIndexOf(Tree parent, Tree daughter)```
Returns the index of `daughter` in `parent` by ==.
`static void` `outputTreeLabels(Tree tree)`
Outputs the labels on the trees, not just the words.
`static void` ```outputTreeLabels(Tree tree, int depth)```
`static List<Tree>` ```pathFromRoot(Tree t, Tree root)```
returns list of tree nodes to root from t.
`static List<String>` ```pathNodeToNode(Tree from, Tree to, Tree root)```
returns a list of categories that is the path from Tree from to Tree to within Tree root.
`static List<Tree>` `preTerminals(Tree t)`
`static Tree` `readTree(String str)`
Simple tree reading utility method.
`static Tree` ```readTree(String ptbTreeString, TreeFactory treeFactory)```
Simple tree reading utility method.
`static void` ```replaceNode(Tree node, Tree node1, Tree t)```
replaces all instances (by ==) of node with node1.
`static int` ```rightEdge(Tree t, Tree root)```
Returns the positional index of the right edge of a tree t within a given root, as defined by the size of the yield of all material preceding t plus all the material contained in t.
`static int` ```rightEdgeUnsafe(Tree t, Tree root)```
Returns the positional index of the right edge of a tree t within a given root, as defined by the size of the yield of all material preceding t plus all the material contained in t.
`static void` ```setLeafLabels(Tree tree, List<Label> labels)```
Replace the labels of the leaves with the given leaves.
`static void` `setLeafTagsIfUnset(Tree tree)`
Given a tree, set the tags on the leaf nodes if they are not already set.
`static void` ```setSentIndex(Tree tree, int sentIndex)```
Set the sentence index of all the leaves in the tree (only works on CoreLabel)
`static List<CoreLabel>` `taggedLeafLabels(Tree t)`
returns the labels of the leaves in a Tree, augmented with POS tags.
`static Tree` ```toFlatTree(List<? extends HasWord> s, LabelFactory lf)```
Turns a sentence into a flat phrasal tree.
`static Tree` `toFlatTree(List<HasWord> s)`
Turns a sentence into a flat phrasal tree.
`static String` `toStructureDebugString(Tree t)`
Returns a String reporting what kinds of Tree and Label nodes this Tree contains.
`static String` `treeToLatex(Tree t)`
`static String` `treeToLatexEven(Tree t)`
• ### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`
• ### Method Detail

• #### leftEdge

```public static int leftEdge(Tree t,
Tree root)```
Returns the positional index of the left edge of a tree t within a given root, as defined by the size of the yield of all material preceding t.
• #### leftEdgeUnsafe

```public static int leftEdgeUnsafe(Tree t,
Tree root)```
Returns the positional index of the left edge of a tree t within a given root, as defined by the size of the yield of all material preceding t. This method returns -1 if no path is found, rather than exceptioning.
See Also:
`leftEdge(Tree, Tree)`
• #### rightEdge

```public static int rightEdge(Tree t,
Tree root)```
Returns the positional index of the right edge of a tree t within a given root, as defined by the size of the yield of all material preceding t plus all the material contained in t.
• #### rightEdgeUnsafe

```public static int rightEdgeUnsafe(Tree t,
Tree root)```
Returns the positional index of the right edge of a tree t within a given root, as defined by the size of the yield of all material preceding t plus all the material contained in t. This method returns root.yield().size() + 1 if no path is found, rather than exceptioning.
See Also:
`rightEdge(Tree, Tree)`
• #### lexicalize

```public static Tree lexicalize(Tree t,
HeadFinder hf)```
Returns a lexicalized Tree whose Labels are CategoryWordTag instances, all corresponds to the input tree.
• #### leaves

`public static List<Tree> leaves(Tree t)`
returns the leaves in a Tree in the order that they're found.
• #### preTerminals

`public static List<Tree> preTerminals(Tree t)`
• #### leafLabels

`public static List<Label> leafLabels(Tree t)`
returns the labels of the leaves in a Tree in the order that they're found.
• #### taggedLeafLabels

`public static List<CoreLabel> taggedLeafLabels(Tree t)`
returns the labels of the leaves in a Tree, augmented with POS tags. assumes that the labels are CoreLabels.
• #### setLeafTagsIfUnset

`public static void setLeafTagsIfUnset(Tree tree)`
Given a tree, set the tags on the leaf nodes if they are not already set. Do this by using the preterminal's value as a tag.
• #### setLeafLabels

```public static void setLeafLabels(Tree tree,
List<Label> labels)```
Replace the labels of the leaves with the given leaves.
• #### maximalProjection

```public static Tree maximalProjection(Tree head,
Tree root,
HeadFinder hf)```
returns the maximal projection of `head` in `root` given a `HeadFinder`
• #### applyToProjections

```public static Tree applyToProjections(TreeVisitor v,
Tree head,
Tree root,
HeadFinder hf)```
• #### getTerminal

```public static Tree getTerminal(Tree tree,
int n)```
gets the `n`th terminal in `tree`. The first terminal is number zero.
• #### getPreTerminal

```public static Tree getPreTerminal(Tree tree,
int n)```
gets the `n`th preterminal in `tree`. The first terminal is number zero.
• #### localTreeAsCatList

`public static List<String> localTreeAsCatList(Tree t)`
returns the syntactic category of the tree as a list of the syntactic categories of the mother and the daughters
• #### objectEqualityIndexOf

```public static int objectEqualityIndexOf(Tree parent,
Tree daughter)```
Returns the index of `daughter` in `parent` by ==. Returns -1 if `daughter` not found.
• #### toStructureDebugString

`public static String toStructureDebugString(Tree t)`
Returns a String reporting what kinds of Tree and Label nodes this Tree contains.
Parameters:
`t` - The tree to examine.
Returns:
A human-readable String reporting what kinds of Tree and Label nodes this Tree contains.
• #### toFlatTree

`public static Tree toFlatTree(List<HasWord> s)`
Turns a sentence into a flat phrasal tree. The structure is S -> tag*. And then each tag goes to a word. The tag is either found from the label or made "WD". The tag and phrasal node have a StringLabel.
Parameters:
`s` - The Sentence to make the Tree from
Returns:
The one phrasal level Tree
• #### toFlatTree

```public static Tree toFlatTree(List<? extends HasWord> s,
LabelFactory lf)```
Turns a sentence into a flat phrasal tree. The structure is S -> tag*. And then each tag goes to a word. The tag is either found from the label or made "WD". The tag and phrasal node have a StringLabel.
Parameters:
`s` - The Sentence to make the Tree from
`lf` - The LabelFactory with which to create the new Tree labels
Returns:
The one phrasal level Tree
• #### treeToLatex

`public static String treeToLatex(Tree t)`
• #### treeToLatexEven

`public static String treeToLatexEven(Tree t)`
• #### main

```public static void main(String[] args)
throws IOException```
Throws:
`IOException`
• #### normalizeTree

```public static Tree normalizeTree(Tree tree,
TreeNormalizer tn,
TreeFactory tf)```
• #### getLeaf

```public static Tree getLeaf(Tree tree,
int i)```
Gets the ith leaf of a tree from the left. The leftmost leaf is numbered 0.
Returns:
The ith leaf as a Tree, or `null` if there is no such leaf.
• #### getLowestCommonAncestor

```public static Tree getLowestCommonAncestor(List<Tree> nodes,
Tree root)```
Get lowest common ancestor of all the nodes in the list with the tree rooted at root
• #### pathNodeToNode

```public static List<String> pathNodeToNode(Tree from,
Tree to,
Tree root)```
returns a list of categories that is the path from Tree from to Tree to within Tree root. If either from or to is not in root, returns null. Otherwise includes both from and to in the list.
• #### pathFromRoot

```public static List<Tree> pathFromRoot(Tree t,
Tree root)```
returns list of tree nodes to root from t. Includes root and t. Returns null if tree not found dominated by root
• #### replaceNode

```public static void replaceNode(Tree node,
Tree node1,
Tree t)```
replaces all instances (by ==) of node with node1. Doesn't affect the node t itself
• #### getLowestCommonAncestor

```public static Tree getLowestCommonAncestor(Tree t1,
Tree t2,
Tree root)```
returns the node of a tree which represents the lowest common ancestor of nodes t1 and t2 dominated by root. If either t1 or or t2 is not dominated by root, returns null.
• #### readTree

```public static Tree readTree(String ptbTreeString,
TreeFactory treeFactory)```
Simple tree reading utility method. Given a tree formatted as a PTB string, returns a Tree made by a specific TreeFactory.
• #### readTree

`public static Tree readTree(String str)`
Simple tree reading utility method. Given a tree formatted as a PTB string, returns a Tree made by the default TreeFactory (LabeledScoredTreeFactory)
• #### outputTreeLabels

`public static void outputTreeLabels(Tree tree)`
Outputs the labels on the trees, not just the words.
• #### outputTreeLabels

```public static void outputTreeLabels(Tree tree,
int depth)```
• #### convertToCoreLabels

`public static void convertToCoreLabels(Tree tree)`
Converts the tree labels to CoreLabels. We need this because we store additional info in the CoreLabel, like token span.
Parameters:
`tree` -
• #### setSentIndex

```public static void setSentIndex(Tree tree,
int sentIndex)```
Set the sentence index of all the leaves in the tree (only works on CoreLabel)

Stanford NLP Group