NLTK - >使用斯坦福依赖性解析器 - >

时间:2018-06-15 21:41:11

标签: java python nltk stanford-nlp

对于NLTK似乎有很多矛盾的文档(NLTK / StanfordNLP文档的权威来源在哪里?)。

我的问题:从nltk调用StanfordParser的首选方法是什么?这是我的代码,但在java调用中有些不正确。

from nltk.parse.stanford import StanfordDependencyParser
import os

parser_home = '/Users/myname/Documents/nlp/stanford-parser-full-2018-02-27/'

# os.environ['CLASSPATH'] = parser_home

parser = StanfordDependencyParser(
    model_path = parser_home + 'stanford-parser.jar',
    path_to_models_jar = parser_home +  'stanford-parser-3.9.1-models.jar',
    verbose = True
)

result = parser.raw_parse('Here is an example sentence.')

这是我的错误。任何帮助赞赏。我还没找到与我完全匹配的东西。我设置了类路径,但我不确定是否需要。

[Found stanford-parser\.jar: /Users/myname/Documents/nlp/stanford-parser-full-2018-02-27/stanford-parser.jar]
[Found stanford-parser-(\d+)(\.(\d+))+-models\.jar: /Users/myname/Documents/nlp/stanford-parser-full-2018-02-27/stanford-parser-3.9.1-models.jar]
/Users/myname/anaconda3/envs/nlp/lib/python3.6/site-packages/ipykernel_launcher.py:12: DeprecationWarning: The StanfordDependencyParser will be deprecated
Please use nltk.parse.corenlp.StanforCoreNLPDependencyParser instead.
  if sys.path[0] == '':

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.lang.RuntimeException: /Users/myname/Documents/nlp/stanford-parser-full-2018-02-27/stanford-parser.jar: expecting BEGIN block; got PK��aL    META-INF/��PKPK��aLMETA-INF/MANIFEST.MFE��
    at edu.stanford.nlp.parser.lexparser.LexicalizedParser.confirmBeginBlock(LexicalizedParser.java:536)
    at edu.stanford.nlp.parser.lexparser.LexicalizedParser.getParserFromTextFile(LexicalizedParser.java:546)
    at edu.stanford.nlp.parser.lexparser.LexicalizedParser.getParserFromFile(LexicalizedParser.java:406)
    at edu.stanford.nlp.parser.lexparser.LexicalizedParser.loadModel(LexicalizedParser.java:186)
    at edu.stanford.nlp.parser.lexparser.LexicalizedParser.main(LexicalizedParser.java:1400)

---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-18-052e46a6f6aa> in <module>()
----> 1 result = parser.raw_parse('Here is an example sentence.')

~/anaconda3/envs/nlp/lib/python3.6/site-packages/nltk/parse/stanford.py in raw_parse(self, sentence, verbose)
    132         :rtype: iter(Tree)
    133         """
--> 134         return next(self.raw_parse_sents([sentence], verbose))
    135 
    136     def raw_parse_sents(self, sentences, verbose=False):

~/anaconda3/envs/nlp/lib/python3.6/site-packages/nltk/parse/stanford.py in raw_parse_sents(self, sentences, verbose)
    150             '-outputFormat', self._OUTPUT_FORMAT,
    151         ]
--> 152         return self._parse_trees_output(self._execute(cmd, '\n'.join(sentences), verbose))
    153 
    154     def tagged_parse(self, sentence, verbose=False):

~/anaconda3/envs/nlp/lib/python3.6/site-packages/nltk/parse/stanford.py in _execute(self, cmd, input_, verbose)
    216                 cmd.append(input_file.name)
    217                 stdout, stderr = java(cmd, classpath=self._classpath,
--> 218                                       stdout=PIPE, stderr=PIPE)
    219 
    220             stdout = stdout.replace(b'\xc2\xa0', b' ')

~/anaconda3/envs/nlp/lib/python3.6/site-packages/nltk/__init__.py in java(cmd, classpath, stdin, stdout, stderr, blocking)
    134     if p.returncode != 0:
    135         print(_decode_stdoutdata(stderr))
--> 136         raise OSError('Java command failed : ' + str(cmd))
    137 
    138     return (stdout, stderr)

OSError: Java command failed : ['/usr/bin/java', '-mx1000m', '-cp', '/Users/myname/Documents/nlp/stanford-parser-full-2018-02-27/stanford-parser-3.9.1-models.jar:/Users/myname/Documents/nlp/stanford-parser-full-2018-02-27/stanford-parser-3.9.1-javadoc.jar:/Users/myname/Documents/nlp/stanford-parser-full-2018-02-27/ejml-0.23.jar:/Users/myname/Documents/nlp/stanford-parser-full-2018-02-27/stanford-parser-3.9.1-sources.jar:/Users/myname/Documents/nlp/stanford-parser-full-2018-02-27/slf4j-api.jar:/Users/myname/Documents/nlp/stanford-parser-full-2018-02-27/stanford-parser-3.9.1-models.jar:/Users/myname/Documents/nlp/stanford-parser-full-2018-02-27/stanford-parser.jar:/Users/myname/Documents/nlp/stanford-parser-full-2018-02-27/slf4j-api-1.7.12-sources.jar', 'edu.stanford.nlp.parser.lexparser.LexicalizedParser', '-model', '/Users/myname/Documents/nlp/stanford-parser-full-2018-02-27/stanford-parser.jar', '-sentences', 'newline', '-outputFormat', 'conll2007', '-encoding', 'utf8', '/var/folders/kg/y1g8nszj77z0pm6mzplqv7580000gp/T/tmp93uyyya_']

1 个答案:

答案 0 :(得分:2)

在挖掘之后,似乎在NLTK中已弃用StanfordDependencyParser类:

新的,改进的方式:

首先,从here下载完整的CoreNLP文件,然后运行以下命令在下载的文件夹中启动CoreNLP服务器(我选择端口9010)。该文件夹看起来像stanford-parser-full-2018-02-27目录,为您:

$ java -mx1g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -port 9010 -timeout 15000

然后,运行以下代码:

from nltk.parse.corenlp import CoreNLPParser
parser = CoreNLPParser(url='http://localhost:{somePort}'
next(
     parser.raw_parse('The quick brown fox sucks at jumping.')
     ).pretty_print()
                ROOT
                 |
                 S
       __________|__________________________
      |                     VP              |
      |                 ____|___            |
      |                |        PP          |
      |                |     ___|_____      |
      |                |    |         S     |
      |                |    |         |     |
      NP               |    |         VP    |
  ____|__________      |    |         |     |
 DT   JJ    JJ   NN   VBZ   IN       VBG    .
 |    |     |    |     |    |         |     |
The quick brown fox  sucks  at     jumping  .

另外,有趣的是,一旦服务器运行,您可以导航到http://localhost:9010(或您选择的任何端口)并查看一个漂亮的小界面来修补。