Executing and testing stanford core nlp example

前端 未结 4 1475
南笙
南笙 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:02

    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.

    0 讨论(0)
  • 2020-12-30 09:02

    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
    
    0 讨论(0)
  • 2020-12-30 09:03

    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

    0 讨论(0)
  • 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<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.
    
    0 讨论(0)
提交回复
热议问题