斯坦福大学CoreNLP提供NullPointerException [英] Stanford CoreNLP gives NullPointerException

查看:103
本文介绍了斯坦福大学CoreNLP提供NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力让我了解Stanford CoreNLP API.我希望得到一个简单的句子,使用以下代码将其标记化:

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 Properties();
    props.put("annotators", "tokenize");
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props);

    // read some text in the text variable
    String text = "I wish this code would run.";

    // create an empty Annotation just with the given text
    Annotation document = new Annotation(text);

    // run all Annotators on this text
    pipeline.annotate(document);

    // these are all the sentences in this document
    // a CoreMap is essentially a Map that uses class objects as keys and has values with custom types
    List<CoreMap> sentences = document.get(SentencesAnnotation.class);

    for(CoreMap sentence: sentences) {
        // traversing the words in the current sentence
        // a CoreLabel is a CoreMap with additional token-specific methods
        for (CoreLabel token: sentence.get(TokensAnnotation.class)) {
            // this is the text of the token
            String word = token.get(TextAnnotation.class);
            // this is the POS tag of the token
            String pos = token.get(PartOfSpeechAnnotation.class);
            // this is the NER label of the token
            String ne = token.get(NamedEntityTagAnnotation.class);       
        }

        // this is the parse tree of the current sentence
        Tree tree = sentence.get(TreeAnnotation.class);

        // this is the Stanford dependency graph of the current sentence
        SemanticGraph dependencies = sentence.get(CollapsedCCProcessedDependenciesAnnotation.class);
    }

    // This is the coreference link graph
    // Each chain stores a set of mentions that link to each other,
    // along with a method for getting the most representative mention
    // Both sentence and token offsets start at 1!
    Map<Integer, CorefChain> graph = document.get(CorefChainAnnotation.class);

这是从Stanford NLP网站本身上摘下来的,所以我希望它能开箱即用.遗憾的是没有,因为它在以下位置给了我NullPointerException:

This is picked off from the Stanford NLP website itself, so I hoped it worked out of the box. Sadly it doesn't since it gives me a NullPointerException at:

for(CoreMap sentence: sentences) {...

推荐答案

您从Stanford NLP网站上获得的代码对text变量执行所有注释.为了执行特定的注释,您必须相应地更改代码.

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);
}

如果注释器不包含Sentence Splitter("ssplit"),则此行代码将返回Null

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

document.get(SentencesAnnotation.class);

所以您遇到了NullPointerException.

And so you were encountering NullPointerException.

这篇关于斯坦福大学CoreNLP提供NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆