一步一步让NLTK中的麦芽解析器工作?

时间:2012-11-03 08:24:04

标签: python parsing nltk

我已经尝试了一切在阳光下使Malt Parser(1.7.1)使用他们预先训练过的模型(加上.mco hack)来工作。我得到的最接近的是DependencyGraph,每个单词的第一个字母作为标签。我只到过那里一次,不能回来。 99%的时间,我得到的是:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/nltk/parse/malt.py", line 98, in parse
    return self.tagged_parse(taggedwords, verbose)
  File "/Library/Python/2.7/site-packages/nltk/parse/malt.py", line 150, in tagged_parse
    "code %d" % (' '.join(cmd), ret))
Exception: MaltParser parsing (java -jar /Users/walrusthecat/maltparser/malt.jar -w /var/folders/2b/0fpc89fd0rqbj8bf4r7xbh640000gp/T -c /Users/walrusthecat/maltparser/model.mco -i /var/folders/2b/0fpc89fd0rqbj8bf4r7xbh640000gp/T/malt_input.conlltApSTj -o /var/folders/2b/0fpc89fd0rqbj8bf4r7xbh640000gp/T/malt_output.conllrkclZz -m parse) failed with exit code 1

如果我列出了写入临时文件的目录,或者在sudo下执行python,就会发生这种情况。我尝试过使用Malt Parser 1.7.1和1.2。什么?

由于

2 个答案:

答案 0 :(得分:2)

2015年8月,NLTK中的MaltParser API获得了新的更新。

这是让MaltParser在Linux上运行的一步一步:

<强> 1。下载提取麦芽解析器和预先训练的模型

cd 
wget http://www.maltparser.org/mco/english_parser/engmalt.linear-1.7.mco
wget http://maltparser.org/dist/maltparser-1.8.1.zip
unzip maltparser-1.8.1.zip

<强> 2。设置环境变量

e.g ..

export MALT_PARSER=$HOME/maltparser-1.8.1/
export MALT_MODEL=$HOME/engmalt.linear-1.7.mco

(见https://github.com/nltk/nltk/wiki/Installing-Third-Party-Software#malt-parser

然后在python

>>> from nltk.parse.malt import MaltParser
>>> mp = MaltParser('maltparser-1.8.1', 'engmalt.linear-1.7.mco')
>>> mp.parse_one('I shot an elephant in my pajamas .'.split()).tree()
Tree('shot', ['I', Tree('elephant', ['an']), Tree('in', [Tree('pajamas', ['my'])]), '.'])

<强> TL;DR

alvas@ubi:~$ cd 
alvas@ubi:~$ wget http://www.maltparser.org/mco/english_parser/engmalt.linear-1.7.mco
alvas@ubi:~$ wget http://maltparser.org/dist/maltparser-1.8.1.zip
alvas@ubi:~$ unzip maltparser-1.8.1.zip
alvas@ubi:~$ export MALT_PARSER=$HOME/maltparser-1.8.1/
alvas@ubi:~$ export MALT_MODEL=$HOME/engmalt.linear-1.7.mco
alvas@ubi:~$ python
Python 2.7.11 (default, Dec 15 2015, 16:46:19) 
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from nltk.parse.malt import MaltParser
>>> mp = MaltParser('maltparser-1.8.1', 'engmalt.linear-1.7.mco')
>>> mp.parse_one('I shot an elephant in my pajamas .'.split()).tree()
Tree('shot', ['I', Tree('elephant', ['an']), Tree('in', [Tree('pajamas', ['my'])]), '.'])

有关详细信息,请参阅演示:

在Windows上,请按照打印屏幕步骤小心https://github.com/nltk/nltk/issues/1294#issuecomment-189831647

总结Windows步骤:

  • 安装Conda(不要先安装NLTK)
  • 安装Git
  • 安装Java
  • 使用NLTK安装pip install -U https://github.com/nltk/nltk.git不要使用conda install nltk ,直到他们将包裹更新为NLTK v3.2 !!!)

答案 1 :(得分:0)

您无需提供.mco文件的完整路径,只需文件名即可。

how-to-use-malt-parser-in-python-nltk