public boolean test(Tree t) { return ! (t.isPreTerminal() && t.value().equals("-NONE-")); } }
/** * Return whether all the children of this node are preterminals or not. * A preterminal is * defined to be a node with one child which is itself a leaf. * Considered false if the node has no children * * @return true if the node is a prepreterminal; false otherwise */ public boolean isPrePreTerminal() { Tree[] kids = children(); if (kids.length == 0) { return false; } for (Tree kid : kids) { if ( ! kid.isPreTerminal()) { return false; } } return true; }
static void preTermHelper(Tree t, List<Tree> l) { if (t.isLeaf()) { return; } if (t.isPreTerminal()) { l.add(t); return; } Tree[] children = t.children(); for (Tree child : children) { preTermHelper(child, l); } }
/** * 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; }
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; }
public String apply(TregexMatcher m) { if(mwCounter == null) throw new RuntimeException("Cannot enable POSSequence features without POS sequence map. Use option -frenchMWMap."); Tree t = m.getMatch(); StringBuilder sb = new StringBuilder(); for(Tree kid : t.children()) { if( ! kid.isPreTerminal()) throw new RuntimeException("Not POS sequence for tree: " + t.toString()); String tag = doBasicCat ? tlp.basicCategory(kid.value()) : kid.value(); sb.append(tag).append(" "); } if(mwCounter.getCount(t.value(), sb.toString().trim()) > cutoff) return annotationMark + sb.toString().replaceAll("\\s+", "").toLowerCase(); else return ""; }
/** * Gets the preterminal yield (i.e., tags) of the tree. All data in * preleaf nodes is returned as a list ordered by the natural left to * right order of the tree. Null values, if any, are inserted into the * list like any other value. Pre-leaves are nodes of height 1. * * @param y The list in which the preterminals of the tree will be * placed. Normally, this will be empty when the routine is called, * but if not, the new yield is added to the end of the list. * @return a {@code List} of the data in the tree's pre-leaves. */ public List<Label> preTerminalYield(List<Label> y) { if (isPreTerminal()) { y.add(label()); } else { Tree[] kids = children(); for (Tree kid : kids) { kid.preTerminalYield(y); } } return y; }
public List<LabeledWord> labeledYield(List<LabeledWord> ty) { if (isPreTerminal()) { ty.add(new LabeledWord(firstChild().label(), label())); } else { for (Tree kid : children()) { kid.labeledYield(ty); } } return ty; }
private static Tree getTreeNonTerminal(Tree root, int startToken, int endToken, boolean acceptPreTerminals) { Tree t = getLowestCommonAncestor(root, startToken, endToken); if (t.isLeaf()) { t = t.parent(root); } if (!acceptPreTerminals && t.isPreTerminal()) { t = t.parent(root); } return t; }
boolean isPunc(Tree t) { if (t.isPreTerminal()) { String s = t.label().value(); if (tlp.isEvalBIgnoredPunctuationTag(s)) { return true; } } return false; }
public void visitTree(Tree t) { if(t == null || t.value().equals("X")) return; if(t.yield().size() > maxLen) return; if(node.isPreTerminal()) { processPreterminal(node); if(removeDashTags && !node.isLeaf()) node.setValue(tlp.basicCategory(node.value())); if (addRoot && t.value() != null && !t.value().equals("ROOT")) { t = tf.newTreeNode("ROOT", Collections.singletonList(t));
/** Doesn't accept nodes that are A over A nodes (perhaps due to * empty removal or are EDITED nodes). * * Also removes all w nodes. */ public boolean test(Tree t) { if(t.value() != null && t.value().equals("w")) return false; if (t.isLeaf() || t.isPreTerminal()) return true; return ! (t.label() != null && t.label().value() != null && t.label().value().equals(t.getChild(0).label().value())); } }
public void recurseOutside(Tree tree, IdentityHashMap<Tree, double[]> probIn, IdentityHashMap<Tree, double[]> probOut) { if (tree.isLeaf() || tree.isPreTerminal()) { return; } if (tree.children().length == 1) { recountOutside(tree.children()[0], tree, probIn, probOut); } else { // length == 2 recountOutside(tree.children()[0], tree.children()[1], tree, probIn, probOut); } }
private boolean shouldSkip(Tree t, boolean origWasInterjection) { return t.isPreTerminal() && (tlp.isPunctuationTag(t.value()) || ! origWasInterjection && "UH".equals(t.value())) || "INTJ".equals(t.value()) && ! origWasInterjection; }
@Override public Tree transformTree(Tree t, Tree root) { // Perform tregex-powered annotations t = super.transformTree(t, root); String cat = t.value(); //Add morphosyntactic features if this is a POS tag if(t.isPreTerminal() && tagSpec != null) { if( !(t.firstChild().label() instanceof CoreLabel) || ((CoreLabel) t.firstChild().label()).originalText() == null ) throw new RuntimeException(String.format("%s: Term lacks morpho analysis: %s",this.getClass().getName(),t.toString())); String morphoStr = ((CoreLabel) t.firstChild().label()).originalText(); Pair<String,String> lemmaMorph = MorphoFeatureSpecification.splitMorphString("", morphoStr); MorphoFeatures feats = tagSpec.strToFeatures(lemmaMorph.second()); cat = feats.getTag(cat); } //Update the label(s) t.setValue(cat); if (t.isPreTerminal() && t.label() instanceof HasTag) ((HasTag) t.label()).setTag(cat); return t; }
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; }
public static void traverseAndFix(Tree t, Tree parent, TwoDimensionalCounter<String, String> unigramTagger, boolean retainNER) { if(t.isPreTerminal()) { if(t.value().equals(SpanishTreeNormalizer.MW_TAG)) { nMissingPOS++; String pos = inferPOS(t, parent, unigramTagger); if (pos != null) { t.setValue(pos); nFixedPOS++; } } return; } for(Tree kid : t.children()) traverseAndFix(kid, t, unigramTagger, retainNER); // Post-order visit if(t.value().startsWith(SpanishTreeNormalizer.MW_PHRASE_TAG)) { nMissingPhrasal++; String phrasalCat = inferPhrasalCategory(t, retainNER); if (phrasalCat != null) { t.setValue(phrasalCat); nFixedPhrasal++; } } }
private static void taggedLeafLabels(Tree t, List<CoreLabel> l) { if (t.isPreTerminal()) { CoreLabel fl = (CoreLabel)t.getChild(0).label(); fl.set(CoreAnnotations.TagLabelAnnotation.class, t.label()); l.add(fl); } else { for (Tree kid : t.children()) { taggedLeafLabels(kid, l); } } }
private static Tree getTreeNonTerminal(Tree root, int startToken, int endToken, boolean acceptPreTerminals) { Tree t = getLowestCommonAncestor(root, startToken, endToken); if (t.isLeaf()) { t = t.parent(root); } if (!acceptPreTerminals && t.isPreTerminal()) { t = t.parent(root); } return t; }