6

I am working on a research about NLP, i woul to use Stanford parser to extract noun phrases from text, the parser version i used is 3.4.1 this is the sample 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 in the loading of the model file, Could any one help me to solve the problem? Thanks

UPDATE:(1) I am already includes the cornlp model jar
UPDATE:(2) I am using Netbeans enter image description here

3 Answers 3

18

Yes, You do not have CoreNLP models Jar. Either you can download them from here- http://nlp.stanford.edu/software/corenlp.shtml#Download

or, you can do this:

  1. Create a Maven project. ( It is easy in eclipse)
  2. In the pom.xml file, add this dependency.

    <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. Do maven clean, maven update and maven install. The model files will be installed in your .m2 folder automatically.

I hope you know maven. If not, please post a comment / question. We will answer.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, i try to run the project in eclips and it's works fine, but i need it to run under netbens, how can i add the pom.xml in netbeans
You can check this: platform.netbeans.org/tutorials/nbm-maven-quickstart.html. If the answer helped, please upvote and click on the tick mark to make it accepted answer. Thanks :)
This is another place to check for the question: stackoverflow.com/q/6819317/1930402
1

You need to have the CoreNLP models jar (downloadable from the CoreNLP homepage) on your classpath for the parser to work properly.

1 Comment

I am already includes the cornlp model jar, see update please i attached a snapshot of the project
0

As others have pointed out you have to include the jar files that come along with the CORE-NLP package that is avalaible at the stanford parser page.

More specifically add these to your class path : stanford-parser-3.4.1-models.jar,stanford-parser-3.4.1-sources.jar,stanford-parser.jar ( these are specific to the version of stanford parser you are using i.e version 3.4.1 )

You can add it to the class path as follows :

For 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

For 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;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.