斯坦福CoreNLP - 单词的Leematisation

时间:2015-06-16 07:56:03

标签: java intellij-idea nlp stanford-nlp

我正在尝试使用以下代码

使用Stanford NLP来句子化句子
import java.util.*;
import edu.stanford.nlp.pipeline.*;
import edu.stanford.nlp.ling.*;
import edu.stanford.nlp.ling.CoreAnnotations.*;
import edu.stanford.nlp.util.CoreMap;

public class Entry
{
    public static void main(String[] args)
    {
        Properties props = new Properties();
        props.put("annotators", "tokenize, ssplit, pos, lemma");
        StanfordCoreNLP pipeline;
        pipeline = new StanfordCoreNLP(props, false);
        String text = "This is my text";
        Annotation document = pipeline.process(text);

        for(CoreMap sentence: document.get(SentencesAnnotation.class))
        {
            for(CoreLabel token: sentence.get(TokensAnnotation.class))
            {
                String word = token.get(TextAnnotation.class);
                String lemma = token.get(LemmaAnnotation.class);
                System.out.println("lemmatized version :" + lemma);
            }
        }
    }
}

编译器抛出以下错误

Exception in thread "main" java.lang.RuntimeException: edu.stanford.nlp.io.RuntimeIOException : Unrecoverable error while loading a tagger model
at edu.stanford.nlp.pipeline.StanfordCoreNLP$4.create(StanfordCoreNLP.java:558)
at edu.stanford.nlp.pipeline.AnnotatorPool.get(AnnotatorPool.java:85)
at edu.stanford.nlp.pipeline.StanfordCoreNLP.construct(StanfordCoreNLP.java:267)
at edu.stanford.nlp.pipeline.StanfordCoreNLP.<init>(StanfordCoreNLP.java:129)
at Entry.main(Entry.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by : edu.stanford.nlp.io.RuntimeIOException : Unrecoverable error while loading a tagger model
at edu.stanford.nlp.tagger.maxent.MaxentTagger.readModelAndInit(MaxentTagger.java:763)
at edu.stanford.nlp.tagger.maxent.MaxentTagger.<init>(MaxentTagger.java:294)
at edu.stanford.nlp.tagger.maxent.MaxentTagger.<init>(MaxentTagger.java:259)
at edu.stanford.nlp.pipeline.POSTaggerAnnotator.loadModel(POSTaggerAnnotator.java:97)
at edu.stanford.nlp.pipeline.POSTaggerAnnotator.<init>(POSTaggerAnnotator.java:77)
at edu.stanford.nlp.pipeline.StanfordCoreNLP$4.create(StanfordCoreNLP.java:556)
... 9 more
Caused by : java.io.IOException : Unable to resolve "edu/stanford/nlp/models/pos-tagger/english-left3words/english-left3words-distsim.tagger" as either class path, filename or URL
at edu.stanford.nlp.io.IOUtils.getInputStreamFromURLOrClasspathOrFileSystem(IOUtils.java:446)
at edu.stanford.nlp.tagger.maxent.MaxentTagger.readModelAndInit(MaxentTagger.java:758)
... 14 more

我下载了CoreNLP库的jar文件,并且我使用了Idea IntelliJ。 无法理解错误,任何人都不知道?

1 个答案:

答案 0 :(得分:0)

您可能错过了* models.jar。除非内存是一个主要问题,否则我发现将完整的corenlp软件包编译成一个jar(包括模型)是部署StanfordNLP的最简单方法。

万无一失的方法是克隆git repo并构建jar:

git clone https://github.com/stanfordnlp/CoreNLP.git
cd CoreNLP
ant jar

然后,无论何时编译并运行,请确保包含生成的jar:

javac -cp /path/to/javanlp-core.jar my.java
编辑:我发现最简单的方式很可能是用maven构建的,并且包含Stanford NLP作为依赖项,但是如果你正在寻找一个更轻量级的选项,那么这个的工作原理。