Example usage for edu.stanford.nlp.trees TreeFactory newTreeNode

List of usage examples for edu.stanford.nlp.trees TreeFactory newTreeNode

Introduction

In this page you can find the example usage for edu.stanford.nlp.trees TreeFactory newTreeNode.

Prototype

public Tree newTreeNode(Label label, List<Tree> children);

Source Link

Document

Create a new tree non-leaf node, with the given label.

Usage

From source file:de.tudarmstadt.ukp.dkpro.core.corenlp.internal.DKPro2CoreNlp.java

License:Open Source License

public static Tree createStanfordTree(org.apache.uima.jcas.tcas.Annotation root, TreeFactory tFact,
        Map<Token, IndexedWord> aIdxTokens) {
    JCas aJCas;/*w  w w.jav  a 2 s . c  om*/
    try {
        aJCas = root.getCAS().getJCas();
    } catch (CASException e) {
        throw new IllegalStateException("Unable to get JCas from JCas wrapper");
    }

    // define the new (root) node
    Tree rootNode;

    // before we can create a node, we must check if we have any children (we have to know
    // whether to create a node or a leaf - not very dynamic)
    if (root instanceof Constituent && !isLeaf((Constituent) root)) {
        Constituent node = (Constituent) root;
        List<Tree> childNodes = new ArrayList<Tree>();

        // get childNodes from child annotations
        FSArray children = node.getChildren();
        for (int i = 0; i < children.size(); i++) {
            childNodes.add(createStanfordTree(node.getChildren(i), tFact, aIdxTokens));
        }

        // now create the node with its children
        rootNode = tFact.newTreeNode(node.getConstituentType(), childNodes);

    } else {
        // Handle leaf annotations
        // Leafs are always Token-annotations
        // We also have to insert a Preterminal node with the value of the
        // POS-Annotation on the token
        // because the POS is not directly stored within the treee
        Token wordAnnotation = (Token) root;

        // create leaf-node for the tree
        Tree wordNode;
        if (aIdxTokens != null) {
            wordNode = tFact.newLeaf(aIdxTokens.get(wordAnnotation));
        } else {
            wordNode = tFact.newLeaf(wordAnnotation.getCoveredText());
        }

        // create information about preceding and trailing whitespaces in the leaf node
        StringBuilder preWhitespaces = new StringBuilder();
        StringBuilder trailWhitespaces = new StringBuilder();

        List<Token> precedingTokenList = selectPreceding(aJCas, Token.class, wordAnnotation, 1);
        List<Token> followingTokenList = selectFollowing(aJCas, Token.class, wordAnnotation, 1);

        if (precedingTokenList.size() > 0) {
            Token precedingToken = precedingTokenList.get(0);
            int precedingWhitespaces = wordAnnotation.getBegin() - precedingToken.getEnd();
            for (int i = 0; i < precedingWhitespaces; i++) {
                preWhitespaces.append(" ");
            }
        }
        if (followingTokenList.size() > 0) {
            Token followingToken = followingTokenList.get(0);
            int trailingWhitespaces = followingToken.getBegin() - wordAnnotation.getEnd();
            for (int i = 0; i < trailingWhitespaces; i++) {
                trailWhitespaces.append(" ");
            }
        }

        // write whitespace information as CoreAnnotation.BeforeAnnotation and
        // CoreAnnotation.AfterAnnotation to the node add annotation to list and write back to
        // node label
        ((CoreLabel) wordNode.label()).set(CoreAnnotations.BeforeAnnotation.class, preWhitespaces.toString());
        ((CoreLabel) wordNode.label()).set(CoreAnnotations.AfterAnnotation.class, trailWhitespaces.toString());

        // get POS-annotation
        POS pos = wordAnnotation.getPos();

        // create POS-Node in the tree and attach word-node to it
        rootNode = tFact.newTreeNode(pos.getPosValue(), Arrays.asList((new Tree[] { wordNode })));
    }

    return rootNode;
}

From source file:de.tudarmstadt.ukp.dkpro.core.stanfordnlp.util.TreeUtils.java

License:Open Source License

public static Tree createStanfordTree(Annotation root, TreeFactory tFact) {
    JCas aJCas;//w w  w.  j a  v a2  s  . c  om
    try {
        aJCas = root.getCAS().getJCas();
    } catch (CASException e) {
        throw new IllegalStateException("Unable to get JCas from JCas wrapper");
    }

    // define the new (root) node
    Tree rootNode;

    // before we can create a node, we must check if we have any children (we have to know
    // whether to create a node or a leaf - not very dynamic)
    if (root instanceof Constituent && !isLeaf((Constituent) root)) {
        Constituent node = (Constituent) root;
        List<Tree> childNodes = new ArrayList<Tree>();

        // get childNodes from child annotations
        FSArray children = node.getChildren();
        for (int i = 0; i < children.size(); i++) {
            childNodes.add(createStanfordTree(node.getChildren(i), tFact));
        }

        // now create the node with its children
        rootNode = tFact.newTreeNode(node.getConstituentType(), childNodes);

    } else {
        // Handle leaf annotations
        // Leafs are always Token-annotations
        // We also have to insert a Preterminal node with the value of the
        // POS-Annotation on the token
        // because the POS is not directly stored within the treee
        Token wordAnnotation = (Token) root;

        // create leaf-node for the tree
        Tree wordNode = tFact.newLeaf(wordAnnotation.getCoveredText());

        // create information about preceding and trailing whitespaces in the leaf node
        StringBuilder preWhitespaces = new StringBuilder();
        StringBuilder trailWhitespaces = new StringBuilder();

        List<Token> precedingTokenList = selectPreceding(aJCas, Token.class, wordAnnotation, 1);
        List<Token> followingTokenList = selectFollowing(aJCas, Token.class, wordAnnotation, 1);

        if (precedingTokenList.size() > 0) {
            Token precedingToken = precedingTokenList.get(0);
            int precedingWhitespaces = wordAnnotation.getBegin() - precedingToken.getEnd();
            for (int i = 0; i < precedingWhitespaces; i++) {
                preWhitespaces.append(" ");
            }
        }
        if (followingTokenList.size() > 0) {
            Token followingToken = followingTokenList.get(0);
            int trailingWhitespaces = followingToken.getBegin() - wordAnnotation.getEnd();
            for (int i = 0; i < trailingWhitespaces; i++) {
                trailWhitespaces.append(" ");
            }
        }

        // write whitespace information as CoreAnnotation.BeforeAnnotation and
        // CoreAnnotation.AfterAnnotation to the node add annotation to list and write back to
        // node label
        ((CoreLabel) wordNode.label()).set(CoreAnnotations.BeforeAnnotation.class, preWhitespaces.toString());
        ((CoreLabel) wordNode.label()).set(CoreAnnotations.AfterAnnotation.class, trailWhitespaces.toString());

        // get POS-annotation
        // get the token that is covered by the POS
        List<POS> coveredPos = JCasUtil.selectCovered(aJCas, POS.class, wordAnnotation);
        // the POS should only cover one token
        assert coveredPos.size() == 1;
        POS pos = coveredPos.get(0);

        // create POS-Node in the tree and attach word-node to it
        rootNode = tFact.newTreeNode(pos.getPosValue(), Arrays.asList((new Tree[] { wordNode })));
    }

    return rootNode;
}