use of edu.stanford.nlp.ling.IndexedWord in project CoreNLP by stanfordnlp.
the class SemanticGraphUtils method setSentIndex.
/**
* GIven a graph, returns a new graph with the the new sentence index enforced.
* NOTE: new vertices are inserted.
* TODO: is this ok? rewrite this?
*/
public static SemanticGraph setSentIndex(SemanticGraph sg, int newSentIndex) {
SemanticGraph newGraph = new SemanticGraph(sg);
List<IndexedWord> prevRoots = new ArrayList<>(newGraph.getRoots());
List<IndexedWord> newRoots = new ArrayList<>();
// vertices while iterating. Perhaps there is a better way to do it.
for (IndexedWord node : newGraph.vertexListSorted()) {
IndexedWord newWord = new IndexedWord(node);
newWord.setSentIndex(newSentIndex);
SemanticGraphUtils.replaceNode(newWord, node, newGraph);
if (prevRoots.contains(node))
newRoots.add(newWord);
}
newGraph.setRoots(newRoots);
return newGraph;
}
use of edu.stanford.nlp.ling.IndexedWord in project CoreNLP by stanfordnlp.
the class SemanticGraphUtils method killNonRooted.
/**
* Deletes all nodes that are not rooted (such as dangling vertices after a series of
* edges have been chopped).
*/
public static void killNonRooted(SemanticGraph sg) {
List<IndexedWord> nodes = new ArrayList<>(sg.vertexSet());
// Hack: store all of the nodes we know are in the rootset
Set<IndexedWord> guaranteed = Generics.newHashSet();
for (IndexedWord root : sg.getRoots()) {
guaranteed.add(root);
guaranteed.addAll(sg.descendants(root));
}
for (IndexedWord node : nodes) {
if (!guaranteed.contains(node)) {
sg.removeVertex(node);
}
}
}
use of edu.stanford.nlp.ling.IndexedWord in project CoreNLP by stanfordnlp.
the class SemanticGraphUtils method semgrexFromGraph.
/**
* nodeValuesTranformation is a function that converts a vertex (IndexedWord) to the value.
* For an example, see {@code semgrexFromGraph}
* function implementations (if useWord and useTag is true, the value is "{word: vertex.word; tag: vertex.tag}").
* @throws Exception
*/
public static String semgrexFromGraph(SemanticGraph sg, Collection<IndexedWord> wildcardNodes, Map<IndexedWord, String> nodeNameMap, Function<IndexedWord, String> wordTransformation) throws Exception {
IndexedWord patternRoot = sg.getFirstRoot();
StringWriter buf = new StringWriter();
Set<IndexedWord> tabu = Generics.newHashSet();
Set<SemanticGraphEdge> seenEdges = Generics.newHashSet();
buf.append(semgrexFromGraphHelper(patternRoot, sg, tabu, seenEdges, true, true, wildcardNodes, nodeNameMap, false, wordTransformation));
String patternString = buf.toString();
return patternString;
}
use of edu.stanford.nlp.ling.IndexedWord in project CoreNLP by stanfordnlp.
the class SemanticGraph method getVerticesWithoutParents.
/**
* Initially looks for nodes which have no incoming arcs. If there are any, it
* returns a list of them. If not, it looks for nodes from which every other
* node is reachable. If there are any, it returns a list of them. Otherwise,
* it returns an empty list.
*
* @return A list of root nodes or an empty list.
*/
private List<IndexedWord> getVerticesWithoutParents() {
List<IndexedWord> result = new ArrayList<>();
for (IndexedWord v : vertexSet()) {
int inDegree = inDegree(v);
if (inDegree == 0) {
result.add(v);
}
}
Collections.sort(result);
return result;
}
use of edu.stanford.nlp.ling.IndexedWord in project CoreNLP by stanfordnlp.
the class SemanticGraph method toEnUncollapsedSentenceString.
/**
* Similar to <code>toRecoveredString</code>, 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 toEnUncollapsedSentenceString() {
List<IndexedWord> uncompressedList = Generics.newLinkedList(vertexSet());
List<Pair<String, IndexedWord>> specifics = Generics.newArrayList();
// to avoid concurrent modification exceptions.
for (IndexedWord word : vertexSet()) {
for (SemanticGraphEdge edge : getIncomingEdgesSorted(word)) {
GrammaticalRelation relation = edge.getRelation();
// Extract the specific: need to account for possibility that relation
// can
// be a String or GrammaticalRelation (how did it happen this way?)
String specific = relation.getSpecific();
if (specific == null) {
if (edge.getRelation().equals(EnglishGrammaticalRelations.AGENT)) {
specific = "by";
}
}
// this node.
if (specific != null) {
Pair<String, IndexedWord> specPair = new Pair<>(specific, word);
specifics.add(specPair);
}
}
}
for (Pair<String, IndexedWord> tuple : specifics) {
insertSpecificIntoList(tuple.first(), tuple.second(), uncompressedList);
}
return StringUtils.join(uncompressedList, " ");
}
Aggregations