public static DEPTree buildFrom(IDEPTreeDatum treeDatum) { List<IDEPNodeDatum> nodeData = treeDatum.getDEPNodeData(); DEPTree tree = new DEPTree(); int i, size = nodeData.size(); IDEPNodeDatum nd; DEPNode node; for (i = 0; i < size; i++) { nd = nodeData.get(i); tree.add( new DEPNode( nd.getID(), nd.getForm(), nd.getLemma(), nd.getPOS(), nd.getNamedEntity(), new DEPFeat(nd.getFeats()))); } for (i = 0; i < size; i++) { nd = nodeData.get(i); node = tree.get(i + 1); node.initSHeads(); node.setHead(new DEPArc(tree, nd.getSyntacticHead())); node.addSHeads(DEPLib.getSRLArcs(tree, nd.getSemanticHeads())); } tree.resetDependents(); return tree; }
public String getSentenceFromPA(int verbId, Pattern labels, String delim) { List<DEPNode> subs = new ArrayList<DEPNode>(); StringBuilder build = new StringBuilder(); DEPNode node, verb = get(verbId); int i, size = size(); subs.add(verb); for (i = 1; i < size; i++) { if (i != verbId) { node = get(i); if (node.isArgumentOf(verb, labels)) subs.addAll(node.getSubNodeSet()); } } Collections.sort(subs); for (DEPNode sub : subs) { build.append(delim); build.append(sub.form); } return build.substring(delim.length()); }
public void projectivize() { IntArrayList ids = new IntArrayList(); int i, size = size(); DEPNode nonProj; for (i = 1; i < size; i++) ids.add(i); while ((nonProj = getSmallestNonProjectiveArc(ids)) != null) nonProj.setHead(nonProj.getHead().getHead(), DEPLib.DEP_NON_PROJ); }
/** * Returns {@code true} if this tree contains a cycle. * * @return {@code true} if this tree contains a cycle. */ public boolean containsCycle() { int i, size = size(); DEPNode node; for (i = 1; i < size; i++) { node = get(i); if (node.getHead().isDescendentOf(node)) return true; } return false; }
public DEPNode getNextPredicate(int prevId) { int i, size = size(); DEPNode pred; for (i = prevId + 1; i < size; i++) { pred = get(i); if (pred.getFeat(DEPLib.FEAT_PB) != null) return pred; } return null; }
public DEPNode getRightMostDependent(int id) { DEPNode node, head = get(id); int i; for (i = size() - 1; i > id; i--) { node = get(i); if (node.getHead() == head) return node; } return null; }
public DEPNode getLeftMostDependent(int id) { DEPNode node, head = get(id); int i; for (i = 1; i < id; i++) { node = get(i); if (node.getHead() == head) return node; } return null; }
public DEPNode getFirstRoot() { DEPNode node, root = get(0); int i, size = size(); for (i = 1; i < size; i++) { node = get(i); if (node.getHead() == root) return node; } return null; }
/** @return a list of root nodes in this tree. */ public List<DEPNode> getRoots() { List<DEPNode> roots = new ArrayList<DEPNode>(); DEPNode node, root = get(0); int i, size = size(); for (i = 1; i < size; i++) { node = get(i); if (node.getHead() == root) roots.add(node); } return roots; }
/** Resets dependents of each node in this tree. */ public void resetDependents() { int i, size = size(); DEPNode node, head; for (i = 0; i < size; i++) get(i).l_dependents = new ArrayList<DEPArc>(); for (i = 1; i < size; i++) { node = get(i); head = node.getHead(); if (head != null) head.addDependent(node, node.getLabel()); } }
public DEPNode getRightNearestSibling(int id) { DEPNode node, head = get(id).getHead(); if (head == null) return null; int i, eIdx = (id < head.id) ? head.id : size(); for (i = id + 1; i < eIdx; i++) { node = get(i); if (node.getHead() == head) return node; } return null; }
public DEPNode getLeftNearestSibling(int id) { DEPNode node, head = get(id).getHead(); if (head == null) return null; int i, eIdx = (head.id < id) ? head.id : 0; for (i = id - 1; i > eIdx; i--) { node = get(i); if (node.getHead() == head) return node; } return null; }
public IDEPTreeDatum getDEPTreeDatum( IDEPTreeDatumFactory treeFactory, IDEPNodeDatumFactory nodeFactory) { IDEPTreeDatum datum = treeFactory.createDEPTreeDatum(); List<IDEPNodeDatum> nodeData = Lists.newArrayList(); int i, size = size(); DEPNode node; for (i = 1; i < size; i++) { node = get(i); nodeData.add(node.getDEPNodeDatum(nodeFactory)); } datum.setDEPNodeData(nodeData); return datum; }
public DEPNode getLeftMostDependent(int id, int order) { DEPNode node, head = get(id); int i; for (i = 1; i < id; i++) { node = get(i); if (node.getHead() == head) { if (order == 0) return node; order--; } } return null; }
public void resetHeads(StringIntPair[] heads) { int i, size = size(), len = heads.length; StringIntPair head; DEPNode node; for (i = 1; i < len && i < size; i++) { node = get(i); head = heads[i]; if (head.i == DEPLib.NULL_ID) node.clearHead(); else node.setHead(get(head.i), head.s); } for (i = len; i < size; i++) get(i).clearHead(); }
public DEPTree clone() { IntObjectOpenHashMap<DEPNode> map = new IntObjectOpenHashMap<DEPNode>(); DEPNode oNode, nNode, oHead, nHead; DEPTree tree = new DEPTree(); int i, size = size(); for (i = 1; i < size; i++) { oNode = get(i); nNode = new DEPNode(oNode); tree.add(nNode); nNode.id = i; map.put(oNode.id, nNode); if (oNode.x_heads != null) nNode.initXHeads(); if (oNode.s_heads != null) nNode.initSHeads(); } for (i = 1; i < size; i++) { oNode = get(i); nNode = tree.get(i); oHead = oNode.getHead(); nHead = map.get(oHead.id); if (nHead == null) { nHead = tree.get(0); } else { if (oNode.x_heads != null) { for (DEPArc xHead : oNode.x_heads) { oHead = xHead.getNode(); nNode.addXHead(map.get(oHead.id), xHead.getLabel()); } } if (oNode.s_heads != null) { for (DEPArc sHead : oNode.s_heads) { oHead = sHead.getNode(); nNode.addSHead(map.get(oHead.id), sHead.getLabel()); } } } nNode.setHead(nHead, oNode.getLabel()); } return tree; }
public SRLTree getSRLTree(DEPNode pred) { if (pred.getFeat(DEPLib.FEAT_PB) == null) return null; SRLTree tree = new SRLTree(pred); int i, size = size(); DEPNode node; String label; for (i = 1; i < size; i++) { node = get(i); label = node.getSLabel(pred); if (label != null) tree.addArgument(node, label); } return tree; }
public List<List<DEPArc>> getArgumentList() { int i, size = size(); List<DEPArc> args; DEPNode node; List<List<DEPArc>> list = new ArrayList<List<DEPArc>>(); for (i = 0; i < size; i++) list.add(new ArrayList<DEPArc>()); for (i = 1; i < size; i++) { node = get(i); for (DEPArc arc : node.getSHeads()) { args = list.get(arc.getNode().id); args.add(new DEPArc(node, arc.getLabel())); } } return list; }
public StringIntPair[] getDiff(StringIntPair[] heads) { int i, size = size(); DEPNode node, head; StringIntPair[] diff = new StringIntPair[size]; StringIntPair p; for (i = 1; i < size; i++) { node = get(i); head = node.getHead(); p = heads[i]; if (head != null && head.id != p.i && !node.isLabel(p.s)) diff[i] = new StringIntPair(node.getLabel(), head.id); else diff[i] = new StringIntPair(null, DEPLib.NULL_ID); } return diff; }
@Deprecated public IntOpenHashSet getNonProjectiveSet() { IntObjectOpenHashMap<IntOpenHashSet> map = new IntObjectOpenHashMap<IntOpenHashSet>(); int i, j, bIdx, eIdx, size = size(); DEPNode curr, head, dep; for (i = 1; i < size; i++) { curr = get(i); head = curr.getHead(); if (curr.id < head.id) { bIdx = curr.id; eIdx = head.id; } else { bIdx = head.id; eIdx = curr.id; } for (j = bIdx + 1; j < eIdx; j++) { curr = get(j); head = curr.getHead(); if (head.id < bIdx || head.id > eIdx) { addNonProjectiveMap(map, i, j); addNonProjectiveMap(map, j, i); } for (DEPArc arc : curr.getDependents()) { dep = arc.getNode(); if (dep.id < bIdx || dep.id > eIdx) { addNonProjectiveMap(map, i, dep.id); addNonProjectiveMap(map, dep.id, i); } } } } return getNonProjectiveMapAux(map); }
/** @return > 0 if w_k is non-projective. */ public int isNonProjective(DEPNode wk) { DEPNode wi = wk.getHead(); if (wi == null) return 0; DEPNode wj; int bId, eId, j; if (wk.id < wi.id) { bId = wk.id; eId = wi.id; } else { bId = wi.id; eId = wk.id; } for (j = bId + 1; j < eId; j++) { wj = get(j); if (!wj.isDescendentOf(wi)) return Math.abs(wi.id - wk.id); } return 0; }
public void clearPredicates() { for (DEPNode node : this) node.removeFeat(DEPLib.FEAT_PB); }
public void clearPOSTags() { for (DEPNode node : this) node.pos = null; }
/** Constructs a dependency tree. An artificial root node gets inserted automatically. */ public DEPTree() { DEPNode root = new DEPNode(); root.initRoot(); add(root); }