较小的 Stanford NLP 模型 Jar 文件

A smaller Stanford NLP Models Jar file

我目前正在将这个 JAR 文件用于斯坦福 NLP 模型:stanford-corenlp-3.5.2-models.jar

这个文件很大:大约 340 MB。

我只使用了 4 个模型:tokenizessplitparselemma。有什么方法可以使用较小的模型 JAR 文件(或者每个模型都有一个 JAR 文件),因为我绝对需要这个文件的大小尽可能小

如果您只在类路径中包含解析器的模型文件和 pos 标记器的模型文件,您应该没问题。 "lemma" 需要 "pos" ,因此您需要将其包含在注释者列表中。

例如:"edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz" 和 "edu/stanford/nlp/models/pos-tagger/english-left3words/english-left3words-distsim.tagger" 应该就是您所需要的。

您可以只创建该目录结构并将这些文件包含在您的类路径中,或者制作一个只包含这些文件的 jar。你绝对可以切掉那个罐子的大部分。

最重要的是,如果您遗漏了某些内容,您的代码将因缺少资源错误而崩溃。所以你只需要继续添加文件,直到代码停止崩溃。你绝对不需要那个罐子里的很多文件。

按照@StanfordNLPHelp 提到的类似方法,我使用了 maven-shade-plugin 并减小了最终编译的 jar 文件的大小。您需要更改 "Package.MainClass" 和 includes 标签或添加 excludes 标签

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.1.0</version>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>shade</goal>
                </goals>
                <configuration>
                    <transformers>
                        <!-- adding Main-Class to manifest file -->
                        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                            <mainClass>Package.MainClass</mainClass>
                        </transformer>
                    </transformers>
                    <minimizeJar>true</minimizeJar>
                    <filters>
                        <filter>
                            <artifact>edu.stanford.nlp:stanford-corenlp</artifact>
                            <includes>
                                <include>**</include>
                            </includes>
                        </filter>
                        <filter>
                            <artifact>edu.stanford.nlp:stanford-corenlp:models</artifact>
                            <includes>
                                <include>edu/stanford/nlp/models/pos-tagger/**</include>
                            </includes>
                        </filter>
                    </filters>
                </configuration>
            </execution>
        </executions>
    </plugin>

根据 StanfordNLPHelp 的建议,我这样做了(我使用 Gradle):

  • 从以下位置下载了 CoreNLP: Stanford CoreNLP download

  • 解压 stanford-corenlp-X-models.jar

  • 去/edu/Stanford/nlp/models

  • 删除不相关的文件夹。不幸的是,这有点猜测和检查

  • 解压缩文件夹并转成jar(我只是改了扩展名,可能有点不爽)

  • 将 libs 文件夹添加到我的 gradle 项目。/app/libs

  • 将下载的 stanford-corenlp-x.jar 和上面制作的新 jar 移动

  • 在build.gradle中添加

      implementation files('libs/stanford-corenlp-4.4.0.jar')
      implementation files('libs/stanford-corenlp-4.4.0-models.jar')
    
  • 运行 gradle 构建。如果出现错误,则说明您删除了一个重要文件。还原和重新压缩,等等。