使用NLTK、Python在StanfordDependencyParser中设置几个corenlp\u选项

2024-05-18 08:19:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试为与NLTK一起使用的StanfordDependencyParser设置几个选项:

from nltk.parse.stanford import StanfordDependencyParser
os.environ['CLASSPATH'] = "path/to/stanford-parser-3.9.1-models.jar"
sdp = StanfordDependencyParser('path/to/stanford-parser-3.9.1-models.jar', corenlp_options=['-retainTmpSubcategories', '-originalDependencies'])

然而,我得到

raise OSError('Java command failed : ' + str(cmd))
OSError: Java command failed : ['C:\\Program Files\\Java\\jdk-11\\bin\\java.exe', '-mx1000m', '-cp', '\\stanford-parser.jar', 'edu.stanford.nlp.parser.lexparser.LexicalizedParser', '-model', 'edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz', '-sentences', 'newline', '-outputFormat', 'conll2007', '-encoding', 'utf8', ['-retainTmpSubcategories', '-originalDependencies'], 'C:\\Users\\...']

(为了更清楚,我去掉了一些路径。)

如果我只使用

sdp = StanfordDependencyParser('path/to/stanford-parser-3.9.1-models.jar', corenlp_options=['-retainTmpSubcategories'])

或者

sdp = StanfordDependencyParser('path/to/stanford-parser-3.9.1-models.jar', corenlp_options=['-originalDependencies'])

效果很好

我还研究了the nltk code,它是用来计算命令行的执行函数

def _execute(self, cmd, input_, verbose=False):
    encoding = self._encoding
    cmd.extend(['-encoding', encoding])
    if self.corenlp_options:
        cmd.append(self.corenlp_options)

这是否意味着我只能在corenlp\u options参数中放置一个选项

谢谢你的帮助


Tags: topathselfcmdparsersdpmodelsencoding
1条回答
网友
1楼 · 发布于 2024-05-18 08:19:19

我一直在寻找解决办法,但似乎没有一个“简单”的办法。 我找到的解决方案是直接修改nltk\parse\文件夹中的stanford.py文件。我替换了

cmd.append(self.corenlp_options)

cmd.extend(self.corenlp_options)

虽然我怀疑这是最好的办法,但也没有例外

相关问题 更多 >