0

I can not make Stanford Parser Version 3.5.1 work. I know that newer versions of this tool are available but I have tons of old code using this particular version. This is for an academic course.

I am using Windows 7, JDK 1.8.0_65, python 3.3.3 and NLTK 3.0.2

My environment variables are as follows:

  • CLASSPATH : C:\Program Files (x86)\stanford-parser-full-2015-01-30\jars\stanford-parser-3.5.1-models.jar;C:\Program Files (x86)\stanford-parser-full-2015-01-30\jars\stanford-parser-3.5.1-sources.jar;C:\Program Files (x86)\stanford-parser-full-2015-01-30\jars\stanford-parser.jar
  • JAVA_HOME : C:\Program Files\Java\jdk1.8.0_65\bin
  • Path : C:\ProgramData\Oracle\Java\javapath;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Common Files\Apple\Internet Services\;C:\Program Files\Git\cmd;C:\Program Files (x86)\stanford-parser-full-2015-01-30\jars\

I run this code:

from nltk.parse import stanford

parser = stanford.StanfordParser(model_path='C:\Program Files (x86)\stanford-parser-full-2015-01-30\edu\stanford\lp\models\lexparser\englishPCFG.ser.gz')

parser.raw_parse('I love apples')

And I am getting this error

Loading parser from serialized file C:\Program Files (x86)\stanford-parser-full-2015-01-30\edu\stanford\lp\models\lexparser\englishPCFG.ser.gz ...

java.io.IOException: Unable to resolve "C:\Program Files (x86)\stanford-parser-full-2015-01-30\edu\stanford\lp\models\lexparser\englishPCFG.ser.gz" as either class path, filename or URL

at edu.stanford.nlp.io.IOUtils.getInputStreamFromURLOrClasspathOrFileSystem(IOUtils.java:463)

at edu.stanford.nlp.io.IOUtils.readStreamFromString(IOUtils.java:396)

at edu.stanford.nlp.parser.lexparser.LexicalizedParser.getParserFromSerializedFile(LexicalizedParser.java:599)

at edu.stanford.nlp.parser.lexparser.LexicalizedParser.getParserFromFile(LexicalizedParser.java:394)

at edu.stanford.nlp.parser.lexparser.LexicalizedParser.loadModel(LexicalizedParser.java:181)

at edu.stanford.nlp.parser.lexparser.LexicalizedParser.main(LexicalizedParser.java:1395)

Loading parser from text file C:\Program Files (x86)\stanford-parser-full-2015-01-30\edu\stanford\lp\models\lexparser\englishPCFG.ser.gz java.io.IOException: Unable to resolve "C:\Program Files (x86)\stanford-parser-full-2015-01-30\edu\stanford\lp\models\lexparser\englishPCFG.ser.gz" as either class path, filename or URL

at edu.stanford.nlp.io.IOUtils.getInputStreamFromURLOrClasspathOrFileSystem(IOUtils.java:463)

at edu.stanford.nlp.io.IOUtils.readerFromString(IOUtils.java:591)

at edu.stanford.nlp.parser.lexparser.LexicalizedParser.getParserFromTextFile(LexicalizedParser.java:533)

at edu.stanford.nlp.parser.lexparser.LexicalizedParser.getParserFromFile(LexicalizedParser.java:396)

at edu.stanford.nlp.parser.lexparser.LexicalizedParser.loadModel(LexicalizedParser.java:181)

at edu.stanford.nlp.parser.lexparser.LexicalizedParser.main(LexicalizedParser.java:1395)

Exception in thread "main" java.lang.NullPointerException

at edu.stanford.nlp.parser.lexparser.LexicalizedParser.loadModel(LexicalizedParser.java:183)

at edu.stanford.nlp.parser.lexparser.LexicalizedParser.main(LexicalizedParser.java:1395)

Traceback (most recent call last): File "C:\Users\Zimtyth\Desktop\PFE\Implémentation\Codes\Code final\Lib_Stanford_Parser.py", line 100, in resultat = parse_sent("My name is Melroy and i want to win.") File "C:\Users\Zimtyth\Desktop\PFE\Implémentation\Codes\Code final\Lib_Stanford_Parser.py", line 10, in parse_sent return parser.raw_parse(sent) File "C:\Python33\lib\site-packages\nltk\parse\stanford.py", line 152, in raw_parse return next(self.raw_parse_sents([sentence], verbose)) File "C:\Python33\lib\site-packages\nltk\parse\stanford.py", line 170, in raw_parse_sents return self._parse_trees_output(self._execute(cmd, '\n'.join(sentences), verbose)) File "C:\Python33\lib\site-packages\nltk\parse\stanford.py", line 230, in _execute stdout=PIPE, stderr=PIPE) File "C:\Python33\lib\site-packages\nltk\internals.py", line 161, in java raise OSError('Java command failed : ' + str(cmd)) OSError: Java command failed : ['C:\Program Files\Java\jdk1.8.0_65\bin\java.exe', '-mx1000m', '-cp', 'C:\Program Files (x86)\stanford-parser-full-2015-01-30\jars\stanford-parser.jar;C:\Program Files (x86)\stanford-parser-full-2015-01-30\jars\stanford-parser-3.5.1-models.jar', 'edu.stanford.nlp.parser.lexparser.LexicalizedParser', '-model', 'C:\Program Files (x86)\stanford-parser-full-2015-01-30\edu\stanford\lp\models\lexparser\englishPCFG.ser.gz', '-sentences', 'newline', '-outputFormat', 'penn', '-encoding', 'utf8', 'c:\users\zimtyth\appdata\local\temp\tmpbf5zdg']

I have already checked a couple of answers in SO about this like this but still I could not make it work. It looks like a Java problem, please tell me what am I doing wrong here?

halfer
  • 19,824
  • 17
  • 99
  • 186
ziMtyth
  • 1,008
  • 16
  • 32

1 Answers1

0

I would guess that your code isn't right and you don't have any file at the location 'C:\Program Files (x86)\stanford-parser-full-2015-01-30\edu\stanford\lp\models\lexparser\englishPCFG.ser.gz'.

If the models jar file is on your classpath, you should be able to get things to work by giving the model path (inside the jar file):

parser = stanford.StanfordParser(model_path='edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz')

The other choice is to expand the jar file (jar -xf) and then you would have an englishPCFG.ser.gz file, which you could as a parameter to the model_path.

p.s. For other people reading this in 2019+: You shouldn't actually be using this NLTK package at all any more, but rather: nltk.parse.corenlp.CoreNLPParser

Christopher Manning
  • 9,360
  • 34
  • 46