use of edu.stanford.nlp.ling.IndexedWord in project CoreNLP by stanfordnlp.
the class SemanticGraph method recToString.
// helper for toString()
private void recToString(IndexedWord curr, CoreLabel.OutputFormat wordFormat, StringBuilder sb, int offset, Set<IndexedWord> used) {
used.add(curr);
List<SemanticGraphEdge> edges = outgoingEdgeList(curr);
Collections.sort(edges);
for (SemanticGraphEdge edge : edges) {
IndexedWord target = edge.getTarget();
sb.append(space(2 * offset)).append("-> ").append(target.toString(wordFormat)).append(" (").append(edge.getRelation()).append(")\n");
if (!used.contains(target)) {
// recurse
recToString(target, wordFormat, sb, offset + 1, used);
}
}
}
use of edu.stanford.nlp.ling.IndexedWord in project CoreNLP by stanfordnlp.
the class SemanticGraph method isDag.
/**
*
* @param root root node of the subgraph.
* @return true if the subgraph rooted at <code>root</code> contains no cycles.
*/
public boolean isDag(IndexedWord root) {
Set<IndexedWord> unused = wordMapFactory.newSet();
unused.addAll(this.getSubgraphVertices(root));
while (!unused.isEmpty()) {
IndexedWord arbitrary = unused.iterator().next();
boolean result = isDagHelper(arbitrary, unused, wordMapFactory.newSet());
if (result) {
return false;
}
}
return true;
}
use of edu.stanford.nlp.ling.IndexedWord in project CoreNLP by stanfordnlp.
the class SemanticGraphFactory method deepCopyFromGraphs.
/**
* Like makeFromGraphs, but it makes a deep copy of the graphs and
* renumbers the index words.
* <br>
* {@code lengths} must be a vector containing the number of
* tokens in each sentence. This is used to reindex the tokens.
*/
public static SemanticGraph deepCopyFromGraphs(List<SemanticGraph> graphs, List<Integer> lengths) {
SemanticGraph newGraph = new SemanticGraph();
Map<Integer, IndexedWord> newWords = Generics.newHashMap();
List<IndexedWord> newRoots = new ArrayList<>();
int vertexOffset = 0;
for (int i = 0; i < graphs.size(); ++i) {
SemanticGraph graph = graphs.get(i);
for (IndexedWord vertex : graph.vertexSet()) {
IndexedWord newVertex = new IndexedWord(vertex);
newVertex.setIndex(vertex.index() + vertexOffset);
newGraph.addVertex(newVertex);
newWords.put(newVertex.index(), newVertex);
}
for (SemanticGraphEdge edge : graph.edgeIterable()) {
IndexedWord gov = newWords.get(edge.getGovernor().index() + vertexOffset);
IndexedWord dep = newWords.get(edge.getDependent().index() + vertexOffset);
if (gov == null || dep == null) {
throw new AssertionError("Counting problem (or broken edge)");
}
newGraph.addEdge(gov, dep, edge.getRelation(), edge.getWeight(), edge.isExtra());
}
for (IndexedWord root : graph.getRoots()) {
newRoots.add(newWords.get(root.index() + vertexOffset));
}
vertexOffset += lengths.get(i);
}
newGraph.setRoots(newRoots);
return newGraph;
}
use of edu.stanford.nlp.ling.IndexedWord in project CoreNLP by stanfordnlp.
the class SemanticGraphFactory method makeFromEdges.
/**
* Given a list of edges, attempts to create and return a rooted SemanticGraph.
* <p>
* TODO: throw Exceptions, or flag warnings on conditions for concern (no root, etc)
*/
public static SemanticGraph makeFromEdges(Iterable<SemanticGraphEdge> edges) {
// Identify the root(s) of this graph
SemanticGraph sg = new SemanticGraph();
Collection<IndexedWord> vertices = getVerticesFromEdgeSet(edges);
for (IndexedWord vertex : vertices) {
sg.addVertex(vertex);
}
for (SemanticGraphEdge edge : edges) {
sg.addEdge(edge.getSource(), edge.getTarget(), edge.getRelation(), edge.getWeight(), edge.isExtra());
}
sg.resetRoots();
return sg;
}
use of edu.stanford.nlp.ling.IndexedWord in project CoreNLP by stanfordnlp.
the class SemanticGraphFactory method duplicateKeepNodes.
/**
* This creates a new graph based off the given, but uses the existing nodes objects.
*/
public static SemanticGraph duplicateKeepNodes(SemanticGraph sg) {
SemanticGraph retSg = new SemanticGraph();
for (IndexedWord node : sg.vertexSet()) {
retSg.addVertex(node);
}
retSg.setRoots(sg.getRoots());
for (SemanticGraphEdge edge : sg.edgeIterable()) {
retSg.addEdge(edge.getGovernor(), edge.getDependent(), edge.getRelation(), edge.getWeight(), edge.isExtra());
}
return retSg;
}
Aggregations