protected Tree findMarkedHead(Tree t) { Tree[] kids = t.children(); for (Tree kid : kids) { if (headMarkedPattern.matcher(kid.label().value()).find() || headMarkedPattern2.matcher(kid.label().value()).find()) { //log.info("found manually-labeled head " + kids[i] + " for tree " + t); return kid; } } return null; }
protected Tree transformTreeHelper(Tree t) { if (t.isLeaf()) { Tree leaf = tf.newLeaf(t.label()); leaf.setScore(t.score()); return leaf; } List<Tree> newChildren = new ArrayList<>(); for (int childNum = 0, numKids = t.numChildren(); childNum < numKids; childNum++) { Tree child = t.getChild(childNum); Tree newChild = transformTreeHelper(child); if ((!newChild.isLeaf()) && newChild.label().value().indexOf('@') >= 0) { newChildren.addAll(newChild.getChildrenAsList()); } else { newChildren.add(newChild); } } Tree node = tf.newTreeNode(t.label(), newChildren); node.setScore(t.score()); return node; }
private List<Tree> spliceOutHelper(Predicate<Tree> nodeFilter, TreeFactory tf) { // recurse over all children first Tree[] kids = children(); List<Tree> l = new ArrayList<>(); for (Tree kid : kids) { l.addAll(kid.spliceOutHelper(nodeFilter, tf)); } // check if this node is being spliced out if (nodeFilter.test(this)) { // no, so add our children and return Tree t; if ( ! l.isEmpty()) { t = tf.newTreeNode(label(), l); } else { t = tf.newLeaf(label()); } l = new ArrayList<>(1); l.add(t); return l; } // we're out, so return our children return l; }
public static List<TreeNodeProxy> create(Tree intree, Tree root) { List<TreeNodeProxy> ret = new ArrayList<>(); if (intree.isLeaf()) { ret.add(new TreeNodeProxy(intree, intree.label().value(), root)); } else for (LabeledWord lword : intree.labeledYield()) { ret.add(new TreeNodeProxy(intree, lword.word(), root)); } return ret; } }
/** * Build a parse tree node corresponding to an elliptic node in the parse XML. */ private Tree buildEllipticNode(Node root) { Element eRoot = (Element) root; String constituentStr = eRoot.getNodeName(); List<Tree> kids = new ArrayList<>(); Tree leafNode = treeFactory.newLeaf(SpanishTreeNormalizer.EMPTY_LEAF_VALUE); if (leafNode.label() instanceof HasWord) ((HasWord) leafNode.label()).setWord(SpanishTreeNormalizer.EMPTY_LEAF_VALUE); kids.add(leafNode); Tree t = treeFactory.newTreeNode(constituentStr, kids); return t; }
/** Divides a sentence into clauses and sort the antecedents for pronoun matching */ private static List<Mention> sortMentionsByClause(List<Mention> l, Mention m1) { List<Mention> sorted = new ArrayList<>(); Tree tree = m1.contextParseTree; Tree current = m1.mentionSubTree; if(tree==null || current==null) return l; while(true){ current = current.ancestor(1, tree); String curLabel = current.label().value(); if("TOP".equals(curLabel) || curLabel.startsWith("S") || curLabel.equals("NP")){ // if(current.label().value().startsWith("S")){ for(Mention m : l){ if(!sorted.contains(m) && current.dominates(m.mentionSubTree)) sorted.add(m); } } if(current.ancestor(1, tree)==null) break; } return sorted; } }
private int numEmbeddedNps(Tree mentionSubTree) { int embeddedNps = 0; for (Tree subtree : mentionSubTree.postOrderNodeList()) { if (!subtree.isLeaf() && !subtree.isPreTerminal()) { String label = ((CoreLabel) subtree.label()) .get(CoreAnnotations.ValueAnnotation.class); if (label.equals("NP")) { embeddedNps++; } } } return embeddedNps; }
/** * Given a tree, set the tags on the leaf nodes if they are not * already set. Do this by using the preterminal's value as a tag. */ public static void setLeafTagsIfUnset(Tree tree) { if (tree.isPreTerminal()) { Tree leaf = tree.children()[0]; if (!(leaf.label() instanceof HasTag)) { return; } HasTag label = (HasTag) leaf.label(); if (label.tag() == null) { label.setTag(tree.value()); } } else { for (Tree child : tree.children()) { setLeafTagsIfUnset(child); } } }
boolean isPunc(Tree t) { if (t.isPreTerminal()) { String s = t.label().value(); if (tlp.isEvalBIgnoredPunctuationTag(s)) { return true; } } return false; }
/** * This method does language-specific tree transformations such as annotating particular nodes with language-relevant * features. Such parameterizations should be inside the specific TreebankLangParserParams class. This method is * recursively applied to each node in the tree (depth first, left-to-right), so you shouldn't write this method to * apply recursively to tree members. This method is allowed to (and in some cases does) destructively change the * input tree <code>t</code>. It changes both labels and the tree shape. * * @param t The input tree (with non-language specific annotation already done, so you need to strip back to basic * categories) * @param root The root of the current tree (can be null for words) * @return The fully annotated tree node (with daughters still as you want them in the final result) */ @Override public Tree transformTree(Tree t, Tree root) { String newCat = t.value() + getAnnotationString(t, root); t.setValue(newCat); if (t.isPreTerminal() && t.label() instanceof HasTag) ((HasTag) t.label()).setTag(newCat); return t; }
/** Doesn't accept nodes that only cover an empty. */ @Override public boolean test(Tree t) { Tree[] kids = t.children(); Label l = t.label(); // Delete (return false for) empty/trace nodes (ones marked '-NONE-') return ! ((l != null) && "-NONE-".equals(l.value()) && !t.isLeaf() && kids.length == 1 && kids[0].isLeaf()); }
Tree tempTree = parser.extractBestParse(label.value(), start, end); boolean isExtra = true; int childEnd = childStart + child.yield().size(); Constituent childConstituent = new LabeledScoredConstituent(childStart, childEnd, child.label(), 0); if (pcfgConstituents.contains(childConstituent)) { isExtra = false; isExtra = false; if (child.isLeaf() || child.isPreTerminal()) { isExtra = false; isExtra = false; if (!label.value().startsWith("NP^NP")) { isExtra = false; log.info("Pruning: " + child.label() + " from " + (childStart + start) + " to " + (childEnd + start)); log.info("Was: " + testTree + " vs " + pcfgTree); prunedChildren.addAll(child.getChildrenAsList()); } else { prunedChildren.add(child);
private static int treeToLatexHelper(Tree t, StringBuilder c, StringBuilder h, int n, int nextN, int indent) { StringBuilder sb = new StringBuilder(); for (int i=0; i<indent; i++) sb.append(" "); h.append('\n').append(sb); h.append("{\\").append(t.isLeaf() ? "" : "n").append("tnode{z").append(n).append("}{").append(t.label()).append('}'); if (!t.isLeaf()) { for (int k=0; k<t.children().length; k++) { h.append(", "); c.append("\\nodeconnect{z").append(n).append("}{z").append(nextN).append("}\n"); nextN = treeToLatexHelper(t.children()[k],c,h,nextN,nextN+1,indent+1); } } h.append('}'); return nextN; }
private String ditrans(Tree t) { int n = 0; for (Tree kid : t.children()) { String childStr = kid.label().value(); if (childStr.startsWith("NP") && !childStr.contains("-TMP")) { n++; } else if (englishTrain.markDitransV == 1 && childStr.startsWith("S")) { n++; } } if (n >= 2) { return "^2Arg"; } else { return ""; } }
public Tree maximalNp(Tree mentionSubTree) { Tree maximalSubtree = mentionSubTree; for (Tree subtree : mentionSubTree.postOrderNodeList()) { if (!subtree.isLeaf() && !subtree.isPreTerminal()) { String label = ((CoreLabel) subtree.label()) .get(CoreAnnotations.ValueAnnotation.class); if (label.equals("NP")) { maximalSubtree = subtree; } } } return maximalSubtree; }