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

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

Introduction

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

Prototype

public Tree firstChild() 

Source Link

Document

Returns the first child of a tree, or null if none.

Usage

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

License:Open Source License

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

    RECKCTTreeNodeImpl newRoot = null;//from   ww w  .  j a  va 2  s . c  om

    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[] getAnyPSArgSpans(String pipe, Map<String, String> spanHashMap) {
    String[] cols = pipe.split("\\|", -1);
    String connSpan = cols[3];/*from  ww w  .  j a  v  a  2  s. c  o  m*/
    int arg2TreeNum = Integer.parseInt(cols[7]);

    Tree arg2Root = trees.get(arg2TreeNum);
    String arg2 = (arg2TreeNum) + ":" + arg2Root.firstChild().nodeNumber(arg2Root);
    ArrayList<String> arg2Nodes = new ArrayList<String>();
    arg2Nodes.add(arg2);

    String arg2Span = calcNodesSpan(arg2Nodes, spanHashMap, connSpan, null);
    String arg1Span = "1..2";

    if (arg2TreeNum > 0) {
        int arg1TreeNum = arg2TreeNum - 1;
        Tree arg1Root = trees.get(arg1TreeNum);
        String arg1 = (arg1TreeNum) + ":" + arg1Root.firstChild().nodeNumber(arg1Root);
        ArrayList<String> arg1Nodes = new ArrayList<String>();
        arg1Nodes.add(arg1);
        arg1Span = calcNodesSpan(arg1Nodes, spanHashMap, connSpan, arg2Nodes);
    }

    return new String[] { arg1Span, arg2Span, Corpus.spanToText(arg1Span, orgText).replaceAll("\\|", "<PIPE>"),
            Corpus.spanToText(arg2Span, orgText).replaceAll("\\|", "<PIPE>") };
}

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.ja  v a 2s  .co m*/
        }
        Tree[] children = node.children();
        for (Tree child : children) {
            result.addAll(getInternalNodes(root, child));
        }
    }

    return result;
}

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

License:Open Source License

private String[] getPSArgSpans(Type corpus, String pipe, ArrayList<String> spanArray,
        Map<String, String> spanHashMap, FeatureType featureType) throws IOException {

    String[] cols = pipe.split("\\|", -1);

    String connSpan = cols[corpus.equals(Type.PDTB) ? 3 : 1];
    String[] connGorn = null;//from ww w .j av  a 2 s  .  c o m
    if (corpus.equals(Type.PDTB)) {
        connGorn = cols[4].split(";");
    } else {
        LinkedList<Integer> temp = Corpus.spanToSenIds(cols[1], spanArray);
        Set<Integer> set = new HashSet<>(temp);
        connGorn = new String[set.size()];
        int i = 0;
        for (Integer t : set) {
            connGorn[i] = t.toString();
            ++i;
        }
    }
    int connTree = -1;

    for (String conn : connGorn) {
        String[] par = conn.split(",");
        int tmp = Integer.parseInt(par[0]);
        if (!(connTree != -1 && connTree == tmp)) {
            connTree = tmp;
        }
    }

    int autoConnTree = getNodeNum(connTree, featureType);

    Tree arg2Root = trees.get(autoConnTree);
    String arg2 = (autoConnTree) + ":" + arg2Root.firstChild().nodeNumber(arg2Root);
    ArrayList<String> arg2Nodes = new ArrayList<String>();
    arg2Nodes.add(arg2);

    String arg2Span = calcNodesSpan(arg2Nodes, spanHashMap, connSpan, null);

    String arg1Span = "1..2";

    if (autoConnTree > 0) {
        int autoConnTree1 = getNodeNum(connTree - 1, featureType);
        Tree arg1Root = trees.get(autoConnTree1);
        String arg1 = (autoConnTree1) + ":" + arg1Root.firstChild().nodeNumber(arg1Root);
        ArrayList<String> arg1Nodes = new ArrayList<String>();
        arg1Nodes.add(arg1);
        arg1Span = calcNodesSpan(arg1Nodes, spanHashMap, connSpan, arg2Nodes);
    }

    return new String[] { arg1Span, arg2Span };
}

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

License:Open Source License

private static boolean containsTrace(Tree node) {
    if (node == null) {
        return false;
    }//from   www.  j  av a 2s  .  c o  m
    if (node.value().equals("-NONE-") && node.firstChild().value().matches("^\\*T\\*.*")) {
        return true;
    }
    List<Tree> children = node.getChildrenAsList();
    for (Tree child : children) {
        if (containsTrace(child)) {
            return true;
        }
    }

    return false;
}

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

License:Open Source License

public static void getDependencyRules(TreeNode treeNode, HashMap<String, Dependency> dtreeMap,
         HashMap<String, Integer> depRules) {

     Tree root = treeNode.getRoot();//from w  ww  . j a v  a  2s . c  o m
     Tree node = treeNode.getNode();
     int treeNumber = treeNode.getTreeNumber();

     List<Tree> allPosNodes = new ArrayList<Tree>();
     getAllPosNodes(node, allPosNodes);

     List<Integer> leafNodes = new ArrayList<Integer>();
     for (Tree pn : allPosNodes) {
         leafNodes.add(pn.firstChild().nodeNumber(root));
     }

     for (int i = 0; i < leafNodes.size(); ++i) {
         Tree n = root.getNodeNumber(leafNodes.get(i));
         int nInd = n.nodeNumber(root);
         Dependency nDepRel = dtreeMap.get(treeNumber + " : " + nInd);
         if (nDepRel != null) {
             List<Tree> nDependents = new ArrayList<Tree>();
             for (Integer ind : nDepRel.getDependents()) {
                 if (leafNodes.contains(ind)) {
                     Tree d = root.getNodeNumber(ind);
                     nDependents.add(d);
                 }
             }

             if (nDependents.size() > 0) { // has nDependents

                 String rule = n.value() + " <- ";

                 for (Tree n1 : nDependents) {
                     int n1Ind = n1.nodeNumber(root);
                     Dependency n1DepRel = dtreeMap.get(treeNumber + " : " + n1Ind);
                     String dependency = n1DepRel.getLabel();
                     rule += "<" + dependency + "> ";
                 }
                 rule = rule.trim();

                 if (!rule.endsWith("<-")) { // rule has some child values
                     rule = rule.replace(' ', '_');
                     Integer value = depRules.containsKey(rule) ? depRules.get(rule) : 0;
                     value += 1;
                     depRules.put(rule, value);
                 }
             }
         }
     }
 }

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

License:Open Source License

public static void getAllPosNodes(Tree node, List<Tree> allPosNodes) {
     boolean isPos = node.numChildren() == 1 && node.firstChild().isLeaf();
     if (!isPos) {
         for (Tree child : node.children()) {
             getAllPosNodes(child, allPosNodes);
         }/*from   w  ww.  j  ava 2s .  c o  m*/
     } else {
         if (!node.value().equalsIgnoreCase("-NONE-")) {
             allPosNodes.add(node);
         }
     }
 }

From source file:sg.edu.nus.comp.pdtb.util.Corpus.java

License:Open Source License

public static String nodesToString(List<Tree> posNodes) {
     StringBuilder sba = new StringBuilder();
     for (Tree leaf : posNodes) {
         sba.append(Corpus.nodeToString(leaf.firstChild()));
         sba.append(' ');
     }//from  w ww  . j  a  va2  s. c o  m
     return sba.toString().trim();
 }