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

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

Go to download

Stanford CoreNLP provides a set of natural language analysis tools which can take raw English language text input and give the base forms of words, their parts of speech, whether they are names of companies, people, etc., normalize dates, times, and numeric quantities, mark up the structure of sentences in terms of phrases and word dependencies, and indicate which noun phrases refer to the same entities. It provides the foundational building blocks for higher level text understanding applications.

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

import edu.stanford.nlp.ling.Label;
import edu.stanford.nlp.util.XMLUtils;

/**
 * An individual dependency between a head and a dependent.
 * The head and dependent are represented as a Label.
 * For example, these can be a
 * Word or a WordTag.  If one wishes the dependencies to preserve positions
 * in a sentence, then each can be a NamedConstituent.
 *
 * @author Christopher Manning
 * @author Spence Green
 * 
 */
public class NamedDependency extends UnnamedDependency {

  private static final long serialVersionUID = -1635646451505721133L;

  private final Object name;

  public NamedDependency(String regent, String dependent, Object name) {
    super(regent, dependent);
    this.name = name;
  }

  public NamedDependency(Label regent, Label dependent, Object name) {
    super(regent, dependent);
    this.name = name;
  }

  @Override
  public Object name() {
    return name;
  }

  @Override
  public int hashCode() {
    return regentText.hashCode() ^ dependentText.hashCode() ^ name.hashCode();
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    } else if ( !(o instanceof NamedDependency)) {
      return false;
    }
    NamedDependency d = (NamedDependency) o;
    return equalsIgnoreName(o) && name.equals(d.name);
  }

  @Override
  public String toString() {
    return String.format("%s --%s--> %s", regentText, name.toString(), dependentText);
  }
  
  /**
   * Provide different printing options via a String keyword.
   * The recognized options are currently "xml", and "predicate".
   * Otherwise the default toString() is used.
   */
  @Override
  public String toString(String format) {
    switch (format) {
      case "xml":
        return "  \n    " + XMLUtils.escapeXML(governor().value()) + "\n    " + XMLUtils.escapeXML(dependent().value()) + "\n  ";
      case "predicate":
        return "dep(" + governor() + "," + dependent() + "," + name() + ")";
      default:
        return toString();
    }
  }

  @Override
  public DependencyFactory dependencyFactory() {
    return DependencyFactoryHolder.df;
  }
  
  public static DependencyFactory factory() {
    return DependencyFactoryHolder.df;
  }

  // extra class guarantees correct lazy loading (Bloch p.194)
  private static class DependencyFactoryHolder {
    private static final DependencyFactory df = new NamedDependencyFactory();
  }

  /**
   * A DependencyFactory acts as a factory for creating objects
   * of class Dependency
   */
  private static class NamedDependencyFactory implements DependencyFactory {
    /**
     * Create a new Dependency.
     */
    public Dependency newDependency(Label regent, Label dependent) {
      return newDependency(regent, dependent, null);
    }

    /**
     * Create a new Dependency.
     */
    public Dependency newDependency(Label regent, Label dependent, Object name) {
      return new NamedDependency(regent, dependent, name);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy