public class GrammaticalRelation extends java.lang.Object implements java.lang.Comparable<GrammaticalRelation>, java.io.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:
String
short name, which should be a lowercase
abbreviation of some kind (in the fure mainly Universal Dependency names).String
long name, which should be descriptive.GrammaticalRelation
hierarchy.{@code 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. This prefiltering is used for efficiency.){@code 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, while
literal string label descriptions that are not regular expressions must
be equals()
.)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 {@code TregexPattern}
such that:
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 {@code 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, {@code getRelatedNodes()}
can be overridden on a per-relation basis using anonymous subclassing.
GrammaticalStructure
,
EnglishGrammaticalStructure
,
EnglishGrammaticalRelations
,
ChineseGrammaticalRelations
,
Serialized FormModifier and Type | Field and Description |
---|---|
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, e.g., in English & Chinese GrammaticalStructure
|
static GrammaticalRelation |
ROOT
The "root" grammatical relation between a faked "ROOT" node, and the root of the sentence.
|
Constructor and Description |
---|
GrammaticalRelation(Language language,
java.lang.String shortName,
java.lang.String longName,
GrammaticalRelation parent) |
GrammaticalRelation(Language language,
java.lang.String shortName,
java.lang.String longName,
GrammaticalRelation parent,
java.lang.String specificString) |
GrammaticalRelation(Language language,
java.lang.String shortName,
java.lang.String longName,
GrammaticalRelation parent,
java.lang.String sourcePattern,
TregexPatternCompiler tregexCompiler,
java.lang.String... targetPatterns) |
Modifier and Type | Method and Description |
---|---|
int |
compareTo(GrammaticalRelation o) |
boolean |
equals(java.lang.Object o)
Grammatical relations are equal with other grammatical relations if they
have the same shortName and specific (if present).
|
Language |
getLanguage()
Get the language of the grammatical relation.
|
java.lang.String |
getLongName() |
GrammaticalRelation |
getParent()
Returns the parent of this
GrammaticalRelation . |
java.util.Collection<TreeGraphNode> |
getRelatedNodes(TreeGraphNode t,
TreeGraphNode root,
HeadFinder headFinder)
Given a
Tree node t , attempts to
return a list of nodes to which node t has this
grammatical relation, with t as the governor. |
java.lang.String |
getShortName() |
java.lang.String |
getSpecific() |
int |
hashCode() |
boolean |
isAncestor(GrammaticalRelation gr)
Returns whether this is equal to or an ancestor of gr in the grammatical relations hierarchy.
|
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(java.lang.String[] args) |
protected java.lang.Object |
readResolve()
When deserializing a GrammaticalRelation, it needs to be matched
up with the existing singleton relation of the same type.
|
java.lang.String |
toPrettyString()
Returns a
String representation of this
GrammaticalRelation and the hierarchy below
it, with one node per line, indented according to level. |
java.lang.String |
toString()
Returns short name (abbreviation) for this
GrammaticalRelation . |
static GrammaticalRelation |
valueOf(Language language,
java.lang.String s)
Convert from a String representation of a GrammaticalRelation to a
GrammaticalRelation.
|
static GrammaticalRelation |
valueOf(java.lang.String s) |
static GrammaticalRelation |
valueOf(java.lang.String s,
java.util.Collection<GrammaticalRelation> values,
java.util.concurrent.locks.Lock readValuesLock)
Returns the GrammaticalRelation having the given string
representation (e.g.
|
static GrammaticalRelation |
valueOf(java.lang.String s,
java.util.Map<java.lang.String,GrammaticalRelation> map)
Returns the GrammaticalRelation having the given string
representation (e.g.
|
public static final GrammaticalRelation GOVERNOR
Example: "the red car" → gov
(red, car)
public static final GrammaticalRelation DEPENDENT
Example: "the red car" → dep
(car, red)
public static final GrammaticalRelation ROOT
public static final GrammaticalRelation KILL
public GrammaticalRelation(Language language, java.lang.String shortName, java.lang.String longName, GrammaticalRelation parent, java.lang.String sourcePattern, TregexPatternCompiler tregexCompiler, java.lang.String... targetPatterns)
public GrammaticalRelation(Language language, java.lang.String shortName, java.lang.String longName, GrammaticalRelation parent)
public GrammaticalRelation(Language language, java.lang.String shortName, java.lang.String longName, GrammaticalRelation parent, java.lang.String specificString)
public static GrammaticalRelation valueOf(java.lang.String s, java.util.Collection<GrammaticalRelation> values, java.util.concurrent.locks.Lock readValuesLock)
s
- The short name of the GrammaticalRelationvalues
- The set of GrammaticalRelations to look for it among.public static GrammaticalRelation valueOf(java.lang.String s, java.util.Map<java.lang.String,GrammaticalRelation> map)
s
- The short name of the GrammaticalRelationmap
- The map from string to GrammaticalRelationpublic static GrammaticalRelation valueOf(Language language, java.lang.String s)
s
- The String representation of a GrammaticalRelationpublic static GrammaticalRelation valueOf(java.lang.String s)
public boolean isFromString()
public java.util.Collection<TreeGraphNode> getRelatedNodes(TreeGraphNode t, TreeGraphNode root, HeadFinder headFinder)
Tree
node t
, attempts to
return a list of nodes to which node t
has this
grammatical relation, with t
as the governor.t
- Target for finding dependents of t related by this GRroot
- The root of the Treepublic boolean isApplicable(Tree t)
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.public boolean isAncestor(GrammaticalRelation gr)
public final java.lang.String toString()
GrammaticalRelation
. toString() for collapsed
relations will include the word that was collapsed.
toString
in class java.lang.Object
public java.lang.String toPrettyString()
String
representation of this
GrammaticalRelation
and the hierarchy below
it, with one node per line, indented according to level.String
representation of this
GrammaticalRelation
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
o
- Object to be comparedpublic int hashCode()
hashCode
in class java.lang.Object
public int compareTo(GrammaticalRelation o)
compareTo
in interface java.lang.Comparable<GrammaticalRelation>
public java.lang.String getLongName()
public java.lang.String getShortName()
public Language getLanguage()
public java.lang.String getSpecific()
protected java.lang.Object readResolve() throws java.io.ObjectStreamException
java.io.ObjectStreamException
public GrammaticalRelation getParent()
GrammaticalRelation
.public static void main(java.lang.String[] args)