java - 无法将 CoreNLP Shift-Reduce 模型加载到 CoreNLP jar 中

标签 java maven stanford-nlp

我不明白如何从我的 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

最佳答案

首先,下载 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

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

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

关于java - 无法将 CoreNLP Shift-Reduce 模型加载到 CoreNLP jar 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34778266/

相关文章:

Java 缓慢的 2D 性能 - 调整大小

java - 斯坦福解析器的类型化依赖

python - 使用 Python 中的斯坦福 CoreNLP 进行情感分析

java - 安卓接听来电

java - 当共享 JDBC 缓存存储到位时,将 "numOwners"在 Infinispan 中设置为大于 1 是否有效?

java - 无法在没有 Moskito UI 的情况下找到 Moskito 与 java web 应用程序集成的解决方案

java - MavenError : Failed to execute goal on project: Could not resolve dependencies In Maven Multimodule project

java - 停用 maven-ear-plugin

stanford-nlp - Stanford CoreNLP BasicPipelineExample 不起作用

java - 什么是 "ignorable character in a Java identifier"