Stanford CoreNLP gives NullPointerException

后端 未结 2 727
一整个雨季
一整个雨季 2021-01-16 14:26

I\'m trying to get my head around the Stanford CoreNLP API. I wish to get a simple sentence to be tokenized using following code:

    Properties props = new          


        
相关标签:
2条回答
  • 2021-01-16 15:05

    This line retrieves sentence annotations.

    List<CoreMap> sentences = document.get(SentencesAnnotation.class);
    

    But your pipeline contains only the tokenizer, not the sentence splitter.

    Change the following line:

        props.put("annotators", "tokenize, ssplit"); // add sentence splitter
    
    0 讨论(0)
  • 2021-01-16 15:11

    The code you have picked up from Stanford NLP website performs all the annotations on the text variable. In order to perform specific annotations you have to change the code accordingly.

    To perform tokenization, this would be sufficient

    Properties props = new Properties();
    props.put("annotators", "tokenize");
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
    
    Annotation document = new Annotation(text);
    pipeline.annotate(document);
    for (CoreLabel token: document.get(TokensAnnotation.class)) {
        String word = token.get(TextAnnotation.class);
    }
    

    This line of code would return Null if annotators doesn't include Sentence Splitter("ssplit")

    document.get(SentencesAnnotation.class);
    

    And so you were encountering NullPointerException.

    0 讨论(0)
提交回复
热议问题