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, " ");
}
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;
}
Aggregations