public class SemanticGraph extends Object implements Serializable
Notes:
The root is not at present represented as a vertex in the graph.
At present you need to get a root/roots
from the separate roots variable and to know about it.
This should maybe be changed, because otherwise, doing things like
simply getting the set of nodes or edges from the graph doesn't give
you root nodes or edges.
Given the kinds of representations that we normally use with
typedDependenciesCollapsed, there can be (small) cycles in a
SemanticGraph, and these cycles may involve the node that is conceptually the
root of the graph, so there may be no node without a parent node. You can
better get at the root(s) via the variable and methods provided.
There is no mechanism for returning all edges at once (eg edgeSet()
).
This is intentional. Use edgeIterable()
to iterate over the edges if necessary.
SemanticGraphEdge
,
IndexedWord
,
Serialized FormModifier and Type | Class and Description |
---|---|
static class |
SemanticGraph.OutputFormat |
Modifier and Type | Field and Description |
---|---|
static boolean |
addSRLArcs |
Constructor and Description |
---|
SemanticGraph() |
SemanticGraph(Collection<TypedDependency> dependencies)
This is the constructor used by the parser.
|
SemanticGraph(SemanticGraph g)
Returns a new SemanticGraph which is a copy of the supplied SemanticGraph.
|
SemanticGraph(SemanticGraph g,
Map<IndexedWord,IndexedWord> prevToNewMap)
Copies a the current graph, but also sets the mapping from the old to new
graph.
|
Modifier and Type | Method and Description |
---|---|
SemanticGraphEdge |
addEdge(IndexedWord s,
IndexedWord d,
GrammaticalRelation reln,
double weight,
boolean isExtra) |
void |
addRoot(IndexedWord root) |
void |
addVertex(IndexedWord vertex) |
boolean |
attachedNegatedVerb(IndexedWord vertex) |
List<Pair<GrammaticalRelation,IndexedWord>> |
childPairs(IndexedWord vertex)
Returns a list of pairs of a relation name and the child
IndexedFeatureLabel that bears that relation.
|
Set<GrammaticalRelation> |
childRelns(IndexedWord vertex)
Returns a list of relations which this node has with its children.
|
int |
commonAncestor(IndexedWord v1,
IndexedWord v2)
Return the maximum distance to a least common ancestor.
|
boolean |
containsEdge(IndexedWord source,
IndexedWord target) |
boolean |
containsEdge(SemanticGraphEdge edge) |
boolean |
containsVertex(IndexedWord vertex) |
Set<IndexedWord> |
descendants(IndexedWord vertex)
Returns the set of descendants governed by this node in the graph.
|
int |
edgeCount() |
Iterable<SemanticGraphEdge> |
edgeIterable() |
List<SemanticGraphEdge> |
edgeListSorted()
Returns an ordered list of edges in the graph.
|
boolean |
equals(Object o) |
List<SemanticGraphEdge> |
findAllRelns(GrammaticalRelation tgtRelation)
Given a semantic graph, and a target relation, returns a list of all
relations (edges) matching.
|
List<SemanticGraphEdge> |
getAllEdges(IndexedWord gov,
IndexedWord dep) |
List<IndexedWord> |
getAllNodesByWordPattern(String pattern)
Returns all nodes of type
IndexedWord in this SemanticGraph having the given word or
regex, or returns empty list if no such found. |
List<IndexedWord> |
getChildList(IndexedWord vertex) |
Set<IndexedWord> |
getChildren(IndexedWord vertex) |
Set<IndexedWord> |
getChildrenWithReln(IndexedWord vertex,
GrammaticalRelation reln)
Returns a set of all children bearing a certain grammatical relation, or
an empty set if none.
|
Set<IndexedWord> |
getChildrenWithRelns(IndexedWord vertex,
Collection<GrammaticalRelation> relns)
Returns a set of all children bearing one of a set of grammatical
relations, or an empty set if none.
|
IndexedWord |
getChildWithReln(IndexedWord vertex,
GrammaticalRelation reln)
Returns the first IndexedFeatureLabel bearing a certain grammatical
relation, or null if none.
|
IndexedWord |
getCommonAncestor(IndexedWord v1,
IndexedWord v2)
Returns the least common ancestor.
|
SemanticGraphEdge |
getEdge(IndexedWord gov,
IndexedWord dep) |
SemanticGraphEdge |
getEdge(IndexedWord gov,
IndexedWord dep,
GrammaticalRelation reln)
Given a governor, dependent, and the relation between them, returns the
SemanticGraphEdge object of that arc if it exists, otherwise returns null.
|
IndexedWord |
getFirstRoot()
Returns the (first) root of this SemanticGraph.
|
List<SemanticGraphEdge> |
getIncomingEdgesSorted(IndexedWord vertex) |
Set<IndexedWord> |
getLeafVertices() |
IndexedWord |
getNodeByIndex(int index)
Returns the first
IndexedWord in this SemanticGraph having the given integer index,
or throws IllegalArgumentException if no such node is found. |
IndexedWord |
getNodeByIndexSafe(int index)
Same as above, but returns
null if the index does not exist
(instead of throwing an exception). |
IndexedWord |
getNodeByWordPattern(String pattern)
Returns the first
IndexedWord in this SemanticGraph having the given word or
regex, or return null if no such found. |
List<SemanticGraphEdge> |
getOutEdgesSorted(IndexedWord vertex) |
IndexedWord |
getParent(IndexedWord vertex)
Return the real syntactic parent of vertex.
|
List<IndexedWord> |
getParentList(IndexedWord vertex) |
Set<IndexedWord> |
getParents(IndexedWord vertex) |
Set<IndexedWord> |
getParentsWithReln(IndexedWord vertex,
GrammaticalRelation reln)
Returns a set of all parents bearing a certain grammatical relation, or an
empty set if none.
|
List<IndexedWord> |
getPathToRoot(IndexedWord vertex)
Find the path from the given node to a root.
|
Collection<IndexedWord> |
getRoots() |
List<SemanticGraphEdge> |
getShortestDirectedPathEdges(IndexedWord source,
IndexedWord target) |
List<IndexedWord> |
getShortestDirectedPathNodes(IndexedWord source,
IndexedWord target)
Returns the shortest directed path between two edges in the graph.
|
List<SemanticGraphEdge> |
getShortestUndirectedPathEdges(IndexedWord source,
IndexedWord target) |
List<IndexedWord> |
getShortestUndirectedPathNodes(IndexedWord source,
IndexedWord target)
Returns the nodes in the shortest undirected path between two edges in the
graph.
|
Collection<IndexedWord> |
getSiblings(IndexedWord vertex)
Method for getting the siblings of a particular node.
|
boolean |
hasChild(IndexedWord vertex,
GrammaticalRelation reln,
String childLemma)
Does the given
vertex have at least one child with the given reln and the lemma childLemma ? |
boolean |
hasChildren(IndexedWord vertex) |
boolean |
hasChildWithReln(IndexedWord vertex,
GrammaticalRelation reln)
Does the given
vertex have at least one child with the given reln ? |
int |
hashCode() |
boolean |
hasParentWithReln(IndexedWord vertex,
GrammaticalRelation reln)
Returns true if vertex has an incoming relation reln
|
Iterable<SemanticGraphEdge> |
incomingEdgeIterable(IndexedWord v) |
Iterator<SemanticGraphEdge> |
incomingEdgeIterator(IndexedWord v) |
List<SemanticGraphEdge> |
incomingEdgeList(IndexedWord v) |
int |
inDegree(IndexedWord vertex) |
int |
isAncestor(IndexedWord child,
IndexedWord ancestor)
Searches up to 2 levels to determine how far ancestor is from child (i.e.,
returns 1 if "ancestor" is a parent, or 2 if ancestor is a grandparent.
|
boolean |
isAuxiliaryVerb(IndexedWord vertex)
Returns true iff this vertex stands in the "aux" relation to (any of)
its parent(s).
|
boolean |
isDag() |
boolean |
isEmpty() |
boolean |
isInConditionalContext(IndexedWord vertex)
Check if the vertex is in a "conditional" context.
|
boolean |
isNegatedVertex(IndexedWord vertex) |
boolean |
matchPatternToVertex(String pattern,
IndexedWord vertex) |
boolean |
matchPatternToVertex(String pattern,
IndexedWord vertex,
boolean det) |
int |
outDegree(IndexedWord vertex) |
Iterable<SemanticGraphEdge> |
outgoingEdgeIterable(IndexedWord v) |
Iterator<SemanticGraphEdge> |
outgoingEdgeIterator(IndexedWord v) |
List<SemanticGraphEdge> |
outgoingEdgeList(IndexedWord v) |
List<Pair<GrammaticalRelation,IndexedWord>> |
parentPairs(IndexedWord vertex)
Returns a list of pairs of a relation name and the parent
IndexedFeatureLabel to which we bear that relation.
|
void |
prettyPrint()
Pretty-prints this semantic graph to
System.out , formatted by
the default semantic graph formatter. |
void |
prettyPrint(SemanticGraphFormatter formatter)
Pretty-prints this semantic graph to
System.out , formatted by
the supplied semantic graph formatter. |
GrammaticalRelation |
reln(IndexedWord a,
IndexedWord b)
Returns the relation that node a has with node b.
|
Set<GrammaticalRelation> |
relns(IndexedWord vertex)
Returns a set of relations which this node has with its parents.
|
boolean |
removeEdge(SemanticGraphEdge e) |
boolean |
removeVertex(IndexedWord vertex) |
void |
resetRoots()
This method should not be used if possible.
|
void |
setRoot(IndexedWord word) |
void |
setRoots(Collection<IndexedWord> words) |
int |
size() |
String |
toCompactString() |
String |
toCompactString(boolean showTags) |
String |
toDotFormat()
Returns an unnamed dot format digraph.
|
String |
toDotFormat(String graphname)
Returns a dot format digraph with the given name.
|
String |
toDotFormat(String graphname,
CoreLabel.OutputFormat indexedWordFormat) |
String |
toEnUncollapsedSentenceString()
Similar to
toRecoveredString , but will fill in words that were
collapsed into relations (i.e. |
String |
toFormattedString()
Returns a
String representation of this semantic graph,
formatted by the default semantic graph formatter. |
String |
toFormattedString(SemanticGraphFormatter formatter)
Returns a
String representation of this semantic graph,
formatted by the supplied semantic graph formatter. |
String |
toList()
Returns a String representation of this graph as a list of typed
dependencies, as exemplified by the following:
|
List<IndexedWord> |
topologicalSort() |
String |
toPOSList()
Similar to toList(), but uses POS tags instead of word and index.
|
String |
toRecoveredSentenceString() |
String |
toRecoveredSentenceStringWithIndexMarking() |
String |
toString()
Recursive depth first traversal.
|
String |
toString(CoreLabel.OutputFormat wordFormat) |
String |
toString(SemanticGraph.OutputFormat format)
Returns a String representation of the result of this set of typed
dependencies in a user-specified format.
|
Collection<TypedDependency> |
typedDependencies()
Returns a list of TypedDependency in the graph.
|
static SemanticGraph |
valueOf(String s)
Tries to parse a String representing a SemanticGraph.
|
List<IndexedWord> |
vertexListSorted()
This returns an ordered list of vertices (based upon their
indices in the sentence).
|
Set<IndexedWord> |
vertexSet() |
public static final boolean addSRLArcs
public SemanticGraph()
public SemanticGraph(SemanticGraph g)
IndexedWord
s) and the edges (SemanticGraphEdges)
are copied.public SemanticGraph(SemanticGraph g, Map<IndexedWord,IndexedWord> prevToNewMap)
public SemanticGraph(Collection<TypedDependency> dependencies)
public int edgeCount()
public int outDegree(IndexedWord vertex)
public int inDegree(IndexedWord vertex)
public List<SemanticGraphEdge> getAllEdges(IndexedWord gov, IndexedWord dep)
public SemanticGraphEdge getEdge(IndexedWord gov, IndexedWord dep)
public void addVertex(IndexedWord vertex)
public boolean containsVertex(IndexedWord vertex)
public boolean containsEdge(IndexedWord source, IndexedWord target)
public boolean containsEdge(SemanticGraphEdge edge)
public Set<IndexedWord> vertexSet()
public boolean removeEdge(SemanticGraphEdge e)
public boolean removeVertex(IndexedWord vertex)
public List<IndexedWord> vertexListSorted()
public List<SemanticGraphEdge> edgeListSorted()
public Iterable<SemanticGraphEdge> edgeIterable()
public Iterator<SemanticGraphEdge> outgoingEdgeIterator(IndexedWord v)
public Iterable<SemanticGraphEdge> outgoingEdgeIterable(IndexedWord v)
public Iterator<SemanticGraphEdge> incomingEdgeIterator(IndexedWord v)
public Iterable<SemanticGraphEdge> incomingEdgeIterable(IndexedWord v)
public List<SemanticGraphEdge> outgoingEdgeList(IndexedWord v)
public List<SemanticGraphEdge> incomingEdgeList(IndexedWord v)
public boolean isEmpty()
public int isAncestor(IndexedWord child, IndexedWord ancestor)
child
- candidate childancestor
- candidate ancestorpublic int commonAncestor(IndexedWord v1, IndexedWord v2)
public IndexedWord getCommonAncestor(IndexedWord v1, IndexedWord v2)
public boolean matchPatternToVertex(String pattern, IndexedWord vertex, boolean det)
public boolean matchPatternToVertex(String pattern, IndexedWord vertex)
public List<IndexedWord> getChildList(IndexedWord vertex)
public Set<IndexedWord> getChildren(IndexedWord vertex)
public boolean hasChildren(IndexedWord vertex)
public List<SemanticGraphEdge> getIncomingEdgesSorted(IndexedWord vertex)
public List<SemanticGraphEdge> getOutEdgesSorted(IndexedWord vertex)
public List<IndexedWord> getParentList(IndexedWord vertex)
public Set<IndexedWord> getParents(IndexedWord vertex)
public Collection<IndexedWord> getSiblings(IndexedWord vertex)
public List<IndexedWord> getPathToRoot(IndexedWord vertex)
public IndexedWord getParent(IndexedWord vertex)
public IndexedWord getNodeByIndex(int index) throws IllegalArgumentException
IndexedWord
in this SemanticGraph
having the given integer index,
or throws IllegalArgumentException
if no such node is found.IllegalArgumentException
public IndexedWord getNodeByIndexSafe(int index)
null
if the index does not exist
(instead of throwing an exception).public IndexedWord getNodeByWordPattern(String pattern)
IndexedWord
in this SemanticGraph
having the given word or
regex, or return null if no such found.public List<IndexedWord> getAllNodesByWordPattern(String pattern)
IndexedWord
in this SemanticGraph
having the given word or
regex, or returns empty list if no such found.public Set<IndexedWord> descendants(IndexedWord vertex)
public List<Pair<GrammaticalRelation,IndexedWord>> childPairs(IndexedWord vertex)
public List<Pair<GrammaticalRelation,IndexedWord>> parentPairs(IndexedWord vertex)
public Set<GrammaticalRelation> relns(IndexedWord vertex)
public GrammaticalRelation reln(IndexedWord a, IndexedWord b)
a
and
b
, and this method only returns one relation.public Set<GrammaticalRelation> childRelns(IndexedWord vertex)
public Collection<IndexedWord> getRoots()
public IndexedWord getFirstRoot()
public void addRoot(IndexedWord root)
public void resetRoots()
public void setRoot(IndexedWord word)
public void setRoots(Collection<IndexedWord> words)
public List<IndexedWord> topologicalSort()
IllegalStateException
- if this graph is not a DAGpublic boolean hasChild(IndexedWord vertex, GrammaticalRelation reln, String childLemma)
vertex
have at least one child with the given reln
and the lemma childLemma
?public boolean hasChildWithReln(IndexedWord vertex, GrammaticalRelation reln)
vertex
have at least one child with the given reln
?public boolean hasParentWithReln(IndexedWord vertex, GrammaticalRelation reln)
vertex
- A node in this graphreln
- The relation we want to checkpublic IndexedWord getChildWithReln(IndexedWord vertex, GrammaticalRelation reln)
public Set<IndexedWord> getParentsWithReln(IndexedWord vertex, GrammaticalRelation reln)
public Set<IndexedWord> getChildrenWithReln(IndexedWord vertex, GrammaticalRelation reln)
public Set<IndexedWord> getChildrenWithRelns(IndexedWord vertex, Collection<GrammaticalRelation> relns)
public SemanticGraphEdge getEdge(IndexedWord gov, IndexedWord dep, GrammaticalRelation reln)
public boolean isNegatedVertex(IndexedWord vertex)
public boolean isInConditionalContext(IndexedWord vertex)
public boolean attachedNegatedVerb(IndexedWord vertex)
public boolean isAuxiliaryVerb(IndexedWord vertex)
public Set<IndexedWord> getLeafVertices()
public int size()
public boolean isDag()
public String toString()
/-> need-3 (root) -> We-0 (nsubj) -> do-1 (aux) -> n't-2 (neg) -> badges-6 (dobj) -> no-4 (det) -> stinking-5 (amod)
public String toString(CoreLabel.OutputFormat wordFormat)
public String toRecoveredSentenceString()
public String toRecoveredSentenceStringWithIndexMarking()
public String toEnUncollapsedSentenceString()
toRecoveredString
, but will fill in words that were
collapsed into relations (i.e. prep_for --> 'for'). Mostly to deal with
collapsed dependency trees.
TODO: consider merging with toRecoveredString() NOTE: assumptions currently
are for English. NOTE: currently takes immediate successors to current word
and expands them. This assumption may not be valid for other conditions or
languages?public String toString(SemanticGraph.OutputFormat format)
SemanticGraph.OutputFormat
):
nsubj(died-1, Sam-0) tmod(died-1, today-2)
dependent
, relation
, and governor
, as exemplified by the
following:
Sam-0 nsubj died-1 today-2 tmod died-1
<dependencies> <dep type="nsubj"> <governor idx="1">died</governor> <dependent idx="0">Sam</dependent> </dep> <dep type="tmod"> <governor idx="1">died</governor> <dependent idx="2">today</dependent> </dep> </dependencies>
toString()
format
- a String
specifying the desired formatString
representation of the typed dependencies in
this GrammaticalStructure
public String toList()
nsubj(died-6, Sam-3) tmod(died-6, today-9)
String
representation of this set of typed
dependenciespublic String toPOSList()
public String toCompactString()
public String toCompactString(boolean showTags)
public String toFormattedString()
String
representation of this semantic graph,
formatted by the default semantic graph formatter.public String toFormattedString(SemanticGraphFormatter formatter)
String
representation of this semantic graph,
formatted by the supplied semantic graph formatter.public void prettyPrint(SemanticGraphFormatter formatter)
System.out
, formatted by
the supplied semantic graph formatter.public void prettyPrint()
System.out
, formatted by
the default semantic graph formatter.public String toDotFormat()
public String toDotFormat(String graphname)
public String toDotFormat(String graphname, CoreLabel.OutputFormat indexedWordFormat)
public SemanticGraphEdge addEdge(IndexedWord s, IndexedWord d, GrammaticalRelation reln, double weight, boolean isExtra)
public static SemanticGraph valueOf(String s)
public List<IndexedWord> getShortestUndirectedPathNodes(IndexedWord source, IndexedWord target)
source
- nodetarget
- nodepublic List<SemanticGraphEdge> getShortestUndirectedPathEdges(IndexedWord source, IndexedWord target)
public List<IndexedWord> getShortestDirectedPathNodes(IndexedWord source, IndexedWord target)
source
- nodetarget
- nodepublic List<SemanticGraphEdge> getShortestDirectedPathEdges(IndexedWord source, IndexedWord target)
public List<SemanticGraphEdge> findAllRelns(GrammaticalRelation tgtRelation)
public Collection<TypedDependency> typedDependencies()