【问题标题】:Stanford parser java error斯坦福解析器java错误
【发布时间】:2015-03-10 19:41:45
【问题描述】:

我正在研究 NLP,我将使用斯坦福解析器从文本中提取名词短语,我使用的解析器版本是 3.4.1 这是我使用的示例代码

package stanfordparser;


import java.util.Collection;
import java.util.List;
import java.io.StringReader;

import edu.stanford.nlp.process.Tokenizer;
import edu.stanford.nlp.process.TokenizerFactory;
import edu.stanford.nlp.process.CoreLabelTokenFactory;
import edu.stanford.nlp.process.DocumentPreprocessor;
import edu.stanford.nlp.process.PTBTokenizer;
import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.ling.HasWord;
import edu.stanford.nlp.ling.Sentence;
import edu.stanford.nlp.trees.*;
import edu.stanford.nlp.parser.lexparser.LexicalizedParser;

class ParserDemo {

  public static void main(String[] args) {
    LexicalizedParser lp = LexicalizedParser.loadModel("edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz");
    if (args.length > 0) {
      demoDP(lp, args[0]);
    } else {
      demoAPI(lp);
    }
  }


  public static void demoDP(LexicalizedParser lp, String filename) {

    TreebankLanguagePack tlp = new PennTreebankLanguagePack();
    GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();

    for (List<HasWord> sentence : new DocumentPreprocessor(filename)) {
      Tree parse = lp.apply(sentence);
      parse.pennPrint();
      System.out.println();

      GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
      Collection tdl = gs.typedDependenciesCCprocessed();
      System.out.println(tdl);
      System.out.println();
    }
  }

  public static void demoAPI(LexicalizedParser lp) {
    // This option shows parsing a list of correctly tokenized words
    String[] sent = { "This", "is", "an", "easy", "sentence", "." };
    List<CoreLabel> rawWords = Sentence.toCoreLabelList(sent);
    Tree parse = lp.apply(rawWords);
    parse.pennPrint();
    System.out.println();

    // This option shows loading and using an explicit tokenizer
    String sent2 = "This is another sentence.";
    TokenizerFactory<CoreLabel> tokenizerFactory =
        PTBTokenizer.factory(new CoreLabelTokenFactory(), "");
    Tokenizer<CoreLabel> tok =
        tokenizerFactory.getTokenizer(new StringReader(sent2));
    List<CoreLabel> rawWords2 = tok.tokenize();
    parse = lp.apply(rawWords2);

    TreebankLanguagePack tlp = new PennTreebankLanguagePack();
    GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
    GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
    List<TypedDependency> tdl = gs.typedDependenciesCCprocessed();
    System.out.println(tdl);
    System.out.println();

    // You can also use a TreePrint object to print trees and dependencies
    TreePrint tp = new TreePrint("penn,typedDependenciesCollapsed");
    tp.printTree(parse);
  }

  private ParserDemo() {} // static methods only

}

但是当我运行这段代码时,我得到了以下错误

java.io.IOException: Unable to resolve "edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz" as either class path, filename or URL
    at edu.stanford.nlp.io.IOUtils.getInputStreamFromURLOrClasspathOrFileSystem(IOUtils.java:446)
    at edu.stanford.nlp.io.IOUtils.readStreamFromString(IOUtils.java:380)
    at edu.stanford.nlp.parser.lexparser.LexicalizedParser.getParserFromSerializedFile(LexicalizedParser.java:628)
    at edu.stanford.nlp.parser.lexparser.LexicalizedParser.getParserFromFile(LexicalizedParser.java:423)
    at edu.stanford.nlp.parser.lexparser.LexicalizedParser.loadModel(LexicalizedParser.java:182)
    at edu.stanford.nlp.parser.lexparser.LexicalizedParser.loadModel(LexicalizedParser.java:161)
    at stanfordparser.ParserDemo.main(ParserDemo.java:29)

我认为是加载模型文件的问题, 任何人都可以帮我解决这个问题吗? 谢谢

更新:(1) 我已经包含了cornlp 模型罐
更新:(2)我正在使用 Netbeans

【问题讨论】:

    标签: java parsing nlp stanford-nlp


    【解决方案1】:

    是的,您没有 CoreNLP 模型 Jar。您可以从这里下载它们-http://nlp.stanford.edu/software/corenlp.shtml#Download

    或者,您可以这样做:

    1. 创建一个 Maven 项目。 (在eclipse中很容易)
    2. 在 pom.xml 文件中,添加这个依赖。

      <dependency> 
        <groupId>edu.stanford.nlp</groupId>
        <artifactId>stanford-corenlp</artifactId>
        <version>3.5.0</version>
      </dependency>
      <dependency>
        <groupId>edu.stanford.nlp</groupId>
        <artifactId>stanford-corenlp</artifactId>
        <version>3.5.0</version>
        <classifier>models</classifier>
      </dependency>
      
    3. 执行 maven clean、maven 更新和 maven 安装。模型文件将自动安装在您的 .m2 文件夹中。

    我希望你知道 maven。如果没有,请发表评论/问题。我们会回答的。

    【讨论】:

    【解决方案2】:

    您需要在类路径中包含 CoreNLP 模型 jar(可从 CoreNLP 主页下载),以便解析器正常工作。

    【讨论】:

    • 我已经包含了cornlp模型jar,请查看更新,我附上了项目的快照
    【解决方案3】:

    正如其他人指出的那样,您必须包含 CORE-NLP 包随附的 jar 文件,该包可在 stanford parser page 获得。

    更具体地说,将这些添加到您的类路径中:stanford-parser-3.4.1-models.jar,stanford-parser-3.4.1-sources.jar,stanford-parser.jar(这些特定于 stanford 的版本您正在使用的解析器,即 3.4.1 版)

    您可以将其添加到类路径中,如下所示:

    对于 Linux:export CLASSPATH=$CLASSPATH:/some_path/stanford-parser-3.4.1-sources.jar:/some_path/stanford-parser-3.4.1-models.jar:/some_path/stanford-parser.jar

    对于 Windows: 设置 CLASSPATH=%CLASSPATH%;\some_path\stanford-parser-3.4.1-models.jar;\some_path\stanford-parser-3.4.1-sources.jar;\some_path\stanford-parser;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多