我不明白如何从我的Java应用程序加载CoreNLP的Shift-Reduce选区分析器(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
我解压缩了已编译的项目大战,并且不存在“ 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</artifactId>
<version>3.5.2</version>
</dependency>
...
</dependencies>
最后,干净安装:
mvn clean install
如果您继续遇到问题,清除Maven依赖关系可能会有所帮助:
mvn dependency:purge-local-repository
并且不要忘记将下载/安装命令添加到您的项目README /环境引导文件中!
(感谢您的帮助@jah和@GaborAngeli。)