/** * Create a new {@code TreeGraphNode} with the supplied * label. * * @param label the label for this node. */ public TreeGraphNode(Label label) { this.label = (CoreLabel) mlf.newLabel(label); }
/** * Create a new leaf node with the given label * * @param label the label for the leaf node * @return A new tree leaf */ @Override public Tree newLeaf(Label label) { return new LabeledScoredTreeNode(lf.newLabel(label)); }
/** * Create a new non-leaf tree node with the given label * * @param parentLabel The label for the node * @param children A <code>List</code> of the children of this node, * each of which should itself be a <code>LabeledScoredTree</code> * @return A new internal tree node */ @Override public Tree newTreeNode(Label parentLabel, List<Tree> children) { return new LabeledScoredTreeNode(lf.newLabel(parentLabel), children); } }
@Override public Tree newTreeNode(final String parent, final List<Tree> children) { return new LabeledScoredTreeNode(lf.newLabel(parent), children); }
/** {@inheritDoc} */ @Override public Tree newLeaf(final String word) { return newLeaf(mlf.newLabel(word)); }
/** {@inheritDoc} */ @Override public Tree newTreeNode(final String parent, final List<Tree> children) { return newTreeNode(mlf.newLabel(parent), children); }
@Override public Tree newLeaf(final String word) { return new LabeledScoredTreeNode(lf.newLabel(word)); }
@Override public void setLabel(Label label) { if (label instanceof CoreLabel) { this.setLabel((CoreLabel) label); } else { this.setLabel((CoreLabel) mlf.newLabel(label)); } }
private static Label headMark(Label l) { Label l1 = l.labelFactory().newLabel(l); l1.setValue(l1.value() + headMark); return l1; }
public Label transformLabel(Tree tree) { if (tree.label() == null) { return null; } return tree.label().labelFactory().newLabel(tree.label()); } }
/** Turns a sentence into a flat phrasal tree. * The structure is S -> tag*. And then each tag goes to a word. * The tag is either found from the label or made "WD". * The tag and phrasal node have a StringLabel. * * @param s The Sentence to make the Tree from * @param lf The LabelFactory with which to create the new Tree labels * @return The one phrasal level Tree */ public static Tree toFlatTree(List<? extends HasWord> s, LabelFactory lf) { List<Tree> daughters = new ArrayList<>(s.size()); for (HasWord word : s) { Tree wordNode = new LabeledScoredTreeNode(lf.newLabel(word.word())); if (word instanceof TaggedWord) { TaggedWord taggedWord = (TaggedWord) word; wordNode = new LabeledScoredTreeNode(new StringLabel(taggedWord.tag()), Collections.singletonList(wordNode)); } else { wordNode = new LabeledScoredTreeNode(lf.newLabel("WD"), Collections.singletonList(wordNode)); } daughters.add(wordNode); } return new LabeledScoredTreeNode(new StringLabel("S"), daughters); }
private Pair<Tree,Tree> copyHelper(Tree node, Map<String,Tree> newNamesToNodes, TreeFactory treeFactory, LabelFactory labelFactory) { Tree clone; Tree newFoot = null; if (node.isLeaf()) { if (node == foot) { // found the foot node; pass it up. clone = treeFactory.newTreeNode(node.label(), new ArrayList<>(0)); newFoot = clone; } else { clone = treeFactory.newLeaf(labelFactory.newLabel(node.label())); } } else { List<Tree> newChildren = new ArrayList<>(node.children().length); for (Tree child : node.children()) { Pair<Tree,Tree> newChild = copyHelper(child, newNamesToNodes, treeFactory, labelFactory); newChildren.add(newChild.first()); if (newChild.second() != null) { if (newFoot != null) { log.info("Error -- two feet found when copying auxiliary tree " + tree.toString() + "; using last foot found."); } newFoot = newChild.second(); } } clone = treeFactory.newTreeNode(labelFactory.newLabel(node.label()),newChildren); } if (nodesToNames.containsKey(node)) newNamesToNodes.put(nodesToNames.get(node),clone); return new Pair<>(clone, newFoot); }
private void insertNPinPP(Tree t) { if (tlp.basicCategory(t.label().value()).equals("PP")) { Tree[] kids = t.children(); int i = 0; int j = kids.length - 1; while (i < j && prepositionTags.contains(tlp.basicCategory(kids[i].label().value()))) { i++; } // i now indexes first dtr of new NP while (i < j && postpositionTags.contains(tlp.basicCategory(kids[j].label().value()))) { j--; } // j now indexes last dtr of new NP if (i > j) { log.info("##### Warning -- no NP material here!"); return; // there is no NP material! } int npKidsLength = j - i + 1; Tree[] npKids = new Tree[npKidsLength]; System.arraycopy(kids, i, npKids, 0, npKidsLength); Tree np = t.treeFactory().newTreeNode(t.label().labelFactory().newLabel("NP"), Arrays.asList(npKids)); Tree[] newPPkids = new Tree[kids.length - npKidsLength + 1]; System.arraycopy(kids, 0, newPPkids, 0, i + 1); newPPkids[i] = np; System.arraycopy(kids, j + 1, newPPkids, i + 1, kids.length - j - 1); t.setChildren(newPPkids); System.out.println("#### inserted NP in PP"); t.pennPrint(); } }
@Override public Label transformNonterminalLabel(Tree tree) { if (tree.label() == null) { return null; } return tree.label().labelFactory().newLabel(tlp.basicCategory(tree.label().value())); }
public Tree treeSkeletonConstituentCopy(TreeFactory tf, LabelFactory lf) { if (isLeaf()) { // Reuse the current label for a leaf. This way, trees which // are based on tokens in a sentence can have the same tokens // even after a "deep copy". // TODO: the LabeledScoredTreeFactory copies the label for a new // leaf. Perhaps we could add a newLeafNoCopy or something like // that for efficiency. Tree newLeaf = tf.newLeaf(label()); newLeaf.setLabel(label()); return newLeaf; } Label label = lf.newLabel(label()); Tree[] kids = children(); List<Tree> newKids = new ArrayList<>(kids.length); for (Tree kid : kids) { newKids.add(kid.treeSkeletonConstituentCopy(tf, lf)); } return tf.newTreeNode(label, newKids); }
public Tree transformTree(Tree t) { if (t.isLeaf()) { return t.treeFactory().newLeaf(t.label()); } Tree[] children = t.children(); Tree[] transformedChildren = new Tree[children.length]; for (int childIndex = 0; childIndex < children.length; childIndex++) { Tree child = children[childIndex]; transformedChildren[childIndex] = transformTree(child); } Label label = t.label(); if (!label.value().startsWith("&")) { return t.treeFactory().newTreeNode(label, Arrays.asList(transformedChildren)); } String[] nodeStrings = label.value().split("&"); int i = nodeStrings.length - 1; label = t.label().labelFactory().newLabel(nodeStrings[i]); Tree result = t.treeFactory().newTreeNode(label, Arrays.asList(transformedChildren)); while (i > 1) { i--; label = t.label().labelFactory().newLabel(nodeStrings[i]); result = t.treeFactory().newTreeNode(label, Collections.singletonList(result)); } return result; } }
/** * Create a new {@code TreeGraphNode} having the same tree * structure and label values as an existing tree (but no shared * storage). Operates recursively to construct an entire * subtree. * * @param t the tree to copy * @param parent the parent node */ protected TreeGraphNode(Tree t, TreeGraphNode parent) { this.parent = parent; Tree[] tKids = t.children(); int numKids = tKids.length; children = new TreeGraphNode[numKids]; for (int i = 0; i < numKids; i++) { children[i] = new TreeGraphNode(tKids[i], this); if (t.isPreTerminal()) { // add the tags to the leaves children[i].label.setTag(t.label().value()); } } this.label = (CoreLabel) mlf.newLabel(t.label()); }
public Tree transformTree(Tree tree) { if (tree.isPreTerminal() || tree.isLeaf()) { return tree.deepCopy(); } 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); } }
/** * Convert a constituency label to a dependency label. Options are provided for selecting annotations * to copy. * * @param oldLabel * @param copyLabel * @param copyIndex * @param copyPosTag */ private static Label makeDependencyLabel(Label oldLabel, boolean copyLabel, boolean copyIndex, boolean copyPosTag) { if ( ! copyLabel) return oldLabel; String wordForm = (oldLabel instanceof HasWord) ? ((HasWord) oldLabel).word() : oldLabel.value(); Label newLabel = oldLabel.labelFactory().newLabel(wordForm); if (newLabel instanceof HasWord) ((HasWord) newLabel).setWord(wordForm); if (copyPosTag && newLabel instanceof HasTag && oldLabel instanceof HasTag) { String tag = ((HasTag) oldLabel).tag(); ((HasTag) newLabel).setTag(tag); } if (copyIndex && newLabel instanceof HasIndex && oldLabel instanceof HasIndex) { int index = ((HasIndex) oldLabel).index(); ((HasIndex) newLabel).setIndex(index); } return newLabel; }
private static int markStrahler(Tree t) { if (t.isLeaf()) { // don't annotate the words at leaves! return 1; } else { String cat = t.label().value(); int maxStrahler = -1; int maxMultiplicity = 0; for (int i = 0; i < t.numChildren(); i++) { int strahler = markStrahler(t.getChild(i)); if (strahler > maxStrahler) { maxStrahler = strahler; maxMultiplicity = 1; } else if (strahler == maxStrahler) { maxMultiplicity++; } } if (maxMultiplicity > 1) { maxStrahler++; // this is the one case where it grows } cat = cat + '~' + maxStrahler; Label label = t.label().labelFactory().newLabel(t.label()); label.setValue(cat); t.setLabel(label); return maxStrahler; } }