我尝试使用NLTK的MaltParser。在
我可以开始配置解析器了:
import nltk
parser = nltk.parse.malt.MaltParser()
parser.config_malt()
parser.train_from_file('malt_train.conll')
但在实际解析时,解析器返回一个错误:
^{pr2}$以下是给出错误的命令(来自麦芽糖.py)公司名称:
['java', '-jar /usr/lib/malt-1.6.1/malt.jar', '-w /var/folders/77/ch5yxf153jl67kmqr5jqywgr0000gn/T', '-c malt_temp', '-i /var/folders/77/ch5yxf153jl67kmqr5jqywgr0000gn/T/malt_input.conll', '-o /var/folders/77/ch5yxf153jl67kmqr5jqywgr0000gn/T/malt_output.conll', '-m parse']
我尝试运行jus java命令,得到的结果是:
The file entry 'malt_temp_singlemalt.info' in the mco file '/var/folders/77/ch5yxf153jl67kmqr5jqywgr0000gn/T/malt_temp.mco' cannot be loaded.
也尝试了同样的预科英国保利公司以及英语.linear.mco在
欢迎提出任何建议。在
编辑:以下是麦芽糖.py在
def tagged_parse(self, sentence, verbose=False):
"""
Use MaltParser to parse a sentence. Takes a sentence as a list of
(word, tag) tuples; the sentence must have already been tokenized and
tagged.
@param sentence: Input sentence to parse
@type sentence: L{list} of (word, tag) L{tuple}s.
@return: C{DependencyGraph} the dependency graph representation of the sentence
"""
if not self._malt_bin:
raise Exception("MaltParser location is not configured. Call config_malt() first.")
if not self._trained:
raise Exception("Parser has not been trained. Call train() first.")
input_file = os.path.join(tempfile.gettempdir(), 'malt_input.conll')
output_file = os.path.join(tempfile.gettempdir(), 'malt_output.conll')
execute_string = 'java -jar %s -w %s -c %s -i %s -o %s -m parse'
if not verbose:
execute_string += ' > ' + os.path.join(tempfile.gettempdir(), "malt.out")
f = None
try:
f = open(input_file, 'w')
for (i, (word,tag)) in enumerate(sentence):
f.write('%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n' %
(i+1, word, '_', tag, tag, '_', '0', 'a', '_', '_'))
f.write('\n')
f.close()
cmd = ['java', '-jar %s' % self._malt_bin, '-w %s' % tempfile.gettempdir(),
'-c %s' % self.mco, '-i %s' % input_file, '-o %s' % output_file, '-m parse']
print cmd
self._execute(cmd, 'parse', verbose)
return DependencyGraph.load(output_file)
finally:
if f: f.close()
我不确定这个问题是否还没有解决(但我认为它已经解决了), 但由于我前一段时间也遇到了同样的问题,我想分享我的知识。在
首先,MaltParser Jar不接受前面有指向其文件的直接路径的.connl文件。就像上面看到的。 为什么是这样。。。我不知道。在
但是,您可以通过将命令行更改为以下内容来轻松修复它:
现在使用-w参数设置.conll文件的目录。使用它,您可以从任何给定的文件夹加载任何.conll文件。 我还从
tempfile.gettempdir()
更改为self.working_dir
,因为在“原始”NLTK版本中,总是将/tmp/文件夹设置为工作目录。即使使用另一个工作目录初始化Maltparser。在我希望这些信息能对某人有所帮助。在
还有一件事, 如果您想一次解析多个句子,但每个句子都是单独的,而不是依赖于所有其他句子,则必须在输入.conll文件,并以1重新开始每个句子的编号。在
相关问题 更多 >
编程相关推荐