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

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

Introduction

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

Prototype

public Tree removeChild(int i) 

Source Link

Document

Destructively removes the child at some daughter index and returns it.

Usage

From source file:pltag.util.Utils.java

License:Open Source License

public static Tree removeEmptyNodes(Tree tree) {
    Tree[] children = tree.children();/*w  w  w  . j av  a  2s  .  com*/
    for (int i = 0; i < children.length; i++) {
        Tree child = children[i];
        // heuristic: all leaf nodes have a special anchor symbol '<>' in the end.
        // If we don't find this, then we should remove the node.
        if (child.numChildren() == 0 && !child.label().value().contains("<>")) {
            //                System.out.println("node " + child);                
            tree.removeChild(i);
        } else {
            removeEmptyNodes(child);
        }
    }
    return tree;
}

From source file:pltag.util.Utils.java

License:Open Source License

public static void pruneChildrenAfter(Tree parent, int index) {
    int numOfChildren = parent.numChildren();
    for (int i = index + 1; i < numOfChildren; i++) {
        parent.removeChild(index + 1);
    }/* ww  w. j a va  2s  .com*/
}

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  ww .  ja v a 2  s  .  c  om
        // 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.StanfordParser.java

License:Open Source License

/**
 * Convenience method: splits utt into sentences, uses {@link LexicalizedParser}'s parse() to tokenize and parse
 * each sentence/*from   w w w. ja va2s.  c o  m*/
 * 
 * @param utt
 * @return a {@link Tree} with ROOT node, with the getBestParse() trees for each sentence as children
 */
public Tree parse(String utt) {
    String[] sentences = utt.split("[.!?]");
    // System.out.println("there are sentences:" + sentences.length);
    // LinkedList<Tree> list=new LinkedList<Tree>();
    Label rootLabel = new StringLabel("ROOT");
    Tree concat = new LabeledScoredTreeNode(rootLabel, new LinkedList<Tree>());

    try {
        for (int i = 0; i < sentences.length; i++) {
            boolean parsed = false;
            if (sentences[i].length() > 0)
                parsed = lp.parse(sentences[i]);
            else
                continue;
            Tree t = lp.getBestParse();
            Tree rootChild;
            if (t.children().length == 1)
                rootChild = t.removeChild(0);
            else
                rootChild = t;
            concat.addChild(rootChild);
        }
        if (concat.children().length > 1)
            return concat;
        else
            return concat.removeChild(0);
    } catch (Throwable t) {
        System.out.println(t.getMessage());
        System.out.println("Reinitializing parser because of trying to parse error " + utt);
        this.lp = null;
        Runtime r = Runtime.getRuntime();
        r.gc();
        lp = new LexicalizedParser(System.getProperty("user.dir") + File.separator + "utils" + File.separator
                + "englishPCFG.ser.gz");
        this.lp.setOptionFlags(new String[] { "-maxLength", "100", "-retainTmpSubcategories" });
        return null;
    }

}

From source file:uk.ac.gla.mir.util.TripletExtractor.java

License:Open Source License

private static void getTriplets(final Tree sentence, List<Tree> leaves) {
    Comparator<KeyValue<Integer, Entity>> cmp = new Comparator<KeyValue<Integer, Entity>>() {
        public int compare(KeyValue<Integer, Entity> o1, KeyValue<Integer, Entity> o2) {
            return o1.getValue().compareTo(o2.getValue());
        }/*from   w  ww  .  j ava2 s  .c om*/
    };
    deepestSentence = null;
    dependancy = "";
    toDependancy = "";
    e = null;
    deepestVerbPhrase = null;
    dependancyDepth = 0;
    depthOfSentence = -10;
    childNumberOfDeepestSenctence = -1;
    getDeepestSentence(sentence, 0);
    if (deepestSentence != null) {
        tmpTriplets = new ArrayList<Triplet>();
        Triplet tempTriplet = extractTriplet(deepestSentence, leaves);
        final Tree ancestor = deepestSentence.ancestor(1, sentence);
        if (ancestor == null)
            return;
        if (childNumberOfDeepestSenctence != -1) {
            addAttribute(cmp, sentence, deepestSentence);
            ancestor.removeChild(childNumberOfDeepestSenctence);
        }
        if (ancestor.value().equalsIgnoreCase("ROOT")) {
            return;
        } else {
            Tree parentOfSentence = ancestor.ancestor(1, sentence);
            int clauseIndex = parentOfSentence.indexOf(ancestor);
            for (int j = clauseIndex - 1; j >= 0; j--) {
                Tree lastNoun = parentOfSentence.getChild(j);
                nounFound = false;
                e = new Entity();
                extractLastNoun(lastNoun);
                tempTriplet.lastNP = e;
                if (!tempTriplet.lastNP.isEmpty())
                    if (tempTriplet.subject.isEmpty()) {
                        tempTriplet.subject = e;
                        tempTriplet.subject.attributes = getSubjectAttributes(e.tree.ancestor(1, sentence),
                                true, sentence, e.tree);
                    }
                if (!tempTriplet.lastNP.isEmpty())
                    break;
            }
        }
        getTriplets(sentence, leaves);

    } else {
        tmpTriplets = new ArrayList<Triplet>();
        Triplet tempTriplet = extractTriplet(sentence, leaves);
        addAttribute(cmp, sentence, sentence);
    }
}