Example usage for edu.stanford.nlp.util Generics newArrayList

List of usage examples for edu.stanford.nlp.util Generics newArrayList

Introduction

In this page you can find the example usage for edu.stanford.nlp.util Generics newArrayList.

Prototype

public static <E> ArrayList<E> newArrayList() 

Source Link

Usage

From source file:CollapseUnaryTransformer.java

License:Apache License

public Tree transformTree(Tree tree) {
    if (tree.isPreTerminal() || tree.isLeaf()) {
        return tree.deepCopy();
    }//  w w  w  . j a va2s.  c om

    Label label = tree.label().labelFactory().newLabel(tree.label());
    Tree[] children = tree.children();
    while (children.length == 1 && !children[0].isLeaf()) {
        children = children[0].children();
    }
    List<Tree> processedChildren = Generics.newArrayList();
    for (Tree child : children) {
        processedChildren.add(transformTree(child));
    }
    return tree.treeFactory().newTreeNode(label, processedChildren);
}

From source file:opennlp.tools.parse_thicket.opinion_processor.DefaultSentimentProcessor.java

License:Apache License

/**
 * Reads an annotation from the given filename using the requested input.
 *//*from www . ja va 2s.c  o  m*/
public static List<Annotation> getAnnotations(StanfordCoreNLP tokenizer, Input inputFormat, String filename,
        boolean filterUnknown) {
    switch (inputFormat) {
    case TEXT: {
        String text = IOUtils.slurpFileNoExceptions(filename);
        Annotation annotation = new Annotation(text);
        tokenizer.annotate(annotation);
        List<Annotation> annotations = Generics.newArrayList();
        for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
            Annotation nextAnnotation = new Annotation(sentence.get(CoreAnnotations.TextAnnotation.class));
            nextAnnotation.set(CoreAnnotations.SentencesAnnotation.class, Collections.singletonList(sentence));
            annotations.add(nextAnnotation);
        }
        return annotations;
    }
    case TREES: {
        List<Tree> trees;
        if (filterUnknown) {
            trees = SentimentUtils.readTreesWithGoldLabels(filename);
            trees = SentimentUtils.filterUnknownRoots(trees);
        } else {
            trees = Generics.newArrayList();
            MemoryTreebank treebank = new MemoryTreebank("utf-8");
            treebank.loadPath(filename, null);
            for (Tree tree : treebank) {
                trees.add(tree);
            }
        }

        List<Annotation> annotations = Generics.newArrayList();
        for (Tree tree : trees) {
            CoreMap sentence = new Annotation(listToString(tree.yield()));
            sentence.set(TreeCoreAnnotations.TreeAnnotation.class, tree);
            List<CoreMap> sentences = Collections.singletonList(sentence);
            Annotation annotation = new Annotation("");
            annotation.set(CoreAnnotations.SentencesAnnotation.class, sentences);
            annotations.add(annotation);
        }
        return annotations;
    }
    default:
        throw new IllegalArgumentException("Unknown format " + inputFormat);
    }
}

From source file:startupistanbul.kodinghackathon.service.SentimentService.java

/**
 * Analyze a tweet and return its sentiment
 *
 * @param tweet//  w  w  w.  j a v a  2s  . c o  m
 * @return 1 if positive -1 if negative 0 if neutral
 */
public Integer analyzeTweet(String tweet) {
    Annotation annotation = new Annotation(tweet);
    tokenizer.annotate(annotation);

    List<Annotation> annotations = Generics.newArrayList();
    for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
        Annotation nextAnnotation = new Annotation(sentence.get(CoreAnnotations.TextAnnotation.class));
        nextAnnotation.set(CoreAnnotations.SentencesAnnotation.class, Collections.singletonList(sentence));
        annotations.add(nextAnnotation);
    }

    //the total sentiment calculated by counting individual sentences in the tweet
    int totalSentiment = 0;

    //annotate all sentences one by one
    for (Annotation ann : annotations) {
        pipeline.annotate(ann);

        for (CoreMap sentence : ann.get(CoreAnnotations.SentencesAnnotation.class)) {
            //System.out.println(sentence);
            //System.out.println(sentence.get(SentimentCoreAnnotations.ClassName.class));

            String sentenceSentiment = sentence.get(SentimentCoreAnnotations.ClassName.class);
            if (sentenceSentiment.equals("Negative") || sentenceSentiment.equals("Very negative")) {
                totalSentiment--;
            } else if (sentenceSentiment.equals("Positive") || sentenceSentiment.equals("Very positive")) {
                totalSentiment++;
            }
        }
    }

    return totalSentiment > 0 ? 1 : totalSentiment < 0 ? -1 : 0;
}