Example usage for opennlp.tools.parser AbstractBottomUpParser TOP_NODE

List of usage examples for opennlp.tools.parser AbstractBottomUpParser TOP_NODE

Introduction

In this page you can find the example usage for opennlp.tools.parser AbstractBottomUpParser TOP_NODE.

Prototype

String TOP_NODE

To view the source code for opennlp.tools.parser AbstractBottomUpParser TOP_NODE.

Click Source Link

Document

The label for the top node.

Usage

From source file:de.tudarmstadt.ukp.dkpro.core.opennlp.OpenNlpParser.java

/**
 * Creates linked constituent annotations + POS annotations
 *
 * @param aNode//from w  ww  .  j a v  a  2s.  c o m
 *            the source tree
 * @param aParentFS
 * @param aCreatePos
 *            sets whether to create or not to create POS tags
 * @param aCreateLemmas
 *            sets whether to create or not to create Lemmas
 * @return the child-structure (needed for recursive call only)
 */
private Annotation createConstituentAnnotationFromTree(JCas aJCas, Parse aNode, Annotation aParentFS,
        List<Token> aTokens) {
    // If the node is a word-level constituent node (== POS):
    // create parent link on token and (if not turned off) create POS tag
    if (aNode.isPosTag()) {
        Token token = getToken(aTokens, aNode.getSpan().getStart(), aNode.getSpan().getEnd());

        // link token to its parent constituent
        if (aParentFS != null) {
            token.setParent(aParentFS);
        }

        // only add POS to index if we want POS-tagging
        if (createPosTags) {
            Type posTag = mappingProvider.getTagType(aNode.getType());
            POS posAnno = (POS) aJCas.getCas().createAnnotation(posTag, token.getBegin(), token.getEnd());
            posAnno.setPosValue(internTags ? aNode.getType().intern() : aNode.getType());
            posAnno.addToIndexes();
            token.setPos((POS) posAnno);
        }

        return token;
    }
    // Check if node is a constituent node on sentence or phrase-level
    else {
        String typeName = aNode.getType();
        if (AbstractBottomUpParser.TOP_NODE.equals(typeName)) {
            typeName = "ROOT"; // in DKPro the root is ROOT, not TOP
        }

        // create the necessary objects and methods
        String constituentTypeName = CONPACKAGE + typeName;

        Type type = aJCas.getTypeSystem().getType(constituentTypeName);

        //if type is unknown, map to X-type
        if (type == null) {
            type = aJCas.getTypeSystem().getType(CONPACKAGE + "X");
        }

        Constituent constAnno = (Constituent) aJCas.getCas().createAnnotation(type, aNode.getSpan().getStart(),
                aNode.getSpan().getEnd());
        constAnno.setConstituentType(typeName);

        // link to parent
        if (aParentFS != null) {
            constAnno.setParent(aParentFS);
        }

        // Do we have any children?
        List<Annotation> childAnnotations = new ArrayList<Annotation>();
        for (Parse child : aNode.getChildren()) {
            Annotation childAnnotation = createConstituentAnnotationFromTree(aJCas, child, constAnno, aTokens);
            if (childAnnotation != null) {
                childAnnotations.add(childAnnotation);
            }
        }

        // Now that we know how many children we have, link annotation of
        // current node with its children
        FSArray childArray = (FSArray) FSCollectionFactory.createFSArray(aJCas, childAnnotations);
        constAnno.setChildren(childArray);

        // write annotation for current node to index
        aJCas.addFsToIndexes(constAnno);

        return constAnno;
    }
}