use of edu.stanford.nlp.ling.IndexedWord in project CoreNLP by stanfordnlp.
the class SemanticGraph method toRecoveredSentenceString.
public String toRecoveredSentenceString() {
StringBuilder sb = new StringBuilder();
boolean pastFirst = false;
for (IndexedWord word : vertexListSorted()) {
if (pastFirst) {
sb.append(' ');
}
pastFirst = true;
sb.append(word.word());
}
return sb.toString();
}
use of edu.stanford.nlp.ling.IndexedWord in project CoreNLP by stanfordnlp.
the class SemanticGraph method getAllNodesByWordPattern.
/**
* Returns all nodes of type {@link edu.stanford.nlp.ling.IndexedWord
* IndexedWord} in this <code>SemanticGraph</code> having the given word or
* regex, or returns empty list if no such found.
*/
public List<IndexedWord> getAllNodesByWordPattern(String pattern) {
Pattern p = Pattern.compile(pattern);
List<IndexedWord> nodes = new ArrayList<>();
for (IndexedWord vertex : vertexSet()) {
String w = vertex.word();
if ((w == null && pattern == null) || w != null && p.matcher(w).matches()) {
nodes.add(vertex);
}
}
return nodes;
}
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;
}
Aggregations