List of usage examples for edu.stanford.nlp.trees Tree isLeaf
public boolean isLeaf()
From source file:CollapseUnaryTransformer.java
License:Apache License
public Tree transformTree(Tree tree) { if (tree.isPreTerminal() || tree.isLeaf()) { return tree.deepCopy(); }/*from ww w.j av a 2s.co m*/ Label label = tree.label().labelFactory().newLabel(tree.label()); Tree[] children = tree.children(); while (children.length == 1 && !children[0].isLeaf()) { children = children[0].children(); } List<Tree> processedChildren = Generics.newArrayList(); for (Tree child : children) { processedChildren.add(transformTree(child)); } return tree.treeFactory().newTreeNode(label, processedChildren); }
From source file:BuildBinarizedDataset.java
/** * Sets all of the labels on a tree to the given default value. */// ww w . j a v a 2 s.com public static void setUnknownLabels(Tree tree, Integer defaultLabel) { if (tree.isLeaf()) { return; } for (Tree child : tree.children()) { setUnknownLabels(child, defaultLabel); } tree.label().setValue(defaultLabel.toString()); }
From source file:BuildBinarizedDataset.java
public static void setPredictedLabels(Tree tree) { if (tree.isLeaf()) { return;// w ww . j a va 2 s . co m } for (Tree child : tree.children()) { setPredictedLabels(child); } tree.label().setValue(Integer.toString(RNNCoreAnnotations.getPredictedClass(tree))); }
From source file:KleinBilingualParser.java
private static HashMap<Tree, Tree> getSampleNodeAlignment(Tree eParseTree, Tree fParseTree) { HashMap<Tree, Tree> alignment = new HashMap<>(); Iterator<Tree> eSubtrees = eParseTree.iterator(); for (Tree fSubTree : fParseTree) { if (!eSubtrees.hasNext()) { break; } else {// w w w .j av a 2 s .c o m Tree eSubTree = eSubtrees.next(); if (!fSubTree.isLeaf() && !eSubTree.isLeaf()) { alignment.put(fSubTree, eSubTree); } } } return alignment; }
From source file:KleinBilingualParser.java
private static HashMap<Tree, Tree> getHungarianAlignment(Tree eParseTree, Tree fParseTree, double[] weights, HashMap<Integer, ArrayList<Integer>> alignMap) { // remember to ignore the top two weights because they are monolingual features int numFrenchNodes = fParseTree.size() - fParseTree.getLeaves().size(); int numEnglishNodes = eParseTree.size() - eParseTree.getLeaves().size(); double[][] costMatrix = new double[numFrenchNodes][numEnglishNodes]; int i, j;//w ww. ja v a 2 s . c om i = 0; for (Tree fSubTree : fParseTree) { if (!fSubTree.isLeaf()) { j = 0; for (Tree eSubTree : eParseTree) { if (!eSubTree.isLeaf()) { //IF IT GETS TOO SLOW DON'T COMPUTE WORD ALIGNMENT FEATURES FOR LARGE SENTENCES costMatrix[i][j] = weights[2] * spanDiff(fSubTree, eSubTree) + weights[3] * numChildren(fSubTree, eSubTree) + weights[7] * bias(fSubTree, eSubTree); if (numFrenchNodes < 50 && numEnglishNodes < 50) { costMatrix[i][j] += weights[4] * insideBoth(fSubTree, eSubTree, alignMap) + weights[5] * insideSrcOutsideTgt(fSubTree, eSubTree, alignMap) + weights[6] * insideTgtOutsideSrc(fSubTree, eSubTree, alignMap); } costMatrix[i][j] = 0 - costMatrix[i][j]; j++; } } i++; } } HungarianAlgorithm hungAlgSolver = new HungarianAlgorithm(costMatrix); int[] assignments = hungAlgSolver.execute(); HashMap<Tree, Tree> alignment = new HashMap<>(); i = 0; for (Tree fSubTree : fParseTree) { if (!fSubTree.isLeaf()) { j = 0; for (Tree eSubTree : eParseTree) { if (!eSubTree.isLeaf()) { if (j == assignments[i]) { alignment.put(fSubTree, eSubTree); } j++; } } i++; } } return alignment; }
From source file:conditionalCFG.ConditionalCFGParser.java
License:Open Source License
public double validateBinarizedTree(Tree tree, int start) { if (tree.isLeaf()) { return 0.0; }/*from www . ja v a 2 s . c om*/ float epsilon = 0.0001f; if (tree.isPreTerminal()) { String wordStr = tree.children()[0].label().value(); int tag = tagIndex.indexOf(tree.label().value()); int word = wordIndex.indexOf(wordStr); IntTaggedWord iTW = new IntTaggedWord(word, tag); float score = lex.score(iTW, start, wordStr, null); float bound = iScore[start][start + 1][stateIndex.indexOf(tree.label().value())]; if (score > bound + epsilon) { System.out.println("Invalid tagging:"); System.out.println(" Tag: " + tree.label().value()); System.out.println(" Word: " + tree.children()[0].label().value()); System.out.println(" Score: " + score); System.out.println(" Bound: " + bound); } return score; } int parent = stateIndex.indexOf(tree.label().value()); int firstChild = stateIndex.indexOf(tree.children()[0].label().value()); if (tree.numChildren() == 1) { UnaryRule ur = new UnaryRule(parent, firstChild); double score = SloppyMath.max(ug.scoreRule(ur), -10000.0) + validateBinarizedTree(tree.children()[0], start); double bound = iScore[start][start + tree.yield().size()][parent]; if (score > bound + epsilon) { System.out.println("Invalid unary:"); System.out.println(" Parent: " + tree.label().value()); System.out.println(" Child: " + tree.children()[0].label().value()); System.out.println(" Start: " + start); System.out.println(" End: " + (start + tree.yield().size())); System.out.println(" Score: " + score); System.out.println(" Bound: " + bound); } return score; } int secondChild = stateIndex.indexOf(tree.children()[1].label().value()); BinaryRule br = new BinaryRule(parent, firstChild, secondChild); double score = SloppyMath.max(bg.scoreRule(br), -10000.0) + validateBinarizedTree(tree.children()[0], start) + validateBinarizedTree(tree.children()[1], start + tree.children()[0].yield().size()); double bound = iScore[start][start + tree.yield().size()][parent]; if (score > bound + epsilon) { System.out.println("Invalid binary:"); System.out.println(" Parent: " + tree.label().value()); System.out.println(" LChild: " + tree.children()[0].label().value()); System.out.println(" RChild: " + tree.children()[1].label().value()); System.out.println(" Start: " + start); System.out.println(" End: " + (start + tree.yield().size())); System.out.println(" Score: " + score); System.out.println(" Bound: " + bound); } return score; }
From source file:conditionalCFG.ConditionalCFGParser.java
License:Open Source License
public double scoreBinarizedTree(Tree tree, int start, int debugLvl) { if (tree.isLeaf()) { return 0.0; }// w w w . j a v a2 s .c o m if (tree.isPreTerminal()) { String wordStr = tree.children()[0].label().value(); int tag = tagIndex.indexOf(tree.label().value()); int word = wordIndex.indexOf(wordStr); IntTaggedWord iTW = new IntTaggedWord(word, tag); // if (lex.score(iTW,(leftmost ? 0 : 1)) == Double.NEGATIVE_INFINITY) { // System.out.println("NO SCORE FOR: "+iTW); // } float score = lex.score(iTW, start, wordStr, null); tree.setScore(score); if (debugLvl > 0) System.out.println(score + " " + tree.getSpan()); return score; } int parent = stateIndex.indexOf(tree.label().value()); int firstChild = stateIndex.indexOf(tree.children()[0].label().value()); if (tree.numChildren() == 1) { UnaryRule ur = new UnaryRule(parent, firstChild); //+ DEBUG // if (ug.scoreRule(ur) < -10000) { // System.out.println("Grammar doesn't have rule: " + ur); // } // return SloppyMath.max(ug.scoreRule(ur), -10000.0) + scoreBinarizedTree(tree.children()[0], leftmost); double score = ug.scoreRule(ur) + scoreBinarizedTree(tree.children()[0], start, debugLvl) + lex.score(ur, start, start + tree.children()[0].yield().size()); tree.setScore(score); if (debugLvl > 0) System.out.println(score + " " + tree.getSpan()); return score; } int secondChild = stateIndex.indexOf(tree.children()[1].label().value()); BinaryRule br = new BinaryRule(parent, firstChild, secondChild); //+ DEBUG // if (bg.scoreRule(br) < -10000) { // System.out.println("Grammar doesn't have rule: " + br); // } // return SloppyMath.max(bg.scoreRule(br), -10000.0) + // scoreBinarizedTree(tree.children()[0], leftmost) + // scoreBinarizedTree(tree.children()[1], false); int sz0 = tree.children()[0].yield().size(); double score = bg.scoreRule(br) + scoreBinarizedTree(tree.children()[0], start, debugLvl) + scoreBinarizedTree(tree.children()[1], start + sz0, debugLvl) + lex.score(br, start, start + sz0 + tree.children()[1].yield().size(), start + sz0); tree.setScore(score); if (debugLvl > 0) System.out.println(score + " " + tree.getSpan() + " " + (sz0 + start)); return score; }
From source file:de.tudarmstadt.ukp.dkpro.core.stanfordnlp.util.TreeUtils.java
License:Open Source License
private static int reIndexLeaves(Tree t, int startIndex) { if (t.isLeaf()) { CoreLabel afl = (CoreLabel) t.label(); afl.setIndex(startIndex);/* w w w .j a v a 2 s .c o m*/ startIndex++; } else { for (Tree child : t.children()) { startIndex = reIndexLeaves(child, startIndex); } } return startIndex; }
From source file:de.tudarmstadt.ukp.dkpro.core.stanfordnlp.util.TreeWithTokens.java
License:Open Source License
private Tree getLeftmostLeaf(Tree t) { if (t.isLeaf()) { return t; } else {//from w w w . java 2s .c o m return getLeftmostLeaf(t.firstChild()); } }
From source file:de.tudarmstadt.ukp.dkpro.core.stanfordnlp.util.TreeWithTokens.java
License:Open Source License
private Tree getRightmostLeaf(Tree t) { if (t.isLeaf()) { return t; } else {// ww w. ja v a 2 s . c o m return getRightmostLeaf(t.lastChild()); } }