Executing and testing stanford core nlp example

前端 未结 4 1499
南笙
南笙 2020-12-30 08:46

I downloaded stanford core nlp packages and tried to test it on my machine.

Using command: java -cp \"*\" -mx1g edu.stanford.nlp.sentiment.SentimentPipeline -f

4条回答
  •  独厮守ぢ
    2020-12-30 09:13

    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 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.
    

提交回复
热议问题