List of usage examples for edu.stanford.nlp.trees Tree setValue
@Override public void setValue(String value)
From source file:Ceist.CeistView.java
License:Open Source License
/** * Displays the match results in a table with the matched parts * formatted./*from w ww . j av a 2 s . c om*/ * * @param m the matcher containing the match results * @param matchedTree the tree which was matched * @param showTagged whether to show POS tags or not * @return the HTML to be displayed in the table row */ private String[] getMatcherTableRow(TregexMatcher m, Tree matchedTree, boolean showTagged) { //List<Tree> allMatches = new ArrayList<Tree>(); // Find matches for templates String strQuestion = QuestionTemplate.getQuestionString(m, txtQuestionTemplate.getText()); String strAnswer = AnswerTemplate.getAnswerString(m, txtAnswerTemplate.getText()); // Display the full tree in which the match was found String strMatchAll = "<html>"; String lastRef = ""; for (Tree t : matchedTree.getLeaves()) { String nodeValue = t.nodeString(); if (nodeValue.startsWith("{Q")) { // This is a match for the question string String ref = nodeValue.substring(2, nodeValue.indexOf("}")); nodeValue = nodeValue.substring(nodeValue.indexOf("}") + 1); t.setValue(nodeValue); if (!ref.equals(lastRef)) lastRef = ref; else ref = ""; if (!showTagged) strMatchAll += "<sup>" + ref + "</sup><b><font color=green>" + nodeValue + "</font></b> "; else strMatchAll += "<sup>" + ref + "</sup><b><font color=green>" + nodeValue + "</font><font color=gray>/" + t.parent(matchedTree).nodeString() + "</font></b> "; } else if (nodeValue.startsWith("{A")) { // This is a match for the answer string String ref = nodeValue.substring(2, nodeValue.indexOf("}")); nodeValue = nodeValue.substring(nodeValue.indexOf("}") + 1); t.setValue(nodeValue); if (!ref.equals(lastRef)) lastRef = ref; else ref = ""; if (!showTagged) strMatchAll += "<sup>" + ref + "</sup><b>" + nodeValue + "</b> "; else strMatchAll += "<sup>" + ref + "</sup><b>" + nodeValue + "<font color=gray>/" + t.parent(matchedTree).nodeString() + "</font></b> "; } else { // Normal unmatched text if (!showTagged) strMatchAll += nodeValue + " "; else strMatchAll += nodeValue + "<font color=gray>/" + t.parent(matchedTree).nodeString() + "</font> "; } } strMatchAll += "</html>"; return new String[] { strMatchAll, strQuestion, strAnswer }; }
From source file:edu.nus.comp.nlp.stanford.UtilParser.java
License:Open Source License
private static Tree putOnBranch(TypedDependency dep, Tree tree) { /*/*from w w w . j a v a 2s .c o m*/ * Each node is a tree with a single child */ Tree mySubtree = lstf.newTreeNode(dep.gov().label(), new LinkedList<Tree>(dep.dep())); mySubtree.setValue("[<-" + dep.reln() + "-] " + dep.dep().value());//nudge in the dependency relation information if (tree.children().length == 0) { if (tree.label().value().toString().equals("DUMMYROOT")) { tree.addChild(mySubtree); return tree; } else { //Shouldn't happen System.err.println("Forgot to add a child earlier."); return null; } } else { // System.err.println(dep.dep().label() +"\t[on]\t" + tree.label()); for (Tree child : tree.children()) { //if dep is child's parent, insert dep between child and its parent if (((CoreLabel) child.label()).index() == dep.dep().label().index()) { tree.removeChild(tree.objectIndexOf(child)); mySubtree.addChild(child); } } if (mySubtree.children().length > 1) { tree.addChild(mySubtree); return tree; } for (Tree child : tree.children()) { //if dep is Child's sibling, or child if (((CoreLabel) child.label()).index() == dep.gov().label().index()) { tree.addChild(mySubtree); return tree; } if (child.children().length > 0) { if (putOnBranch(dep, child) != null) { return tree; } } } } // tree.getLeaves() == null //check its childrens, recurisively. return null; }
From source file:Engines.Test.StanfordParser.TreeHandling.java
License:Open Source License
private static Tree putOnBranch(TypedDependency dep, Tree tree) { /*//from w w w .j a v a2s . c om * Each node is a tree with a single child */ Tree mySubtree = lstf.newTreeNode(dep.gov().backingLabel(), new LinkedList<Tree>()); mySubtree.setValue("[<-" + dep.reln() + "-] " + dep.dep().value());//nudge in the dependency relation information if (tree.children().length == 0) { if (tree.label().value().toString().equals("DUMMYROOT")) { tree.addChild(mySubtree); return tree; } else { //Shouldn't happen System.err.println("Forgot to add a child earlier."); return null; } } else { // System.err.println(dep.dep().label() +"\t[on]\t" + tree.label()); for (Tree child : tree.children()) { //if dep is child's parent, insert dep between child and its parent if (((CoreLabel) child.label()).index() == ((CoreLabel) ((Labeled) dep.dep()).label()).index()) { tree.removeChild(tree.objectIndexOf(child)); mySubtree.addChild(child); } } if (mySubtree.children().length > 1) { tree.addChild(mySubtree); return tree; } for (Tree child : tree.children()) { //if dep is Child's sibling, or child if (((CoreLabel) child.label()).index() == ((CoreLabel) ((Labeled) dep.gov()).label()).index()) { tree.addChild(mySubtree); return tree; } if (child.children().length > 0) { if (putOnBranch(dep, child) != null) { return tree; } } } } // tree.getLeaves() == null //check its childrens, recurisively. return null; }
From source file:opennlp.tools.parse_thicket.kernel_interface.TreeExtenderByAnotherLinkedTree.java
License:Apache License
public List<String> buildForestForRSTArcs(ParseThicket pt) { List<String> results = new ArrayList<String>(); for (WordWordInterSentenceRelationArc arc : pt.getArcs()) { // TODO - uncomment // if (!arc.getArcType().getType().startsWith("rst")) // continue; int fromSent = arc.getCodeFrom().getFirst(); int toSent = arc.getCodeTo().getFirst(); String wordFrom = arc.getLemmaFrom(); String wordTo = arc.getLemmaTo(); if (wordFrom == null || wordFrom.length() < 1 || wordTo == null || wordTo.length() < 1) log.severe("Empty lemmas for RST arc " + arc); List<Tree> trees = getASubtreeWithRootAsNodeForWord1(pt.getSentences().get(fromSent), pt.getSentences().get(fromSent), new String[] { wordFrom }); if (trees == null || trees.size() < 1) continue; System.out.println(trees); StringBuilder sb = new StringBuilder(10000); Tree tree = trees.get(0); // instead of phrase type for the root of the tree, we want to put // the RST relation name if (arc.getArcType().getType().startsWith("rst")) tree.setValue(arc.getArcType().getSubtype()); toStringBuilderExtenderByAnotherLinkedTree1(sb, pt.getSentences().get(toSent), tree, new String[] { wordTo }); System.out.println(sb.toString()); results.add(sb.toString());/*from www . j a va 2s . c o m*/ } // if no arcs then orig sentences if (results.isEmpty()) { for (Tree t : pt.getSentences()) { results.add(t.toString()); } } return results; }