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

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

Introduction

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

Prototype

public int depth() 

Source Link

Document

Finds the depth of the tree.

Usage

From source file:de.tudarmstadt.ukp.experiments.argumentation.convincingness.features.DependencyTreeDepthFeature.java

License:Apache License

@Override
protected List<Feature> extractFeaturesFromArgument(JCas jCas, Paragraph paragraph, String prefix)
        throws AnalysisEngineProcessException {
    List<Feature> featList = new ArrayList<Feature>();

    Collection<ROOT> root = JCasUtil.selectCovered(ROOT.class, paragraph);

    if (!root.isEmpty()) {
        Tree tree = TreeUtils.createStanfordTree(root.iterator().next());
        featList.add(new Feature(prefix + FN_DEPENDENCY_TREE_DEPTH, tree.depth()));
    }//from  w  w w  .j  av a  2 s  .  co m

    return featList;
}

From source file:de.tudarmstadt.ukp.experiments.argumentation.sequence.feature.syntactic.DependencyTreeDepth.java

License:Apache License

@Override
protected List<Feature> extract(JCas jCas, Sentence sentence, String sentencePrefix)
        throws TextClassificationException {
    List<Feature> featList = new ArrayList<>();

    Collection<ROOT> root = JCasUtil.selectCovered(ROOT.class, sentence);

    if (!root.isEmpty()) {
        Tree tree = TreeUtils.createStanfordTree(root.iterator().next());
        featList.add(new Feature(sentencePrefix + FN_DEPENDENCY_TREE_DEPTH, tree.depth()));
    }// w w w.java 2  s. c  om

    return featList;
}

From source file:gate.stanford.Parser.java

License:Open Source License

/**
 * Generate a SyntaxTreeNode Annotation corresponding to this Tree.  Work 
 * recursively so that the annotations are actually generated from the 
 * bottom up, in order to build the consists list of annotation IDs.
 * /*  w  w  w.  j ava  2 s .c om*/
 * @param tree  the current subtree
 * @param rootTree  the whole sentence, used to find the span of the current subtree
 * @return a GATE Annotation of type "SyntaxTreeNode"
 */
protected Annotation annotatePhraseStructureRecursively(AnnotationSet annotationSet,
        StanfordSentence stanfordSentence, Tree tree, Tree rootTree) {
    Annotation annotation = null;
    Annotation child;
    String label = tree.value();

    List<Tree> children = tree.getChildrenAsList();

    if (children.size() == 0) {
        return null;
    }
    /* implied else */

    /* following line generates ClassCastException
     *       IntPair span = tree.getSpan();
     * edu.stanford.nlp.ling.CategoryWordTag
     * at edu.stanford.nlp.trees.Tree.getSpan(Tree.java:393)
     * but I think it's a bug in the parser, so I'm hacking 
     * around it as follows. */
    int startPos = Trees.leftEdge(tree, rootTree);
    int endPos = Trees.rightEdge(tree, rootTree);

    Long startNode = stanfordSentence.startPos2offset(startPos);
    Long endNode = stanfordSentence.endPos2offset(endPos);

    List<Integer> consists = new ArrayList<Integer>();

    Iterator<Tree> childIter = children.iterator();
    while (childIter.hasNext()) {
        child = annotatePhraseStructureRecursively(annotationSet, stanfordSentence, childIter.next(), rootTree);
        if ((child != null) && (!child.getType().equals(inputTokenType))) {
            consists.add(child.getId());
        }
    }
    annotation = annotatePhraseStructureConstituent(annotationSet, startNode, endNode, label, consists,
            tree.depth());

    return annotation;
}

From source file:qmul.util.treekernel.FragmentIdentifier.java

License:Open Source License

public ArrayList<Fragment> getFragments(Tree t) {

    List<Tree> children = t.getChildrenAsList();
    ArrayList<Fragment> fragments = new ArrayList<Fragment>();
    String dominatedPhrase = getDominatedPhrase(t);
    Fragment frag = new Fragment(t.label().value(), dominatedPhrase);
    // System.out.println("Adding "+t.label()+"-->"+dominatedPhrase);
    if (dominatedPhrase.length() > 0) {

        if (t.children().length > 1) {

            fragments.add(frag);//from  w w  w .  j  a  va2 s.  c o  m
        } else if (t.depth() == 1)
            fragments.add(frag);
    }
    for (Tree child : children) {
        // System.out.println("Merging "+)
        fragments.addAll(getFragments(child));

    }
    return fragments;

}

From source file:qmul.util.treekernel.FragmentIdentifier.java

License:Open Source License

public HashMap<String, ArrayList<String>> getPhraseTypeToFragmentsMap(Tree t) {

    List<Tree> children = t.getChildrenAsList();
    HashMap<String, ArrayList<String>> fragments = new HashMap<String, ArrayList<String>>();
    String dominatedPhrase = getDominatedPhrase(t);
    // System.out.println("Adding "+t.label()+"-->"+dominatedPhrase);
    if (dominatedPhrase.length() > 0) {
        ArrayList<String> v = new ArrayList<String>();
        v.add(dominatedPhrase);/*ww w  .  ja va  2  s.  c  om*/

        if (t.children().length > 1) {

            fragments.put(t.label().value(), v);
        } else if (t.depth() == 1)
            fragments.put(t.label().value(), v);
    }
    for (Tree child : children) {
        // System.out.println("Merging "+)
        fragments = merge(fragments, getPhraseTypeToFragmentsMap(child));

    }
    return fragments;

}

From source file:qmul.util.treekernel.TreeKernel.java

License:Open Source License

/**
 * Set up sorted lists of all productions in each tree, optionally including lexical rules
 *///from   www .j  a v a 2 s .  c  om
private static void extractSortProductionLists() {
    l1.clear();
    l2.clear();
    l1counts.clear();
    l2counts.clear();
    for (Tree cur1 : t1) {
        if (cur1.depth() < (includeWords ? 1 : 2))
            continue;
        Production p = new Production(cur1, true);
        if (!allowedProductions.isEmpty() && !allowedProductions.contains(p.getBnf()))
            continue;
        if (!bannedProductions.isEmpty() && bannedProductions.contains(p.getBnf()))
            continue;
        l1.add(p);
        MapUtil.increment(l1counts, p.getBnf());
    }
    for (Tree cur2 : t2) {
        if (cur2.depth() < (includeWords ? 1 : 2))
            continue;
        Production p = new Production(cur2, true);
        if (!allowedProductions.isEmpty() && !allowedProductions.contains(p.getBnf()))
            continue;
        if (!bannedProductions.isEmpty() && bannedProductions.contains(p.getBnf()))
            continue;
        l2.add(p);
        MapUtil.increment(l2counts, p.getBnf());
    }
    Comparator<Production> pc = new ProductionComparator();
    Collections.sort(l1, pc);
    Collections.sort(l2, pc);
}

From source file:qmul.util.treekernel.TreeKernel.java

License:Open Source License

public static long countSubSetTrees(Tree t) {
    if (t.depth() == (includeWords ? 0 : 1))
        return 0;
    else {/*ww  w. ja  va 2  s .c  o m*/
        int sum = 0;
        int product = 1;
        for (Tree cur : t.getChildrenAsList()) {
            sum += countSubSetTrees(cur);
            product *= (1 + countRootSST(cur));
        }
        return sum + product;
    }
}

From source file:qmul.util.treekernel.TreeKernel.java

License:Open Source License

private static long countRootSST(Tree t) {
    if (t.depth() == (includeWords ? 0 : 1))
        return 0;
    else {//  w w  w . j  a  v  a  2 s .  c om
        long product = 1;
        for (Tree cur : t.getChildrenAsList()) {
            product *= (1 + countRootSST(cur));
        }
        return product;
    }
}