edu.stanford.nlp.trees
Class GrammaticalStructure

java.lang.Object
  extended by edu.stanford.nlp.trees.TreeGraph
      extended by edu.stanford.nlp.trees.GrammaticalStructure
All Implemented Interfaces:
java.io.Serializable

public class GrammaticalStructure
extends TreeGraph
implements java.io.Serializable

A GrammaticalStructure is a TreeGraph (that is, a tree with additional labeled arcs between nodes) for representing the grammatical relations in a parse tree. A new GrammaticalStructure is constructed from an existing parse tree with the help of GrammaticalRelation, which defines a hierarchy of English grammatical relations, along with patterns for identifying them in parse trees. The constructor for GrammaticalStructure uses these definitions to populate the new GrammaticalStructure with as many labeled grammatical relations as it can. Once constructed, the new GrammaticalStructure can be printed in various formats, or interrogated using the interface methods in this class.

Caveat emptor! This is a work in progress. Nothing in here should be relied upon to function perfectly. Feedback welcome.

See Also:
Serialized Form

Field Summary
 
Fields inherited from class edu.stanford.nlp.trees.TreeGraph
root
 
Constructor Summary
GrammaticalStructure(Tree t)
          Construct a new GrammaticalStructure from an existing parse tree.
 
Method Summary
 TreeGraphNode getAdjectivalComplement(TreeGraphNode t)
          Tries to return a node representing the ADJECTIVAL_COMPLEMENT of the given node t.
 TreeGraphNode getClausalComplement(TreeGraphNode t)
          Tries to return a node representing the CLAUSAL_COMPLEMENT of the given node t.
 TreeGraphNode getClausalSubject(TreeGraphNode t)
          Tries to return a node representing the CLAUSAL_SUBJECT of the given node t.
 java.util.List getDependencyPath(int nodeIndex, int rootIndex)
          Returns the dependency path as a list of labels, from node to root, it is assumed that that root is an ancestor of node
 java.util.List getDependencyPath(TreeGraphNode node, TreeGraphNode root)
          Returns the dependency path as a list of labels, from node to root, it is assumed that that root is an ancestor of node
 java.util.Set getDependents(TreeGraphNode t)
          Tries to return a Set of leaf (terminal) nodes which are the DEPENDENTs of the given node t.
 TreeGraphNode getDirectObject(TreeGraphNode t)
          Tries to return a node representing the DIRECT_OBJECT of the given node t.
 TreeGraphNode getGovernor(TreeGraphNode t)
          Tries to return a leaf (terminal) node which is the GOVERNOR of the given node t.
 GrammaticalRelation getGrammaticalRelation(int govIndex, int depIndex)
          Get GrammaticalRelation between gov and dep, and null if gov is not the governor of dep
 GrammaticalRelation getGrammaticalRelation(TreeGraphNode gov, TreeGraphNode dep)
          Get GrammaticalRelation between gov and dep, and null if gov is not the governor of dep
 TreeGraphNode getHeadTag(TreeGraphNode t)
          Tries to return a (preterminal) node which represents the head tag for the given node t.
 TreeGraphNode getHeadWord(TreeGraphNode t)
          Tries to return a leaf (terminal) node which represents the head word for the given node t.
 TreeGraphNode getIndirectObject(TreeGraphNode t)
          Tries to return a node representing the INDIRECT_OBJECT of the given node t.
 TreeGraphNode getNominalSubject(TreeGraphNode t)
          Tries to return a node representing the NOMINAL_SUBJECT of the given node t.
 TreeGraphNode getPredicate(TreeGraphNode t)
          Tries to return a node representing the PREDICATE of the given node t.
 TreeGraphNode getSubject(TreeGraphNode t)
          Tries to return a node representing the SUBJECT (whether nominal or clausal) of the given node t.
 TreeGraphNode getTemporalModifier(TreeGraphNode t)
          Tries to return a node representing a TEMPORAL_MODIFIER of the given node t.
static void main(java.lang.String[] args)
          Just for testing.
 void printTypedDependencies()
          Prints the result of typedDependenciesToString() to stdout.
 void printTypedDependencies(java.io.PrintWriter pw)
          Prints the result of typedDependenciesToString() to the specified PrintWriter.
 void printTypedDependencies(java.lang.String format)
          Prints the result of typedDependenciesToString(format) to stdout.
 void printTypedDependencies(java.lang.String format, java.io.PrintWriter pw)
          Prints the result of typedDependenciesToString(format) to the specified PrintWriter.
 java.util.List typedDependencies()
          Returns a List of Triples, each representing a typed dependency in the GrammaticalStructure.
 java.lang.String typedDependenciesToString()
          Returns a String representation of the result of typedDependencies() as exemplified by the following:
 java.lang.String typedDependenciesToString(java.lang.String format)
          Returns a String representation of the result of typedDependencies() in a user-specified format.
 
Methods inherited from class edu.stanford.nlp.trees.TreeGraph
addNodeToIndexMap, getNodeByIndex, root, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

GrammaticalStructure

public GrammaticalStructure(Tree t)
Construct a new GrammaticalStructure from an existing parse tree. The new GrammaticalStructure has the same tree structure and label values as the given tree (but no shared storage). As part of construction, the parse tree is analyzed using definitions from GrammaticalRelation to populate the new GrammaticalStructure with as many labeled grammatical relations as it can.

Method Detail

getHeadWord

public TreeGraphNode getHeadWord(TreeGraphNode t)
Tries to return a leaf (terminal) node which represents the head word for the given node t.

Parameters:
t - any node in this GrammaticalStructure
Returns:
a node which represents the head word for node t, or else null

getHeadTag

public TreeGraphNode getHeadTag(TreeGraphNode t)
Tries to return a (preterminal) node which represents the head tag for the given node t.

Parameters:
t - any node in this GrammaticalStructure
Returns:
a node which represents the head tag for node t, or else null

getGovernor

public TreeGraphNode getGovernor(TreeGraphNode t)
Tries to return a leaf (terminal) node which is the GOVERNOR of the given node t. Probably, t should be a leaf node as well.

Parameters:
t - a leaf node in this GrammaticalStructure
Returns:
a node which is the governor for node t, or else null

getDependents

public java.util.Set getDependents(TreeGraphNode t)
Tries to return a Set of leaf (terminal) nodes which are the DEPENDENTs of the given node t. Probably, t should be a leaf node as well.

Parameters:
t - a leaf node in this GrammaticalStructure
Returns:
a Set of nodes which are dependents of node t, or else null

getPredicate

public TreeGraphNode getPredicate(TreeGraphNode t)
Tries to return a node representing the PREDICATE of the given node t. Probably, node t should represent a clause.

Parameters:
t - a node in this GrammaticalStructure
Returns:
a node which is the predicate of node t, or else null

getNominalSubject

public TreeGraphNode getNominalSubject(TreeGraphNode t)
Tries to return a node representing the NOMINAL_SUBJECT of the given node t. Probably, node t should represent a clause or verb phrase.

Parameters:
t - a node in this GrammaticalStructure
Returns:
a node which is the nominal subject of node t, or else null

getClausalSubject

public TreeGraphNode getClausalSubject(TreeGraphNode t)
Tries to return a node representing the CLAUSAL_SUBJECT of the given node t. Probably, node t should represent a clause or verb phrase.

Parameters:
t - a node in this GrammaticalStructure
Returns:
a node which is the clausal subject of node t, or else null

getSubject

public TreeGraphNode getSubject(TreeGraphNode t)
Tries to return a node representing the SUBJECT (whether nominal or clausal) of the given node t. Probably, node t should represent a clause or verb phrase.

Parameters:
t - a node in this GrammaticalStructure
Returns:
a node which is the subject of node t, or else null

getDirectObject

public TreeGraphNode getDirectObject(TreeGraphNode t)
Tries to return a node representing the DIRECT_OBJECT of the given node t. Probably, node t should represent a clause or verb phrase.

Parameters:
t - a node in this GrammaticalStructure
Returns:
a node which is the direct object of node t, or else null

getIndirectObject

public TreeGraphNode getIndirectObject(TreeGraphNode t)
Tries to return a node representing the INDIRECT_OBJECT of the given node t. Probably, node t should represent a clause or verb phrase.

Parameters:
t - a node in this GrammaticalStructure
Returns:
a node which is the indirect object of node t, or else null

getClausalComplement

public TreeGraphNode getClausalComplement(TreeGraphNode t)
Tries to return a node representing the CLAUSAL_COMPLEMENT of the given node t. Probably, node t should represent a clause or verb phrase.

Parameters:
t - a node in this GrammaticalStructure
Returns:
a node which is the clausal complement of node t, or else null

getAdjectivalComplement

public TreeGraphNode getAdjectivalComplement(TreeGraphNode t)
Tries to return a node representing the ADJECTIVAL_COMPLEMENT of the given node t. Probably, node t should represent a clause or verb phrase.

Parameters:
t - a node in this GrammaticalStructure
Returns:
a node which is the adjectival complement of node t, or else null

getTemporalModifier

public TreeGraphNode getTemporalModifier(TreeGraphNode t)
Tries to return a node representing a TEMPORAL_MODIFIER of the given node t. Probably, node t should represent a clause or verb phrase. (Currently, only NP-TMPs are recognized, not PP-TMPs).

Parameters:
t - a node in this GrammaticalStructure
Returns:
a node which is a temporal modifier of node t, or else null

getGrammaticalRelation

public GrammaticalRelation getGrammaticalRelation(int govIndex,
                                                  int depIndex)
Get GrammaticalRelation between gov and dep, and null if gov is not the governor of dep


getGrammaticalRelation

public GrammaticalRelation getGrammaticalRelation(TreeGraphNode gov,
                                                  TreeGraphNode dep)
Get GrammaticalRelation between gov and dep, and null if gov is not the governor of dep


typedDependencies

public java.util.List typedDependencies()
Returns a List of Triples, each representing a typed dependency in the GrammaticalStructure. Each Triple is of the form (GrammaticalRelation, TreeGraphNode, TreeGraphNode), where both TreeGraphNodes are leaf (terminal) nodes. The first TreeGraphNode is the governor, the second is the dependent, and the GrammaticalRelation is the most specific relation found to hold between the two nodes.


getDependencyPath

public java.util.List getDependencyPath(int nodeIndex,
                                        int rootIndex)
Returns the dependency path as a list of labels, from node to root, it is assumed that that root is an ancestor of node

Parameters:
nodeIndex -
rootIndex -
Returns:
a list of dependency labels

getDependencyPath

public java.util.List getDependencyPath(TreeGraphNode node,
                                        TreeGraphNode root)
Returns the dependency path as a list of labels, from node to root, it is assumed that that root is an ancestor of node

Parameters:
node -
root -
Returns:
a list of dependency labels

typedDependenciesToString

public java.lang.String typedDependenciesToString(java.lang.String format)
Returns a String representation of the result of typedDependencies() in a user-specified format. Currently, two formats are supported:
"xml"
Formats the dependencies as XML, as exemplified by the following:
  <dependencies>
    <dep type="tmod">
      <governor idx="6">died</governor>
      <dependent idx="9">today</dependent>
    </dep>
    <dep type="nsubj">
      <governor idx="6">died</governor>
      <dependent idx="3">Sam</dependent>
    </dep>
  </dependencies>
  
"plain"
(Default.) Formats the dependencies as logical relations, as exemplified by the following:
  tmod(died-6, today-9)
  nsubj(died-6, Sam-3)
  

Parameters:
format - a String specifying the desired format
Returns:
a String representation of the typed dependencies in this GrammaticalStructure

typedDependenciesToString

public java.lang.String typedDependenciesToString()
Returns a String representation of the result of typedDependencies() as exemplified by the following:
  tmod(died-6, today-9)
  nsubj(died-6, Sam-3)
  

Returns:
a String representation of the typed dependencies in this GrammaticalStructure

printTypedDependencies

public void printTypedDependencies()
Prints the result of typedDependenciesToString() to stdout.


printTypedDependencies

public void printTypedDependencies(java.io.PrintWriter pw)
Prints the result of typedDependenciesToString() to the specified PrintWriter.


printTypedDependencies

public void printTypedDependencies(java.lang.String format)
Prints the result of typedDependenciesToString(format) to stdout.


printTypedDependencies

public void printTypedDependencies(java.lang.String format,
                                   java.io.PrintWriter pw)
Prints the result of typedDependenciesToString(format) to the specified PrintWriter.


main

public static void main(java.lang.String[] args)
Just for testing. Usage: java GrammaticalStructure [treeFile]