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

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

Introduction

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

Prototype

public boolean isPreTerminal() 

Source Link

Document

Return whether this node is a preterminal or not.

Usage

From source file:org.aksw.simba.bengal.triple2nl.property.PropertyVerbalizer.java

License:Apache License

private PropertyVerbalization getTypeByLinguisticAnalysis(String propertyURI, String propertyText) {
    logger.debug("...using linguistical analysis...");
    Annotation document = new Annotation(propertyText);
    pipeline.annotate(document);//w  w  w .j ava 2 s  .  c  o m
    List<CoreMap> sentences = document.get(SentencesAnnotation.class);

    String pattern = "";
    PropertyVerbalizationType verbalizationType = PropertyVerbalizationType.UNSPECIFIED;
    boolean firstTokenAuxiliary = false;
    for (CoreMap sentence : sentences) {
        List<CoreLabel> tokens = sentence.get(TokensAnnotation.class);
        // get the first word and check if it's 'is' or 'has'
        CoreLabel token = tokens.get(0);
        String word = token.get(TextAnnotation.class);
        String pos = token.get(PartOfSpeechAnnotation.class);
        String lemma = token.getString(LemmaAnnotation.class);

        firstTokenAuxiliary = auxiliaryVerbs.contains(lemma);

        if (lemma.equals("be") || word.equals("have")) {
            pattern += lemma.toUpperCase();
        } else {
            if (pos.startsWith("N")) {
                pattern += "NP";
            } else if (pos.startsWith("V")) {
                pattern += "VP";
            } else {
                pattern += pos;
            }
        }
        if (tokens.size() > 1) {
            pattern += " ";
            for (int i = 1; i < tokens.size(); i++) {
                token = tokens.get(i);
                pos = token.get(PartOfSpeechAnnotation.class);
                if (pos.startsWith("N")) {
                    pattern += "NP";
                } else if (pos.startsWith("V")) {
                    pattern += "VP";
                } else {
                    pattern += pos;
                }
                pattern += " ";
            }
        }
        // get the parse tree
        Tree tree = sentence.get(TreeAnnotation.class);
        // skip ROOT tag
        tree = tree.skipRoot();
        logger.debug("Parse tree:" + tree.pennString());
        // tree.pennPrint();
        // check if VP is directly followed by NP
        // sometimes parent node is S,SINV,etc.
        if (tree.value().matches(Joiner.on('|').join(Lists.newArrayList(S, SBAR, SBARQ, SINV, FRAGMENT)))) {
            tree = tree.getChild(0);
        }
        boolean useDeterminer = false;
        if (tree.value().equals(VERB_PHRASE.getTag())) {
            for (Tree child : tree.getChildrenAsList()) {
                // check if first non terminal is NP and not contains a
                // determiner
                if (!child.isPreTerminal()) {
                    if (child.value().equals(NOUN_PHRASE.getTag())
                            && !child.getChild(0).value().equals(DETERMINER.getTag())) {
                        useDeterminer = true;
                    }
                    break;
                }
            }
        }
        // add determiner tag
        if (useDeterminer) {
            String[] split = pattern.split(" ");
            pattern = split[0] + " DET " + Joiner.on(" ").join(Arrays.copyOfRange(split, 1, split.length));
        }
    }
    pattern = pattern.trim();

    // if first token is an auxiliary can return verb
    if (firstTokenAuxiliary) {
        verbalizationType = PropertyVerbalizationType.VERB;
    }

    // check if pattern matches
    if (pattern.matches(VERB_PATTERN)) {
        logger.debug("...successfully determined type.");
        verbalizationType = PropertyVerbalizationType.VERB;
    }
    return new PropertyVerbalization(propertyURI, propertyText, pattern, verbalizationType);
}

From source file:org.nlp2rdf.implementation.stanfordcore.StanfordCoreNLPWrapper.java

License:Open Source License

public void processTree(Tree currentNode, URIGenerator uriGenerator, String prefix, String text,
        OntModel model) {//from www  .  j a v a  2 s  .  c  om
    // String tag = currentNode.label().value();
    //log.info("Current Node :" + currentNode);
    //log.info("Label: " + currentNode.label() + "");
    //log.info("Label Value: " + currentNode.label().value() + "");
    //log.info("Preterminal: " + currentNode.isPreTerminal() + "");
    //log.info("Index: " + ((CoreLabel) currentNode.label()).get(CharacterOffsetBeginAnnotation.class) + "");

    if (currentNode.isLeaf()) {
        //the node is a leaf and belongs in the realm of pos tagging
    } else {
        Phrase p = new Text2RDF().createStringAnnotationForClass(Phrase.class, prefix, text,
                getSpan(currentNode), uriGenerator, model);
        List<Tree> children = currentNode.getChildrenAsList();
        for (Tree child : children) {

            /* if (false && child.isPreTerminal()) {
            //skip preterminals
            log.debug("skipping preterminal: "+currentNode);
            log.debug("label: "+currentNode.label());
            child = child.getChild(0);
            Word childTerminal = new Text2RDF().createStringAnnotationForClass(Word.class, prefix, text, getSpan(child), uriGenerator, model);
            p.addChild(childTerminal);
             */
            Phrase childPhrase = new Text2RDF().createStringAnnotationForClass(Phrase.class, prefix, text,
                    getSpan(child), uriGenerator, model);
            p.addChild(childPhrase);
            processTree(child, uriGenerator, prefix, text, model);

            log.info("Current Node :" + currentNode);
            log.info("Label: " + currentNode.label() + "");
            log.info("Label Value: " + currentNode.label().value() + "");
            log.info("Preterminal: " + currentNode.isPreTerminal() + "");
            //log.info("Index: " + ((CoreLabel) currentNode.label()).get(CharacterOffsetBeginAnnotation.class) + "");

            //adding syntax classes from olia and olia-top
            String tag = ((CoreLabel) currentNode.label()).get(CategoryAnnotation.class);
            Set<String> classes = penn_syntax.getClassURIsForTag(tag);
            for (String classUri : classes) {
                log.info("found: " + classUri + " for: " + tag);
                OntModel hierarchy = penn_syntax.getHierarchy(classUri);
                for (ExtendedIterator<OntClass> it = hierarchy.listClasses(); it.hasNext();) {
                    OntClass oc = it.next();
                    p.addOntClass(model.createResource(oc.getURI()));
                }
                //Copy the hierarchy
                model.add(hierarchy);
            }
        }
    }
}

From source file:reactivetechnologies.sentigrade.engine.nlp.SentimentAnalyzer.java

License:Apache License

private static void print(Tree tree) {
    if (LOG.isDebugEnabled()) {
        Tree t = tree.deepCopy();
        //setSentimentLabels(t);
        LOG.debug("leaf?" + t.isLeaf() + " phrrasal?" + t.isPhrasal() + " preterminal?" + t.isPreTerminal()
                + " class:" + RNNCoreAnnotations.getPredictedClass(t));
        LOG.debug("" + t + "");

    }//from w  w w .j av a 2s . c o m

}