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

edu.stanford.nlp.trees.Trees Maven / Gradle / Ivy

Go to download

Stanford Parser processes raw text in English, Chinese, German, Arabic, and French, and extracts constituency parse trees.

There is a newer version: 3.9.2
Show newest version
package edu.stanford.nlp.trees;

import edu.stanford.nlp.io.IOUtils;
import java.util.function.Function;
import edu.stanford.nlp.util.Generics;
import edu.stanford.nlp.util.MutableInteger;
import edu.stanford.nlp.util.StringUtils;
import edu.stanford.nlp.ling.*;

import java.util.*;
import java.io.*;

/**
 * Various static utilities for the Tree class.
 *
 * @author Roger Levy
 * @author Dan Klein
 * @author Aria Haghighi (tree path methods)
 */
public class Trees {

  private static final LabeledScoredTreeFactory defaultTreeFactory = new LabeledScoredTreeFactory();

  private Trees() {}


  /**
   * 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.
   */
  public static int leftEdge(Tree t, Tree root) {
    MutableInteger i = new MutableInteger(0);
    if (leftEdge(t, root, i)) {
      return i.intValue();
    } else {
      throw new RuntimeException("Tree is not a descendant of root.");
//      return -1;
    }
  }

  static boolean leftEdge(Tree t, Tree t1, MutableInteger i) {
    if (t == t1) {
      return true;
    } else if (t1.isLeaf()) {
      int j = t1.yield().size(); // so that empties don't add size
      i.set(i.intValue() + j);
      return false;
    } else {
      for (Tree kid : t1.children()) {
        if (leftEdge(t, kid, i)) {
          return true;
        }
      }
      return false;
    }
  }

  /**
   * 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.
   */
  public static int rightEdge(Tree t, Tree root) {
    MutableInteger i = new MutableInteger(root.yield().size());
    if (rightEdge(t, root, i)) {
      return i.intValue();
    } else {
      throw new RuntimeException("Tree is not a descendant of root.");
//      return root.yield().size() + 1;
    }
  }

  static boolean rightEdge(Tree t, Tree t1, MutableInteger i) {
    if (t == t1) {
      return true;
    } else if (t1.isLeaf()) {
      int j = t1.yield().size(); // so that empties don't add size
      i.set(i.intValue() - j);
      return false;
    } else {
      Tree[] kids = t1.children();
      for (int j = kids.length - 1; j >= 0; j--) {
        if (rightEdge(t, kids[j], i)) {
          return true;
        }
      }
      return false;
    }
  }


  /**
   * Returns a lexicalized Tree whose Labels are CategoryWordTag
   * instances, all corresponds to the input tree.
   */
  public static Tree lexicalize(Tree t, HeadFinder hf) {
    Function a =
      TreeFunctions.getLabeledTreeToCategoryWordTagTreeFunction();
    Tree t1 = a.apply(t);
    t1.percolateHeads(hf);
    return t1;
  }

  /**
   * returns the leaves in a Tree in the order that they're found.
   */
  public static List leaves(Tree t) {
    List l = new ArrayList();
    leaves(t, l);
    return l;
  }

  private static void leaves(Tree t, List l) {
    if (t.isLeaf()) {
      l.add(t);
    } else {
      for (Tree kid : t.children()) {
        leaves(kid, l);
      }
    }
  }

  public static List preTerminals(Tree t) {
    List l = new ArrayList();
    preTerminals(t, l);
    return l;
  }

  private static void preTerminals(Tree t, List l) {
    if (t.isPreTerminal()) {
      l.add(t);
    } else {
      for (Tree kid : t.children()) {
        preTerminals(kid, l);
      }
    }
  }


  /**
   * returns the labels of the leaves in a Tree in the order that they're found.
   */
  public static List




© 2015 - 2024 Weber Informatics LLC | Privacy Policy