## edu.stanford.nlp.trees
Class GrammaticalRelation

    java.lang.Object
      edu.stanford.nlp.trees.GrammaticalRelation

All Implemented Interfaces:
  Serializable, Comparable<GrammaticalRelation>

    public class GrammaticalRelation

extends Object
implements Comparable<GrammaticalRelation>, Serializable

`GrammaticalRelation` is used to define a standardized, hierarchical set of grammatical relations, together with patterns for identifying them in parse trees.

Each `GrammaticalRelation` has:

- A `String` short name, which should be a lowercase abbreviation of some kind.
- A `String` long name, which should be descriptive.
- A parent in the `GrammaticalRelation` hierarchy.
- A ``Pattern`` called `sourcePattern` which matches (parent) nodes from which this `GrammaticalRelation` could hold. (Note: this is done with the Java regex Pattern `matches()` predicate: the pattern must match the whole node name, and `^` or `$` aren't needed. Tregex constructions like __ do not work. Use ".*" to be applicable at all nodes.)
- A list of zero or more ``TregexPattern`s` called `targetPatterns`, which describe the local tree structure which must hold between the source node and a target node for the `GrammaticalRelation` to apply. (Note `tregex` regular expressions match with the `find()` method - though literal string label descriptions that are not regular expressions must be `equals()`.)

The `targetPatterns` associated with a `GrammaticalRelation` are designed as follows. In order to recognize a grammatical relation X holding between nodes A and B in a parse tree, we want to associate with `GrammaticalRelation` X a ``TregexPattern`` such that:

- the root of the pattern matches A, and
- the pattern includes a special node label, "target", which matches B.

For example, for the grammatical relation `PREDICATE` which holds between a clause and its primary verb phrase, we might want to use the pattern `"S < VP=target"`, in which the root will match a clause and the node labeled `"target"` will match the verb phrase.

For a given grammatical relation, the method ``getRelatedNodes()`` takes a `Tree` node as an argument and attempts to return other nodes which have this grammatical relation to the argument node. By default, this method operates as follows: it steps through the patterns in the pattern list, trying to match each pattern against the argument node, until it finds some matches. If a pattern matches, all matching nodes (that is, each node which corresponds to node label "target" in some match) are returned as a list; otherwise the next pattern is tried.

For some grammatical relations, we need more sophisticated logic to identify related nodes. In such cases, ``getRelatedNodes()`` can be overridden on a per-relation basis using anonymous subclassing.

Author:
  Bill MacCartney, Galen Andrew (refactoring English-specific stuff), Ilya Sherman (refactoring annotation-relation pairing)
See Also:
  `GrammaticalStructure`, `EnglishGrammaticalStructure`, `EnglishGrammaticalRelations`, `edu.stanford.nlp.trees.international.pennchinese.ChineseGrammaticalRelations`, Serialized Form

Nested Class Summary
`static class` `GrammaticalRelation.DependentGRAnnotation`
`static class` `GrammaticalRelation.GovernorGRAnnotation`
`static class` `GrammaticalRelation.GrammaticalRelationAnnotation`
`static class` `GrammaticalRelation.KillGRAnnotation`
`static class` `GrammaticalRelation.Language`

Field Summary
`static GrammaticalRelation` `DEPENDENT` The "dependent" grammatical relation, which is the inverse of "governor".
`static GrammaticalRelation` `GOVERNOR` The "governor" grammatical relation, which is the inverse of "dependent".
`static GrammaticalRelation` `KILL` Dummy relation, used while collapsing relations, in English & Chinese GrammaticalStructure

Constructor Summary
`GrammaticalRelation(GrammaticalRelation.Language language, String shortName, String longName, Class<? extends GrammaticalRelation.GrammaticalRelationAnnotation> annotation, GrammaticalRelation parent)`
`GrammaticalRelation(GrammaticalRelation.Language language, String shortName, String longName, Class<? extends GrammaticalRelation.GrammaticalRelationAnnotation> annotation, GrammaticalRelation parent, String specificString)`
`GrammaticalRelation(GrammaticalRelation.Language language, String shortName, String longName, Class<? extends GrammaticalRelation.GrammaticalRelationAnnotation> annotation, GrammaticalRelation parent, String sourcePattern, String[] targetPatterns)`
`GrammaticalRelation(GrammaticalRelation.Language language, String shortName, String longName, Class<? extends GrammaticalRelation.GrammaticalRelationAnnotation> annotation, GrammaticalRelation parent, String sourcePattern, String[] targetPatterns, String specificString)`

Method Summary
`int` `compareTo(GrammaticalRelation o)`
`boolean` `equals(Object o)` Grammatical relations are equal with other grammatical relations if they have the same shortName and specific (if present).
`static Class<? extends GrammaticalRelation.GrammaticalRelationAnnotation>` `getAnnotationClass(GrammaticalRelation relation)`
`String` `getLongName()`
`GrammaticalRelation` `getParent()` Returns the parent of this `GrammaticalRelation`.
`Collection<Tree>` `getRelatedNodes(Tree t, Tree root)` Given a `Tree` node `t`, attempts to return a list of nodes to which node `t` has this grammatical relation.
`static GrammaticalRelation` `getRelation(Class<? extends GrammaticalRelation.GrammaticalRelationAnnotation> annotation)`
`String` `getShortName()`
`String` `getSpecific()`
`int` `hashCode()`
`boolean` `isAncestor(GrammaticalRelation gr)`
`boolean` `isApplicable(Tree t)` Returns `true` iff the value of `Tree` node `t` matches the `sourcePattern` for this `GrammaticalRelation`, indicating that this `GrammaticalRelation` is one that could hold between `Tree` node `t` and some other node.
`boolean` `isFromString()` This function is used to determine whether the GrammaticalRelation in question is one that was created to be a thin wrapper around a String representation by valueOf(String), or whether it is a full-fledged GrammaticalRelation created by direct invocation of the constructor.
`static void` `main(String[] args)`
`String` `toPrettyString()` Returns a `String` representation of this `GrammaticalRelation` and the hierarchy below it, with one node per line, indented according to level.
`String` `toString()` Returns short name (abbreviation) for this `GrammaticalRelation`.
`static GrammaticalRelation` `valueOf(GrammaticalRelation.Language language, String s)` Convert from a String representation of a GrammaticalRelation to a GrammaticalRelation.
`static GrammaticalRelation` `valueOf(String s)`
`static GrammaticalRelation` `valueOf(String s, Collection<GrammaticalRelation> values)` Returns the GrammaticalRelation having the given string representation (e.g.

Methods inherited from class java.lang.Object
`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

Field Detail

### GOVERNOR

    public static final GrammaticalRelation GOVERNOR

  The "governor" grammatical relation, which is the inverse of "dependent".

  Example: "the red car" → `gov`(red, car)

### DEPENDENT

    public static final GrammaticalRelation DEPENDENT

  The "dependent" grammatical relation, which is the inverse of "governor".

  Example: "the red car" → `dep`(car, red)

### KILL

    public static final GrammaticalRelation KILL

  Dummy relation, used while collapsing relations, in English & Chinese GrammaticalStructure

Constructor Detail

### GrammaticalRelation

    public GrammaticalRelation(GrammaticalRelation.Language language,
                               String shortName,
                               String longName,
                               Class<? extends GrammaticalRelation.GrammaticalRelationAnnotation> annotation,
                               GrammaticalRelation parent,
                               String sourcePattern,
                               String[] targetPatterns,
                               String specificString)

### GrammaticalRelation

    public GrammaticalRelation(GrammaticalRelation.Language language,
                               String shortName,
                               String longName,
                               Class<? extends GrammaticalRelation.GrammaticalRelationAnnotation> annotation,
                               GrammaticalRelation parent,
                               String sourcePattern,
                               String[] targetPatterns)

### GrammaticalRelation

    public GrammaticalRelation(GrammaticalRelation.Language language,
                               String shortName,
                               String longName,
                               Class<? extends GrammaticalRelation.GrammaticalRelationAnnotation> annotation,
                               GrammaticalRelation parent)

### GrammaticalRelation

    public GrammaticalRelation(GrammaticalRelation.Language language,
                               String shortName,
                               String longName,
                               Class<? extends GrammaticalRelation.GrammaticalRelationAnnotation> annotation,
                               GrammaticalRelation parent,
                               String specificString)

Method Detail

### getAnnotationClass

    public static Class<? extends GrammaticalRelation.GrammaticalRelationAnnotation> getAnnotationClass(GrammaticalRelation relation)

### getRelation

    public static GrammaticalRelation getRelation(Class<? extends GrammaticalRelation.GrammaticalRelationAnnotation> annotation)

### valueOf

    public static GrammaticalRelation valueOf(String s,
                                              Collection<GrammaticalRelation> values)

  Returns the GrammaticalRelation having the given string representation (e.g. "nsubj"), or null if no such is found.

  Parameters:
    `s` - The short name of the GrammaticalRelation
    `values` - The set of GrammaticalRelations to look for it among.
  Returns:
    The GrammaticalRelation with that name

### valueOf

    public static GrammaticalRelation valueOf(GrammaticalRelation.Language language,
                                              String s)

  Convert from a String representation of a GrammaticalRelation to a GrammaticalRelation. Where possible, you should avoid using this method and simply work with true GrammaticalRelations rather than String representations. Correct behavior of this method depends on the underlying data structure resources used being kept in sync with the toString() and equals() methods. However, there is really no choice but to use this method when storing GrammaticalRelations to text files and then reading them back in, so this method is not deprecated.

  Parameters:
    `s` - The String representation of a GrammaticalRelation
  Returns:
    The grammatical relation represented by this String

### valueOf

    public static GrammaticalRelation valueOf(String s)

### isFromString

    public boolean isFromString()

  This function is used to determine whether the GrammaticalRelation in question is one that was created to be a thin wrapper around a String representation by valueOf(String), or whether it is a full-fledged GrammaticalRelation created by direct invocation of the constructor.

  Returns:
    Whether this relation is just a wrapper created by valueOf(String)

### getRelatedNodes

    public Collection<Tree> getRelatedNodes(Tree t,
                                            Tree root)

  Given a `Tree` node `t`, attempts to return a list of nodes to which node `t` has this grammatical relation.

  Parameters:
    `t` - Target for finding governors of t related by this GR
    `root` - The root of the Tree
  Returns:
    Governor nodes to which t bears this GR

### isApplicable

    public boolean isApplicable(Tree t)

  Returns `true` iff the value of `Tree` node `t` matches the `sourcePattern` for this `GrammaticalRelation`, indicating that this `GrammaticalRelation` is one that could hold between `Tree` node `t` and some other node.

### isAncestor

    public boolean isAncestor(GrammaticalRelation gr)

### toString

    public final String toString()

  Returns short name (abbreviation) for this `GrammaticalRelation`. Implementation note: Note that this method must be synced with the equals() and valueOf(String) methods

  Overrides:
    `toString` in class `Object`

### toPrettyString

    public String toPrettyString()

  Returns a `String` representation of this `GrammaticalRelation` and the hierarchy below it, with one node per line, indented according to level.

  Returns:
    `String` representation of this `GrammaticalRelation`

### equals

    public boolean equals(Object o)

  Grammatical relations are equal with other grammatical relations if they have the same shortName and specific (if present). Implementation note: Note that this method must be synced with the toString() and valueOf(String) methods

  Overrides:
    `equals` in class `Object`
  Parameters:
    `o` - Object to be compared
  Returns:
    Whether equal

### hashCode

    public int hashCode()

  Overrides:
    `hashCode` in class `Object`

### compareTo

    public int compareTo(GrammaticalRelation o)

  Specified by:
    `compareTo` in interface `Comparable<GrammaticalRelation>`

### getLongName

    public String getLongName()

### getShortName

    public String getShortName()

### getSpecific

    public String getSpecific()

### getParent

    public GrammaticalRelation getParent()

  Returns the parent of this `GrammaticalRelation`.

### main

    public static void main(String[] args)

Stanford NLP Group