use of edu.stanford.nlp.ling.IndexedWord in project CoreNLP by stanfordnlp.
the class DependencyIndexITest method checkTree.
private static void checkTree(Tree tree) {
List<Tree> leaves = tree.getLeaves();
for (Tree leaf : leaves) {
CoreLabel l = null;
if (leaf.label() instanceof CoreLabel)
l = (CoreLabel) leaf.label();
if (l != null) {
// System.err.println(l + " " + l.get(CoreAnnotations.IndexAnnotation.class));
int index = l.get(CoreAnnotations.IndexAnnotation.class);
String text = l.get(CoreAnnotations.TextAnnotation.class);
if (text.equals("Mary"))
assertEquals(1, index);
else if (text.equals("had"))
assertEquals(2, index);
else if (text.equals("a"))
assertEquals(3, index);
else if (text.equals("little"))
assertEquals(4, index);
else if (text.equals("lamb"))
assertEquals(5, index);
else if (text.equals("."))
assertEquals(6, index);
} else {
// System.err.println(leaf + " is not a CoreLabel.");
}
}
TreebankLanguagePack tlp = new PennTreebankLanguagePack();
GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
GrammaticalStructure gs = gsf.newGrammaticalStructure(tree);
Collection<TypedDependency> deps = gs.typedDependenciesCCprocessed(GrammaticalStructure.Extras.MAXIMAL);
// System.out.println(deps);
// collect all nodes in deps
Set<IndexedWord> nodes = Generics.newHashSet();
for (TypedDependency dep : deps) {
nodes.add(dep.gov());
nodes.add(dep.dep());
}
// check the indices for all nodes
for (IndexedWord n : nodes) {
String text = n.value();
int index = n.get(CoreAnnotations.IndexAnnotation.class);
if (text.equals("Mary"))
assertEquals(1, index);
else if (text.equals("had"))
assertEquals(2, index);
else if (text.equals("a"))
assertEquals(3, index);
else if (text.equals("little"))
assertEquals(4, index);
else if (text.equals("lamb"))
assertEquals(5, index);
else if (text.equals("."))
assertEquals(6, index);
}
}
use of edu.stanford.nlp.ling.IndexedWord in project CoreNLP by stanfordnlp.
the class SemanticGraph method toReadableString.
private String toReadableString() {
StringBuilder buf = new StringBuilder();
buf.append(String.format("%-20s%-20s%-20s%n", "dep", "reln", "gov"));
buf.append(String.format("%-20s%-20s%-20s%n", "---", "----", "---"));
for (IndexedWord root : getRoots()) {
buf.append(String.format("%-20s%-20s%-20s%n", root.toString(CoreLabel.OutputFormat.VALUE_TAG_INDEX), "root", "root"));
}
for (SemanticGraphEdge edge : this.edgeListSorted()) {
buf.append(String.format("%-20s%-20s%-20s%n", edge.getTarget().toString(CoreLabel.OutputFormat.VALUE_TAG_INDEX), edge.getRelation().toString(), edge.getSource().toString(CoreLabel.OutputFormat.VALUE_TAG_INDEX)));
}
return buf.toString();
}
use of edu.stanford.nlp.ling.IndexedWord in project CoreNLP by stanfordnlp.
the class SemanticGraph method toList.
/**
* Returns a String representation of this graph as a list of typed
* dependencies, as exemplified by the following:
*
* <pre>
* nsubj(died-6, Sam-3)
* tmod(died-6, today-9)
* </pre>
*
* @return a <code>String</code> representation of this set of typed
* dependencies
*/
public String toList() {
StringBuilder buf = new StringBuilder();
for (IndexedWord root : getRoots()) {
buf.append("root(ROOT-0, ");
buf.append(root.toString(CoreLabel.OutputFormat.VALUE_INDEX)).append(")\n");
}
for (SemanticGraphEdge edge : this.edgeListSorted()) {
buf.append(edge.getRelation().toString()).append("(");
buf.append(edge.getSource().toString(CoreLabel.OutputFormat.VALUE_INDEX)).append(", ");
buf.append(edge.getTarget().toString(CoreLabel.OutputFormat.VALUE_INDEX)).append(")\n");
}
return buf.toString();
}
use of edu.stanford.nlp.ling.IndexedWord in project CoreNLP by stanfordnlp.
the class SemanticGraph method getSiblings.
/**
* Method for getting the siblings of a particular node. Siblings are the
* other children of your parent, where parent is determined as the parent
* returned by getParent
*
* @return collection of sibling nodes (does not include vertex)
* the collection is empty if your parent is null
*/
public Collection<IndexedWord> getSiblings(IndexedWord vertex) {
IndexedWord parent = this.getParent(vertex);
if (parent != null) {
Set<IndexedWord> result = wordMapFactory.newSet();
result.addAll(this.getChildren(parent));
//remove this vertex - you're not your own sibling
result.remove(vertex);
return result;
} else {
return Collections.emptySet();
}
}
use of edu.stanford.nlp.ling.IndexedWord in project CoreNLP by stanfordnlp.
the class SemanticGraph method getPathToRoot.
/**
* Helper function for the public function with the same name.
* <br>
* Builds up the list backwards.
*/
private List<IndexedWord> getPathToRoot(IndexedWord vertex, List<IndexedWord> used) {
used.add(vertex);
// TODO: Apparently the order of the nodes in the path to the root
// makes a difference for the RTE system. Look into this some more
List<IndexedWord> parents = getParentList(vertex);
// Set<IndexedWord> parents = wordMapFactory.newSet();
// parents.addAll(getParents(vertex));
parents.removeAll(used);
if (roots.contains(vertex) || (parents.isEmpty())) {
used.remove(used.size() - 1);
if (roots.contains(vertex))
return Generics.newArrayList();
else
// no path found
return null;
}
for (IndexedWord parent : parents) {
List<IndexedWord> path = getPathToRoot(parent, used);
if (path != null) {
path.add(parent);
used.remove(used.size() - 1);
return path;
}
}
used.remove(used.size() - 1);
return null;
}
Aggregations