Example usage for opennlp.tools.parser Parse getSpan

List of usage examples for opennlp.tools.parser Parse getSpan

Introduction

In this page you can find the example usage for opennlp.tools.parser Parse getSpan.

Prototype

public Span getSpan() 

Source Link

Document

Returns the character offsets for this constituent.

Usage

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

/**
 * Creates linked constituent annotations + POS annotations
 *
 * @param aNode//from   w w w .  j  a  va2  s .  co  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;
    }
}

From source file:org.opentestsystem.airose.docquality.processors.PassiveSentencesQualityProcessor.java

private boolean checkPassive(AbstractDocument doc, Parse p) {

    Queue<Parse> queue = new LinkedList<Parse>();
    queue.add(p);/*from  w  ww  .ja va2  s. c  o  m*/

    while (queue.size() > 0) {
        p = queue.remove();
        String parseType = p.getType();
        if ((parseType.length() >= 2) && StringUtils.equalsIgnoreCase(parseType.substring(0, 2), "VB")) {

            String word = p.getText().substring(p.getSpan().getStart(),
                    p.getSpan().getStart() + p.getSpan().length());

            List<String> roots = wordnet.getBaseWords(word, EnumPOS.VERB);
            if ((roots.size() > 0) && (StringUtils.endsWithIgnoreCase(roots.get(0), "be"))) {
                return true;
            } else
                return false;

        } else {
            for (Parse child : p.getChildren())
                queue.add(child);
        }
    }
    return false;
}