parse() 需要两个参数(给了三个)

0 投票
1 回答
525 浏览
提问于 2025-04-18 09:14

当我运行这段代码时:

import nltk
parser = nltk.parse.malt.MaltParser(working_dir="c:\maltparser-1.7.2",mco="engmalt.linear-   1.7", additional_java_args=['-Xmx512m'])
tree=parser.raw_parse("Hi,I am Kruthika");

我遇到了以下错误:

Traceback (most recent call last):
  File "<pyshell#14>", line 1, in <module>
   tree=parser.raw_parse("Hi,I am Kruthika");
  File "C:\Python27\lib\site-packages\nltk-3.0a3-py2.7.egg\nltk\parse\malt.py", line 127, in raw_parse
return self.parse(words, verbose)
TypeError: parse() takes exactly 2 arguments (3 given)

我只提供了一个参数。我正在尝试在Python中使用MaltParser(在Windows操作系统上)……

1 个回答

0

我建议你升级到最新版本的NLTK(3.0a4)。从你的路径来看("C:\Python27\lib\site-packages\nltk-3.0a3-py2.7.egg\nltk\parse\malt.py"),你现在使用的是3.0a3版本。

问题出在raw_parse这个函数里。这个函数的最后一行调用了self.parse

def raw_parse(self, sentence, verbose=False):
    """
    Use MaltParser to parse a sentence. Takes a sentence as a string;
    before parsing, it will be automatically tokenized and tagged with this
    MaltParser instance's tagger.

    :param sentence: Input sentence to parse
    :type sentence: str
    :return: ``DependencyGraph`` the dependency graph representation of the sentence
    """
    words = word_tokenize(sentence)
    return self.parse(words, verbose)

而在当前版本中,这个函数的定义是MaltParser.parse(self, words, verbose=False),它应该接收3个参数("self"这个参数是自动传入的),但在你的情况下,它却提示说parse只接收2个参数。

你可以查看一下help(parser.parse),看看你现在版本中的函数定义是什么,但这很可能是个bug。

撰写回答