Executing and testing stanford core nlp example

两盒软妹~` 提交于 2019-11-30 07:01:28

You need to add the "sentiment" annotator to the list of annotators:

-annotators tokenize,ssplit,pos,lemma,parse,sentiment

This will add a "sentiment" property to each sentence node in your XML.

saganas

You can do the following in your code:

String text = "I am feeling very sad and frustrated.";
Properties props = new Properties();
props.setProperty("annotators", "tokenize, ssplit, pos, lemma, parse, sentiment");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
<...>
Annotation annotation = pipeline.process(text);
List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);
for (CoreMap sentence : sentences) {
  String sentiment = sentence.get(SentimentCoreAnnotations.SentimentClass.class);
  System.out.println(sentiment + "\t" + sentence);
}

It will print the sentiment of the sentence and the sentence itself, e.g. "I am feeling very sad and frustrated.":

Negative    I am feeling very sad and frustrated.

Per the example here you need to run the Sentiment Analysis.

java -cp "*" -mx5g edu.stanford.nlp.sentiment.SentimentPipeline -file input.txt

Apparently this is a memory expensive operation, it may not complete with only 1 gigabyte. Then you can use the "Evaluation Tool"

java -cp "*" edu.stanford.nlp.sentiment.Evaluate edu/stanford/nlp/models/sentiment/sentiment.ser.gz input.txt

This is working fine for me -

Maven Dependencies :

        <dependency>
            <groupId>edu.stanford.nlp</groupId>
            <artifactId>stanford-corenlp</artifactId>
            <version>3.5.2</version>
            <classifier>models</classifier>
        </dependency>
        <dependency>
            <groupId>edu.stanford.nlp</groupId>
            <artifactId>stanford-corenlp</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>edu.stanford.nlp</groupId>
            <artifactId>stanford-parser</artifactId>
            <version>3.5.2</version>
        </dependency>

Java Code :

public static void main(String[] args) throws IOException {
        String text = "This World is an amazing place";
        Properties props = new Properties();
        props.setProperty("annotators", "tokenize, ssplit, pos, lemma, parse, sentiment");
        StanfordCoreNLP pipeline = new StanfordCoreNLP(props);

        Annotation annotation = pipeline.process(text);
        List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);
        for (CoreMap sentence : sentences) {
            String sentiment = sentence.get(SentimentCoreAnnotations.SentimentClass.class);
            System.out.println(sentiment + "\t" + sentence);
        }
    }

Results :

Very positive This World is an amazing place

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!