/** * Returns a factory that makes labels of the same type as this one. * May return {@code null} if no appropriate factory is known. * * @return the LabelFactory for this kind of label */ @Override public LabelFactory labelFactory() { Label lab = label(); if (lab == null) { return null; } return lab.labelFactory(); }
/** * Makes a deep copy of not only the Tree structure but of the labels as well. * The new tree will have nodes made by the given TreeFactory. * Each Label is copied using the labelFactory() returned * by the corresponding node's label. * It assumes that your labels give non-null labelFactory. * (Added by Aria Haghighi.) * * @param tf The TreeFactory used to make all nodes in the copied * tree structure * @return A Tree that is a deep copy of the tree structure and * Labels of the original tree. */ public Tree deepCopy(TreeFactory tf) { return deepCopy(tf, label().labelFactory()); }
/** * Returns a deep copy of everything but the leaf labels. The leaf * labels are reused from the original tree. This is useful for * cases such as the dependency converter, which wants to finish * with the same labels in the dependencies as the parse tree. */ public Tree treeSkeletonConstituentCopy() { return treeSkeletonConstituentCopy(treeFactory(), label().labelFactory()); }
private static Label headMark(Label l) { Label l1 = l.labelFactory().newLabel(l); l1.setValue(l1.value() + headMark); return l1; }
@Override public Tree evaluate(Tree tree, TregexMatcher tregex) { return subTree.copy(this, tree.treeFactory(), tree.label().labelFactory()).tree; } }
public Label transformLabel(Tree tree) { if (tree.label() == null) { return null; } return tree.label().labelFactory().newLabel(tree.label()); } }
String tfCl = StringUtils.getShortClassName(t.treeFactory()); String lCl = StringUtils.getShortClassName(t.label()); String lfCl = StringUtils.getShortClassName(t.label().labelFactory()); Set<String> otherClasses = Generics.newHashSet(); String leafLabels = null; String stfCl = StringUtils.getShortClassName(st.treeFactory()); String slCl = StringUtils.getShortClassName(st.label()); String slfCl = StringUtils.getShortClassName(st.label().labelFactory()); if ( ! tCl.equals(stCl)) { otherClasses.add(stCl);
/** * Return a {@code TreeFactory} that produces trees of the * same type as the current {@code Tree}. That is, this * implementation, will produce trees of type * {@code LabeledScoredTree(Node|Leaf)}. * The {@code Label} of {@code this} * is examined, and providing it is not {@code null}, a * {@code LabelFactory} which will produce that kind of * {@code Label} is supplied to the {@code TreeFactory}. * If the {@code Label} is {@code null}, a * {@code StringLabelFactory} will be used. * The factories returned on different calls a different: a new one is * allocated each time. * * @return a factory to produce labeled, scored trees */ @Override public TreeFactory treeFactory() { LabelFactory lf = (label() == null) ? CoreLabel.factory() : label().labelFactory(); return new LabeledScoredTreeFactory(lf); }
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 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; } }
Label label = t.label().labelFactory().newLabel(conjoinedLabels); Tree[] transformedChildren = new Tree[children.length]; for (int childIndex = 0; childIndex < children.length; childIndex++) {
AuxiliaryTree treeCopy = auxTree.copy(this, tree.treeFactory(), tree.label().labelFactory());
if (word instanceof Label) { Label label = (Label) word; Label newLabel = label.labelFactory().newLabel(label); if (newLabel instanceof HasWord) { sentenceB.add((HasWord) newLabel);
@Override public Tree evaluate(Tree tree, TregexMatcher tregex) { // find match Tree targetNode = childMatcher[0].evaluate(tree, tregex); // put children underneath target in foot of auxilary tree AuxiliaryTree ft = adjunctionTree().copy(this, tree.treeFactory(), tree.label().labelFactory()); ft.foot.setChildren(targetNode.getChildrenAsList()); // put children of auxiliary tree under target. root of auxiliary tree is ignored. root of original is maintained. targetNode.setChildren(ft.tree.getChildrenAsList()); return tree; } }
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); } }
@Override public Tree evaluate(Tree tree, TregexMatcher tregex) { // find match and get its parent Tree targetNode = childMatcher[0].evaluate(tree, tregex); Tree parent = targetNode.parent(tree); // put children underneath target in foot of auxilary tree AuxiliaryTree ft = adjunctionTree.copy(this, tree.treeFactory(), tree.label().labelFactory()); ft.foot.setChildren(targetNode.getChildrenAsList()); // replace match with root of auxiliary tree if (parent==null) { return ft.tree; } else { int i = parent.objectIndexOf(targetNode); parent.setChild(i,ft.tree); return tree; } } }
/** * 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; } }
@Override public Tree evaluate(Tree tree, TregexMatcher tregex) { // find match and get its parent Tree targetNode = childMatcher[0].evaluate(tree, tregex); Tree parent = targetNode.parent(tree); // substitute original node for foot of auxiliary tree. Foot node is ignored AuxiliaryTree ft = adjunctionTree().copy(this, tree.treeFactory(), tree.label().labelFactory()); // log.info("ft=" + ft + "; ft.foot=" + ft.foot + "; ft.tree=" + ft.tree); Tree parentOfFoot = ft.foot.parent(ft.tree); if (parentOfFoot == null) { log.info("Warning: adjoin to foot for depth-1 auxiliary tree has no effect."); return tree; } int i = parentOfFoot.objectIndexOf(ft.foot); if (parent==null) { parentOfFoot.setChild(i,targetNode); return ft.tree; } else { int j = parent.objectIndexOf(targetNode); parent.setChild(j,ft.tree); parentOfFoot.setChild(i,targetNode); return tree; } } }