我试图从NLTK使用MaltParser。NLTK MaltParser不会解析
我能得到配置解析器点:
import nltk
parser = nltk.parse.malt.MaltParser()
parser.config_malt()
parser.train_from_file('malt_train.conll')
,但是当涉及到实际的解析,解析器返回一个错误:
File "<stdin>", line 1, in <module>
File "/Library/Python/2.7/site-packages/nltk/parse/malt.py", line 98, in raw_parse
return self.parse(words, verbose)
File "/Library/Python/2.7/site-packages/nltk/parse/malt.py", line 85, in parse
return self.tagged_parse(taggedwords, verbose)
File "/Library/Python/2.7/site-packages/nltk/parse/malt.py", line 139, in tagged_parse
return DependencyGraph.load(output_file)
File "/Library/Python/2.7/site-packages/nltk/parse/dependencygraph.py", line 121, in load
return DependencyGraph(open(file).read())
IOError: [Errno 2] No such file or directory:'/var/folders/77/ch5yxf153jl67kmqr5jqywgr0000gn/T/malt_output.conll'
下面是给出了错误的命令(来自malt.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.
也试图与预先训练engmalt.poly.mco相同,engmalt.linear.mco
任何建议都非常欢迎。
编辑:这里是malt.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()
是否存在'/ var/folders/77/ch5yxf153jl67kmqr5jqywgr0000gn/T/malt_output.conll'文件?程序是否有权在那里写入? – 2012-03-01 21:43:28
该文件不会生成并且权限被修复/授予。 – Stpn 2012-03-01 21:46:06
请参阅http://stackoverflow.com/a/35681125/610569 – alvas 2016-02-28 10:04:46