Stanford Java Parser Error

I am working on NLP research, I use the Stanford parser to extract nouns from text, the version of the parser used is 3.4.1, this is an example of the code I used

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

}

but when I run this code, I get the following error:

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)

I think the problem is loading the model file. Can someone help me solve the problem? Thanks

UPDATE: (1) I am already including the cornlp model cup


UPDATE: (2) I am using Netbeans enter image description here

+4
source share
3 answers

Yes, you have no CoreNLP Jar models. Or you can download them here - http://nlp.stanford.edu/software/corenlp.shtml#Download

:

  • Maven. ( )
  • 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>
    
  • maven , maven maven. .m2.

, maven. , /. .

+12

, ​​ CoreNLP ( CoreNLP) .

+1

, jar, CORE-NLP, stanford parser.

, : stanford-parser-3.4.1-models.jar, stanford-parser-3.4.1-sources.jar, stanford-parser.jar( stanford , , .. 3.4.1)

:

Linux: 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: set CLASSPATH =% CLASSPATH%;\some_path\stanford-parser-3.4.1-models.jar;\some_path\stanford-parser-3.4.1-sources.jar;\some_path\stanford-parser;

0

Source: https://habr.com/ru/post/1570939/


All Articles