I am trying to test the new Stanford Dependency parser which works with Neural Networks. I am trying to run the demos which are included in the zip file. The files ParserDemo.java
and ParserDemo2.java
work fine. However the file DependencyParserDemo.java
:
import edu.stanford.nlp.ling.HasWord;
import edu.stanford.nlp.ling.TaggedWord;
import edu.stanford.nlp.parser.nndep.DependencyParser;
import edu.stanford.nlp.process.DocumentPreprocessor;
import edu.stanford.nlp.tagger.maxent.MaxentTagger;
import edu.stanford.nlp.trees.GrammaticalStructure;
import java.io.StringReader;
import java.util.List;
/**
* Demonstrates how to first use the tagger, then use the NN dependency
* parser. Note that the parser will not work on untagged text.
*
* @author Jon Gauthier
*/
public class DependencyParserDemo {
public static void main(String[] args) {
String modelPath = DependencyParser.DEFAULT_MODEL;
String taggerPath = "edu/stanford/nlp/models/pos-tagger/english-left3words/english-left3words-distsim.tagger";
for (int argIndex = 0; argIndex < args.length; ) {
switch (args[argIndex]) {
case "-tagger":
taggerPath = args[argIndex + 1];
argIndex += 2;
break;
case "-model":
modelPath = args[argIndex + 1];
argIndex += 2;
break;
default:
throw new RuntimeException("Unknown argument " + args[argIndex]);
}
}
String text = "I can almost always tell when movies use fake dinosaurs.";
MaxentTagger tagger = new MaxentTagger(taggerPath);
DependencyParser parser = DependencyParser.loadFromModelFile(modelPath);
DocumentPreprocessor tokenizer = new DocumentPreprocessor(new StringReader(text));
for (List<HasWord> sentence : tokenizer) {
List<TaggedWord> tagged = tagger.tagSentence(sentence);
GrammaticalStructure gs = parser.predict(tagged);
// Print typed dependencies
System.err.println(gs);
}
}
}
Throws an error:
Exception in thread "main" edu.stanford.nlp.io.RuntimeIOException: Unrecoverable error while loading a tagger model
at edu.stanford.nlp.tagger.maxent.MaxentTagger.readModelAndInit(MaxentTagger.java:769)
at edu.stanford.nlp.tagger.maxent.MaxentTagger.<init>(MaxentTagger.java:297)
at edu.stanford.nlp.tagger.maxent.MaxentTagger.<init>(MaxentTagger.java:262)
at DependencyParserDemo.main(DependencyParserDemo.java:40)
Caused by: java.io.IOException: Unable to resolve "edu/stanford/nlp/models/pos-tagger/english-left3words/english-left3words-distsim.tagger" as either class path, filename or URL
at edu.stanford.nlp.io.IOUtils.getInputStreamFromURLOrClasspathOrFileSystem(IOUtils.java:448)
at edu.stanford.nlp.tagger.maxent.MaxentTagger.readModelAndInit(MaxentTagger.java:764)
... 3 more
Could someone tell me what am I doing wrong?
It seems like your path to the file "english-left3words-distsim.tagger" is not correct. Check if path you provide is correct. You can also try it with absolute path.
I solved it with absolute path. I first created the following folders in the src folders: edu/stanford/nlp/models/pos-tagger/english-left3words/ and inside I pasted the file english-left3words-distsim.tagger (which is in the POS-tagger file stanford-postagger-full-2015-04-20.zip). After this - it worked.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.