Example usage for edu.stanford.nlp.trees Tree parent

List of usage examples for edu.stanford.nlp.trees Tree parent

Introduction

In this page you can find the example usage for edu.stanford.nlp.trees Tree parent.

Prototype

public Tree parent(Tree root) 

Source Link

Document

Return the parent of the tree node.

Usage

From source file:pltag.util.Utils.java

License:Open Source License

public static Tree getMinimalConnectedStructure(Tree tree, Tree firstLeaf, Tree lastLeaf, int lastLeafIndex) {
    // find common ancestor node by traversing the tree bottom-up from the last leaf and up
    Tree commonAncestorNode = lastLeaf.parent(tree);
    while (!commonAncestorNode.getLeaves().get(0).equals(firstLeaf)) {
        commonAncestorNode = commonAncestorNode.parent(tree);
    }//from   w  w  w .ja v a 2 s.co  m
    // found the common ancestor, now we need to clone the tree and chop the children non-terminals the span of which is outwith the last leaf
    Tree result = commonAncestorNode.deepCopy();
    List<Tree> leaves = result.getLeaves();
    lastLeaf = leaves.get(lastLeafIndex);
    Tree p = lastLeaf.parent(result);
    Tree d = lastLeaf;
    while (p != null) {
        if (p.numChildren() > 1) {
            // remove siblings to the right of d
            int index = indexOfChild(p, d.nodeNumber(result), result);
            pruneChildrenAfter(p, index);
        }
        d = p;
        p = p.parent(result);
    }
    return result;
}

From source file:pltag.util.Utils.java

License:Open Source License

public static String removeSubtreesAfterWord(String inputTree, int numOfLeaves) {
    Tree tree = Tree.valueOf(inputTree);
    List<Tree> leaves = tree.getLeaves();
    if (leaves.size() > numOfLeaves) {
        // find common ancestor between last valid leaf and extraneous leaf
        Tree firstLeaf = leaves.get(numOfLeaves - 1);
        Tree lastLeaf = leaves.get(leaves.size() - 1);
        Tree commonAncestorNode = lastLeaf.parent(tree);
        while (!commonAncestorNode.getLeaves().contains(firstLeaf)) {
            commonAncestorNode = commonAncestorNode.parent(tree);
        }/*from   w w  w .ja va2 s  .co  m*/
        // found the common ancestor, now we need to chop the children nodes the span of which is outwith the last valid leaf

        Tree p = lastLeaf.parent(tree);
        while (p != commonAncestorNode) {
            int numOfChildren = p.numChildren();
            for (int i = 0; i < numOfChildren; i++)
                p.removeChild(0);
            p = p.parent(tree);
        }
        // remove last leftover parent node of the invalid leaf
        commonAncestorNode.removeChild(commonAncestorNode.numChildren() - 1);
        return tree.toString();
    } else {
        return inputTree;
    }

}

From source file:qmul.util.parse.CreateTreeFromClarkCurranCCGProlog.java

License:Open Source License

/**
 * @param reader//from   w w  w.  ja  va 2 s.  c  o m
 *            a {@link BufferedReader}
 * @return the Stanford {@link Tree}
 */
public static Tree makeTree(BufferedReader reader) {
    if (options == null) {
        setDefaultOptions();
    }

    NodeFilter nodeFilter = new NodeFilter();
    String line = null;
    boolean doingTree = false;
    boolean doingWords = false;
    HashMap<Integer, Tree> leaves = new HashMap<Integer, Tree>();
    Tree currentNode = null;
    Tree rootNode = null;
    int treeLevel = 0;
    try {
        while ((line = reader.readLine()) != null) {
            line = line.trim();
            // first we need to get the ccg/2 tree structure
            if (line.startsWith("ccg(")) {
                doingTree = true;
                doingWords = false;
                treeLevel = 1;
                // nothing useful on the actual ccg functor line
                continue;
            }
            // next the w/8 word definitions
            if (line.startsWith("w(")) {
                if (!doingTree && !doingWords) {
                    // if we've hit the word definitions without seeing a tree, stop
                    return null;
                }
                doingTree = false;
                doingWords = true;
            }
            if (doingTree) {
                Matcher m = LEAF_PAT.matcher(line);
                if (m.find()) {
                    // System.out.println("matched leaf " + line);
                    Tree nonTerminal = tf.newTreeNode(getSynLabel(m.group(3)), new ArrayList<Tree>());
                    if (rootNode == null) {
                        rootNode = nonTerminal;
                    } else {
                        currentNode.addChild(nonTerminal);
                    }
                    Tree leaf = tf.newLeaf("DUMMY");
                    nonTerminal.addChild(leaf);
                    leaves.put(Integer.parseInt(m.group(2)), leaf);
                    // adjust currentNode
                    int numOpening = line.replaceAll("[^(]", "").length();
                    int numClosing = line.replaceAll("\\)\\.$", "").replaceAll("[^)]", "").length();
                    int levelChange = numOpening - numClosing;
                    if (levelChange > 0) {
                        throw new RuntimeException("deepening with leaf node!");
                    } else if (levelChange < 0) {
                        do {
                            // System.out.println("cu node " + currentNode.label());
                            currentNode = currentNode.parent(rootNode);
                            // System.out.println("up node " + (currentNode == null ? null : currentNode.label()));
                            treeLevel--;
                            levelChange++;
                        } while (levelChange < 0);
                    }
                    continue;
                }
                m = RULE_PAT.matcher(line);
                if (m.find()) {
                    // System.out.println("matched rule " + line);
                    treeLevel++;
                    Tree node = tf.newTreeNode(getSynLabel(m.group(2)), new ArrayList<Tree>());
                    if (rootNode == null) {
                        rootNode = node;
                    }
                    if (currentNode != null) {
                        currentNode.addChild(node);
                    }
                    currentNode = node;
                    // System.out.println("current node " + node.label());
                    continue;
                }
                m = LEXR_PAT.matcher(line);
                if (m.find()) {
                    // System.out.println("matched lexr " + line);
                    treeLevel++;
                    Tree node = tf.newTreeNode(getSynLabel(m.group(3)), new ArrayList<Tree>());
                    if (rootNode == null) {
                        rootNode = node;
                    }
                    if (currentNode != null) {
                        currentNode.addChild(node);
                    }
                    currentNode = node;
                    // System.out.println("current node " + node.label());
                    continue;
                }
                m = CONJ_PAT.matcher(line);
                if (m.find()) {
                    // System.out.println("matched conj " + line);
                    treeLevel++;
                    Tree node = tf.newTreeNode(getSynLabel(m.group(4)), new ArrayList<Tree>());
                    if (rootNode == null) {
                        rootNode = node;
                    }
                    if (currentNode != null) {
                        currentNode.addChild(node);
                    }
                    currentNode = node;
                    // System.out.println("current node " + node.label());
                    continue;
                }
                throw new RuntimeException("no match for line " + line);
            }
            if (doingWords) {
                Matcher m = WORD_PAT.matcher(line);
                if (m.find()) {
                    Tree leaf = leaves.get(Integer.parseInt(m.group(2)));
                    if (leaf == null) {
                        throw new RuntimeException("Missing leaf " + m.group(2));
                    }
                    leaf.setLabel(new StringLabel(m.group(3)));
                    leaves.remove(Integer.parseInt(m.group(2)));
                } else {
                    if (line.isEmpty()) {
                        doingWords = false;
                        if (!leaves.isEmpty()) {
                            throw new RuntimeException("unmatched leaves " + leaves);
                        }
                        continue;
                    } else {
                        throw new RuntimeException("strange word line " + line);
                    }
                }
                continue;
            }
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.exit(0);
    }
    // prune to (optionally) remove punctuation nodes etc, then flatten to remove their dedicated parents
    if (rootNode != null) {
        //         System.out.println();
        //         System.out.println("raw tree " + rootNode.pennString());
        //         System.out.println("pru tree " + rootNode.prune(nodeFilter).pennString());
        //         System.out.println("fla tree " + rootNode.prune(nodeFilter).flatten().pennString());
        //         rootNode = rootNode.prune(nodeFilter).flatten();
    }
    return rootNode;
}

From source file:reck.parser.lexparser.RECKLexicalizedParser.java

License:Open Source License

public RECKCTTreeNodeImpl convertToRECKTree(Tree root, int startSentence, String content) {

    RECKCTTreeNodeImpl newRoot = null;/* w  ww  .j a v  a  2 s .  com*/

    Charseq pos = null;

    List nodeList = root.getLeaves();
    HashSet parentSet = new HashSet();
    int docIndex = startSentence;
    String st = null;

    // compute leaves' positions
    for (int i = 0; i < nodeList.size(); i++) {
        Tree oldNode = (Tree) nodeList.get(i);
        st = oldNode.toString();

        int start = content.indexOf(st, docIndex);
        if (start == -1 || start - docIndex > maxDistanceBetweenLeaves) {
            if (st.indexOf("&") != -1) {
                String tmp = st.replaceAll("&", "&amp;");
                start = content.indexOf(tmp, docIndex);
                if (start == -1 || start - docIndex > maxDistanceBetweenLeaves) {
                    tmp = st.replaceAll("&", "&AMP;");
                    start = content.indexOf(tmp, docIndex);
                }
            }
            if (start != -1 && start - docIndex <= maxDistanceBetweenLeaves) {
                docIndex = start + st.length() + 4;
            } else {
                st = reConvert(st);
                start = content.indexOf(st, docIndex);
                if (start == -1 || start - docIndex > maxDistanceBetweenLeaves) {
                    if (st.equals("-LRB-") || st.equals("-LCB-")) {
                        int i1 = content.indexOf("(", docIndex);
                        int i2 = content.indexOf("[", docIndex);
                        int i3 = content.indexOf("{", docIndex);
                        if (i1 == -1)
                            i1 = content.length();
                        if (i2 == -1)
                            i2 = content.length();
                        if (i3 == -1)
                            i3 = content.length();

                        if ((i1 == i2) && (i1 == i3))
                            System.out.println("Come here !");
                        else if (i1 < i2) {
                            if (i3 < i1) {
                                // st = "{";
                                start = i3;
                            } else {
                                // st = "(";
                                start = i1;
                            }
                        } else {
                            if (i3 < i2) {
                                // st = "{";
                                start = i3;
                            } else {
                                // st = "[";
                                start = i2;
                            }
                        }
                        docIndex = start + 1;
                    }

                    else if (st.equals("-RRB-") || st.equals("-RCB-")) {
                        int i1 = content.indexOf(")", docIndex);
                        int i2 = content.indexOf("]", docIndex);
                        int i3 = content.indexOf("}", docIndex);
                        if (i1 == -1)
                            i1 = content.length();
                        if (i2 == -1)
                            i2 = content.length();
                        if (i3 == -1)
                            i3 = content.length();

                        if ((i1 == i2) && (i1 == i3))
                            System.out.println("Come here !");
                        else if (i1 < i2) {
                            if (i3 < i1) {
                                // st = "}";
                                start = i3;
                            } else {
                                // st = ")";
                                start = i1;
                            }
                        } else {
                            if (i3 < i2) {
                                // st = "}";
                                start = i3;
                            } else {
                                // st = "]";
                                start = i2;
                            }
                        }
                        docIndex = start + 1;
                    }

                    else {

                        for (int k = 0; k < newStrings.length; k++) {
                            st = st.replace(newStrings[k], oldStrings[k]);
                        }

                        String oldSubSt1 = new String(new char[] { (char) 39, (char) 39 });
                        String oldSubSt2 = new String(new char[] { (char) 96, (char) 96 });
                        String newSubSt = new String(new char[] { (char) 34 });
                        if (st.indexOf(oldSubSt1) != -1 && content.substring(docIndex).indexOf(newSubSt) != -1)
                            st = st.replace(oldSubSt1, newSubSt);
                        else if (st.indexOf(oldSubSt2) != -1
                                && content.substring(docIndex).indexOf(newSubSt) != -1)
                            st = st.replace(oldSubSt2, newSubSt);

                        int i39 = content.indexOf(39, docIndex);
                        int i96 = content.indexOf(96, docIndex);

                        if ((st.indexOf(39) != -1) && (i96 != -1 && i96 - docIndex <= maxDistanceBetweenLeaves))
                            st = st.replace((char) 39, (char) 96);
                        else if ((st.indexOf(96) != -1)
                                && (i39 != -1 && i39 - docIndex <= maxDistanceBetweenLeaves))
                            st = st.replace((char) 96, (char) 39);

                        start = content.indexOf(st, docIndex);
                        if (start == -1 || start - docIndex > maxDistanceBetweenLeaves)
                            System.out.println("Come here !");
                        else
                            docIndex = start + st.length();
                    }
                } else
                    docIndex = start + st.length();
            }
        } else
            docIndex = start + st.length();

        // Test if next node is a sentence splitter, means "."
        if (st.endsWith(".") && i < nodeList.size() - 1) {
            Tree nextNode = (Tree) nodeList.get(i + 1);
            String nextLabel = nextNode.label().value();
            int nextStart = content.indexOf(nextLabel, docIndex);

            if (nextLabel.equals(".") && (nextStart == -1 || nextStart - docIndex > maxDistanceBetweenLeaves)) {
                docIndex--;
                oldNode.setLabel(new StringLabel(st.substring(0, st.length() - 1)));
            }
        }

        pos = new Charseq(start, docIndex);
        RECKCTTreeNodeImpl newNode = new RECKCTTreeNodeImpl(new StringLabel(st),
                (List) oldNode.getChildrenAsList(), pos);
        Tree parent = oldNode.parent(root);
        parent.setChild(parent.indexOf(oldNode), newNode);
        parentSet.add(parent);
    }

    nodeList.clear();
    nodeList.addAll(parentSet);

    // compute upper nodes' positions
    while (!nodeList.isEmpty()) {
        parentSet = new HashSet();
        for (int i = 0; i < nodeList.size(); i++) {
            Tree oldNode = (Tree) nodeList.get(i);
            Iterator nodeIter = oldNode.getChildrenAsList().iterator();
            Tree node = (Tree) nodeIter.next();
            while (node instanceof RECKCTTreeNodeImpl && nodeIter.hasNext()) {
                node = (Tree) nodeIter.next();
            }
            if (node instanceof RECKCTTreeNodeImpl) {
                Long start = ((RECKCTTreeNodeImpl) oldNode.firstChild()).getPosition().getStart();
                Long end = ((RECKCTTreeNodeImpl) oldNode.lastChild()).getPosition().getEnd();
                pos = new Charseq(start, end);
                RECKCTTreeNodeImpl newNode = new RECKCTTreeNodeImpl(oldNode.label(),
                        (List) oldNode.getChildrenAsList(), pos);
                Tree parent = oldNode.parent(root);
                parent.setChild(parent.indexOf(oldNode), newNode);
                parentSet.add(parent);

                // if oldNode is in parentSet, remove it
                if (parentSet.contains(oldNode)) {
                    parentSet.remove(oldNode);
                }
            } else {
                parentSet.add(oldNode);
            }
        }

        nodeList.clear();
        if (parentSet.size() == 1 && parentSet.contains(root)) {
            Long start = ((RECKCTTreeNodeImpl) root.firstChild()).getPosition().getStart();
            Long end = ((RECKCTTreeNodeImpl) root.lastChild()).getPosition().getEnd();
            pos = new Charseq(start, end);
            newRoot = new RECKCTTreeNodeImpl(root.label(), (List) root.getChildrenAsList(), pos);
        } else {
            nodeList.addAll(parentSet);
        }
    }

    return newRoot;

}

From source file:sg.edu.nus.comp.pdtb.parser.ArgExtComp.java

License:Open Source License

private String printFeature(Tree root, Tree node, String connStr, String connCat, Tree connNode, String label) {
    StringBuilder feature = new StringBuilder();

    feature.append("conn:");
    feature.append(connStr);//from   w  w w. ja  v  a2s .  c  o m
    feature.append(' ');

    feature.append("conn_lc:");
    feature.append(connStr.toLowerCase());
    feature.append(' ');

    feature.append("conn_cat:");
    feature.append(connCat);
    feature.append(' ');

    String path = findPath(root, connNode, node);
    feature.append("conn_to_node:");
    feature.append(path);
    feature.append(' ');

    Tree parent = connNode.parent(root);
    Tree[] children = parent.children();

    int lsibs = 0;
    int rsibs = 0;
    boolean countLeft = true;
    for (int i = 0; i < children.length; ++i) {
        if (children[i].equals(connNode)) {
            countLeft = false;
            continue;
        }
        if (countLeft) {
            ++lsibs;
        } else {
            ++rsibs;
        }
    }

    feature.append("conn_node_lsib_size=");
    feature.append(lsibs);
    feature.append(' ');

    feature.append("conn_node_rsib_size=");
    feature.append(rsibs);
    feature.append(' ');
    if (lsibs > 1) {
        feature.append("conn_to_node:");
        feature.append(path);
        feature.append("^conn_node_lsib_size:>1");
        feature.append(' ');
    }

    int relpos = relativePosition(root, connNode, node);
    feature.append("conn_to_node_relpos:");
    feature.append(relpos);
    feature.append(' ');

    feature.append(label);

    return feature.toString();
}

From source file:sg.edu.nus.comp.pdtb.parser.ArgExtComp.java

License:Open Source License

/**
 * <pre>//w ww. j  a va  2s. co  m
 *     # 0:  node1 and node2 in the same path to root
 *     # 1:  node2 is at the rhs of node1's path to root
 *     # -1: node2 is at the lhs of node1's path to root
 * </pre>
 * 
 * @param root
 * @param connNode
 * @param node
 * @return
 */
private static int relativePosition(Tree root, Tree connNode, Tree node) {
    Tree curr = connNode;
    while (curr != null && !curr.equals(root)) {
        if (curr.equals(node)) {
            return 0;
        }
        Tree parent = curr.parent(root);
        Tree[] children = parent.children();

        for (int i = 0; i < children.length; ++i) {
            if (children[i].contains(node)) {
                int nodeNum = node.nodeNumber(root);
                int connNum = connNode.nodeNumber(root);

                if (nodeNum < connNum) {
                    return -1;
                } else {
                    return 1;
                }
            }
        }

        curr = parent;
    }

    return 0;
}

From source file:sg.edu.nus.comp.pdtb.parser.ArgExtComp.java

License:Open Source License

private static List<String> findUpwardPath(Tree root, Tree connNode, Tree lca) {
    List<String> path = new ArrayList<>();
    if (connNode == null || lca == null) {// || connNode.equals(lca)) {
        return path;
    }//from w ww  .j  a  va 2s .c o m

    Tree curr = connNode;
    while (curr != null && !curr.equals(lca)) {
        String val = curr.value();
        if (val != null) {
            int t = val.indexOf("-");
            if (t > 0) {
                val = val.substring(0, t);
            }
            path.add(val);
        }
        curr = curr.parent(root);
    }

    if (curr != null && curr.equals(lca)) {
        String val = curr.value();
        if (val != null) {
            int t = val.indexOf("-");
            if (t > 0) {
                val = val.substring(0, t);
            }
            path.add(val);
        }
    }

    if (curr == null && path.isEmpty()) {
        return new ArrayList<>();
    } else {
        return path;
    }
}

From source file:sg.edu.nus.comp.pdtb.parser.ArgExtComp.java

License:Open Source License

private Tree[] getBioArgNodes(Tree root, String[] cols, ArrayList<String> spanArray, String connCat,
        Tree connNode) {
    List<Tree> arg1Nodes = getTreeNodesFromSpan(cols[14], spanArray);

    if (connCat.equals("Coordinator") && connNode.value().equals("CC")) {
        Tree[] children = connNode.parent(root).children();
        for (Tree child : children) {
            int ind = arg1Nodes.indexOf(child);
            if (ind == -1 && isPuncTag(child.value())) {
                arg1Nodes.add(child);/*from ww  w.  j av  a 2s. c om*/
            }
        }
    }

    Tree arg1Node = (arg1Nodes.size() == 1) ? arg1Nodes.get(0) : getLCA(root, arg1Nodes);

    List<Tree> arg2Nodes = getTreeNodesFromSpan(cols[20], spanArray);

    Tree arg2Node = (arg2Nodes.size() == 1) ? arg2Nodes.get(0) : getLCA(root, arg2Nodes);

    return new Tree[] { arg1Node, arg2Node };
}

From source file:sg.edu.nus.comp.pdtb.parser.ArgExtComp.java

License:Open Source License

private Tree[] getArgNodes(Tree root, String[] cols, ArrayList<String> spanArray, String connCat,
        Tree connNode) {
    List<Tree> arg1Nodes = getTreeNodesFromSpan(cols[22], spanArray);
    arg1Nodes.addAll(getTreeNodesFromSpan(cols[29], spanArray));

    if (connCat.equals("Coordinator") && connNode.value().equals("CC")) {
        Tree[] children = connNode.parent(root).children();
        for (Tree child : children) {
            int ind = arg1Nodes.indexOf(child);
            if (ind == -1 && isPuncTag(child.value())) {
                arg1Nodes.add(child);/*from  www. j  a v a  2  s.co m*/
            }
        }
    }

    Tree arg1Node = (arg1Nodes.size() == 1) ? arg1Nodes.get(0) : getLCA(root, arg1Nodes);

    List<Tree> arg2Nodes = getTreeNodesFromSpan(cols[32], spanArray);
    arg2Nodes.addAll(getTreeNodesFromSpan(cols[39], spanArray));

    Tree arg2Node = (arg2Nodes.size() == 1) ? arg2Nodes.get(0) : getLCA(root, arg2Nodes);

    return new Tree[] { arg1Node, arg2Node };
}

From source file:sg.edu.nus.comp.pdtb.parser.ArgExtComp.java

License:Open Source License

private static List<Tree> getInternalNodes(Tree root, Tree node) {
    // @child_nodes.size == 1 and @child_nodes.first.class != Node

    List<Tree> result = new ArrayList<>();

    if (node != null
            && !(node.children().length == 1 && node.firstChild() != null && node.firstChild().isLeaf())) {
        Tree parent = node.parent(root);
        if (parent != null && !node.value().equals("-NONE-")) {
            result.add(node);//from  w  w w .  jav  a 2s.co m
        }
        Tree[] children = node.children();
        for (Tree child : children) {
            result.addAll(getInternalNodes(root, child));
        }
    }

    return result;
}