【问题标题】:Unable to load CoreNLP Shift-Reduce model into CoreNLP jar无法将 CoreNLP Shift-Reduce 模型加载到 CoreNLP jar
【发布时间】:2016-04-19 02:39:24
【问题描述】:

我不明白如何从我的 java 应用程序加载 CoreNLP 的 Shift-Reduce Constituency Parser (SRCP)。

我正在使用 Apache Maven 来管理我的项目的依赖项。根据文档,SRCP 模型未与 CoreNLP 捆绑,因此我已单独下载 stanford-srparser-2014-10-23-models.jar (http://nlp.stanford.edu/software/srparser.shtml) 并将该文件放入:

~/.m2/repository/edu/stanford/nlp/stanford-corenlp/3.5.2/stanford-srparser-2014-10-23-models.jar 

就是和核心依赖jar在同一个目录

~/.m2/repository/edu/stanford/nlp/stanford-corenlp/3.5.2/stanford-corenlp-3.5.2.jar

这是我项目的 pom.xml 的相关部分:

    <dependency>
        <groupId>edu.stanford.nlp</groupId>
        <artifactId>stanford-corenlp</artifactId>
        <version>3.5.2</version>
    </dependency>
    <dependency>
        <groupId>edu.stanford.nlp</groupId>
        <artifactId>stanford-corenlp</artifactId>
        <version>3.5.2</version>
        <classifier>models</classifier>
    </dependency>

编译成功:

mvn clean compile

但是当我尝试加载应用程序时,我收到:

java.lang.reflect.InvocationTargetException
...
Caused by: edu.stanford.nlp.io.RuntimeIOException: java.io.IOException: Unable to resolve "edu/stanford/nlp/models/srparser/englishSR.ser.gz" as either class path, filename or URL

我解压编译好的项目war,“edu/stanford/nlp/models/srparser/englishSR.ser.gz”不存在。

这是我在应用中调用模型的方式:

// Initialize a CoreNLP pipeline
public static Properties props = new Properties();
public static StanfordCoreNLP pipeline;

// Set the CoreNLP pipeline annotators.
props.setProperty("annotators", "tokenize, ssplit, parse, sentiment");
props.setProperty("parse.model", "edu/stanford/nlp/models/srparser/englishSR.ser.gz");
pipeline = new StanfordCoreNLP(props);

如何更新我的 Maven 配置以强制我的 CoreNLP 依赖项包含 srparser 模型?请记住,我需要此配置才能在其他开发人员的环境中运行,因此如果可能,该解决方案应该是干净且可重复使用的。

谢谢!

编辑:

针对@jah 的评论,下面是mvn dependency:tree 的结果。构建成功,但 srparser 模型未编译/存在:

[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ [REDACTED] ---

Downloading:

...

[INFO] com.[REDACTED].nlp:nlp:war:0.1.0
[INFO] +- com.strategicgains:RestExpress:jar:0.11.2:compile
[INFO] |  +- com.strategicgains:RestExpress-Common:jar:0.11.2:compile
[INFO] |  +- com.strategicgains:DateAdapterJ:jar:1.1.4:compile
[INFO] |  +- com.thoughtworks.xstream:xstream:jar:1.4.7:compile
[INFO] |  |  +- xmlpull:xmlpull:jar:1.1.3.1:compile
[INFO] |  |  \- xpp3:xpp3_min:jar:1.1.4c:compile
[INFO] |  +- io.netty:netty-all:jar:4.0.29.Final:compile
[INFO] |  +- org.owasp.encoder:encoder:jar:1.1.1:compile
[INFO] |  \- com.jcraft:jzlib:jar:1.1.3:compile
[INFO] +- junit:junit:jar:4.11:test
[INFO] |  \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] +- edu.stanford.nlp:stanford-corenlp:jar:3.5.2:compile
[INFO] |  +- com.io7m.xom:xom:jar:1.2.10:compile
[INFO] |  |  +- xml-apis:xml-apis:jar:1.3.03:compile
[INFO] |  |  +- xerces:xercesImpl:jar:2.8.0:compile
[INFO] |  |  \- xalan:xalan:jar:2.7.0:compile
[INFO] |  +- joda-time:joda-time:jar:2.1:compile
[INFO] |  +- de.jollyday:jollyday:jar:0.4.7:compile
[INFO] |  |  \- javax.xml.bind:jaxb-api:jar:2.2.7:compile
[INFO] |  +- com.googlecode.efficient-java-matrix-library:ejml:jar:0.23:compile
[INFO] |  \- javax.json:javax.json-api:jar:1.0:compile
[INFO] +- edu.stanford.nlp:stanford-corenlp:jar:models:3.5.2:compile
[INFO] +- org.json:json:jar:20151123:compile
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.6.4:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.6.0:compile
[INFO] |  \- com.fasterxml.jackson.core:jackson-core:jar:2.6.4:compile
[INFO] \- commons-io:commons-io:jar:1.3.2:compile

【问题讨论】:

  • 当你mvn dependency:tree时会发生什么?
  • 有趣的是,maven central 中的该库的这个版本具有非常不同的内容(主要是文本文件)
  • @jah 我用mvn dependency:tree 的结果编辑了我的帖子。 srparser 依赖项不存在。
  • 存在依赖,它是从底部算起的第 6 行。我猜这个罐子也包含在你的战争中。我认为edu/stanford/nlp/models/srparser/englishSR.ser.gz 也包括在内,通过那个罐子。
  • @jah 倒数第6行实际上是指默认的corenlp模型jar,不包括srparser模型。

标签: java maven stanford-nlp


【解决方案1】:

首先,下载 srparser jar 并将其放在您的项目根目录中:http://nlp.stanford.edu/software/stanford-srparser-2014-10-23-models.jar

其次,从项目根目录,执行以下命令,通过Maven安装srparser模型依赖:

mvn install:install-file -Dfile=stanford-srparser-2014-10-23-models.jar -DgroupId=edu.stanford.nlp -DartifactId=stanford-srparser -Dversion=3.5.2 -Dpackaging=jar

注意命令中的自定义 artifactId 和缺少分类器 - 这是为了防止命名空间与其他 CoreNLP 模块混淆。

三、在Maven项目的pom.xml中添加依赖:

<dependencies>
...
    <dependency>
         <groupId>edu.stanford.nlp</groupId>
         <artifactId>stanford-srparser</a‌​rtifactId>
         <version>3.5.2</version>
    </dependency>
...
</dependencies>

最后,全新安装:

mvn clean install

如果您仍然遇到问题,清除 Maven 依赖项可能会有所帮助:

mvn dependency:purge-local-repository

不要忘记将下载/安装命令添加到您的项目 README/环境引导文件中!

(感谢@jah 和@GaborAngeli 的帮助。)

【讨论】:

    【解决方案2】:

    要运行 shift-reduce 解析器,您需要包含 shift-reduce 模型 jar,该 jar 位于:http://nlp.stanford.edu/software/srparser.shtml

    不确定它是否在 maven 上,但似乎不是?

    【讨论】:

    • 谢谢加博。我意识到我需要包含 shift-reduce 模型 jar,但我正在努力弄清楚如何做到这一点。我不相信 jar 在 Maven search.maven.org/#search%7Cga%7C1%7Cg%3A%22edu.stanford.nlp%22> 上。我对 Java 很陌生——下载 srparser jar 后,我应该将它放在哪里,以便在正确的命名空间中编译它,我是否需要将任何依赖项或任务添加到我的 Maven pom.xml 文件中?
    • 一般来说,您只需将其添加到您的类路径中,使用-cp model.jarCLASSPATH="$CLASSPATH:model.jar"。对于 Maven,您可以使用 mvn install 命令将其添加到本地存储库:mkyong.com/maven/…
    • 你说你已经把你的罐子放在了你的本地仓库中,但可能不是以正确的方式。要在本地 reo 中安装 jar,请发送至 installit。 Here 是关于如何做到这一点的描述。然后就可以使用正常的依赖机制了。
    【解决方案3】:

    如果您从放置 stanford-srparser-2014-10-23-models.jar 的文件夹中运行 mvn 命令,那么以下命令应该可以解决问题。

    mvn install:install-file -Dfile=stanford-srparser-2014-10-23-models.jar -DgroupId=edu.stanford.nlp -DartifactId=stanford-corenlp -Dversion=3.5.2 -Dclassifier=models -Dpackaging=jar

    【讨论】:

    • 您的下一个例外将是java.lang.IllegalArgumentException: annotator "parse" requires annotator "pos"
    • 这基本上是正确的。不过有两个问题。首先,此命令将使用 srparser jar 覆盖现有的模型分类器 jar。我修改后的命令:mvn install:install-file -Dfile=stanford-srparser-2014-10-23-models.jar -DgroupId=edu.stanford.nlp -DartifactId=stanford-srparser -Dversion=3.5.2 -Dpackaging=jar。其次,我们需要在我们的 Maven pom.xml 中添加已安装的依赖项:&lt;dependency&gt;&lt;groupId&gt;edu.stanford.nlp&lt;/groupId&gt;&lt;artifactId&gt;stanford-srparser&lt;/artifactId&gt;&lt;version&gt;3.5.2&lt;/version&gt;&lt;/dependency&gt;。感谢您的帮助@jah!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-25
    • 2012-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多