当前位置: 首页 > 面试题库 >

斯坦福大学CoreNLP提供NullPointerException

葛安和
2023-03-14
问题内容

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

    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:

for(CoreMap sentence: sentences) {...

问题答案:

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

要执行令牌化,这就足够了

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

document.get(SentencesAnnotation.class);

因此,您遇到了NullPointerException。



 类似资料:
  • 我对这两个软件包做了一些比较,不确定应该朝哪个方向发展。我想简要介绍的是: 命名实体识别(人员、地点、组织等) 据我所知,OpenNLP和Stanford CoreNLP公开了非常相似的功能。然而,斯坦福大学的CoreNLP看起来有更多的活动,而OpenNLP在过去六个月里只有几次提交。 根据我所看到的,OpenNLP似乎更容易训练新的模型,仅仅因为这个原因可能更具吸引力。然而,我的问题是,其他人

  • 我在使用Stanford pipeline(CoreNLP的最后一个版本)解析BNC时遇到了一个问题。 解析器只是停留在这个句子中,它甚至不会抛出错误。句子在web界面中得到正确的解析。 我尝试了标记器的选项,但没有结果。 我添加了我正在使用的命令行:java[...]edu.stanford.nlp.pipeline.stanfordCorenlp-注释器tokenize,ssplit,pos,

  • 我假设我试图将输出转换成的格式是旧版本的CorenLP的默认输出。有什么方法可以得到所需格式的输出吗?

  • 试图运行示例,但我一直无法打开“english-left3words-distsim.tagger”文件可能丢失。文件没有丢失,目录指向模型jar文件的位置,路径:edu\stanford\nlp\模型\pos-tagger\english-left3word在jar文件中是正确的。 我使用3.7.0,安装从nuget在视觉工作室2015. 代码如下: 我确实在Stack上看到了一个类似的问题,他

  • 这是意料之中的行为吗?我在前面运行完整的管道吗?

  • 我正在注释和分析一系列文本文件。 pipeline.annotate方法每次读取文件时都会变得越来越慢。最终,我得到了一个OutOfMemoryError。 管道初始化一次: 然后,我使用相同的管道实例处理每个文件(如SO和斯坦福大学在其他地方推荐的)。 明确地说,我希望问题出在我的配置上。但是,我确信失速和内存问题发生在pipeline.annotate(file)方法上。 在处理每个文件后,我