Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

python - Step by step to getting malt parser in NLTK to work?

I have tried everything under the sun to make Malt Parser (1.7.1) with their pre-trained model (added with the .mco hack) to work. The closest I've gotten is a DependencyGraph with the first letter of each word as the label. I only got there once and can't get back. 99% of the time, all I get is:

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

It happens if I chown the directories where it's writing the temp files, or execute python under sudo. I've tried with Malt Parser 1.7.1 and 1.2 . Anything?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

MaltParser API in NLTK was given a fresh update during August 2015.

Here's a step by step way to get MaltParser to work on Linux:

1. Download the extract the malt parser and pre-trained model

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. Setup the environment variables

  • Make sure java is installed
  • Download & extract the Malt Parser: http://www.maltparser.org/download.html
  • Set the environment variable MALT_PARSER to point to the MaltParser directory, e.g. /home/user/maltparser-1.8.1/ in Linux.
  • When using a pre-trained model, set the environment variable MALT_MODEL to point to .mco file, e.g. engmalt.linear-1.7.mco from http://www.maltparser.org/mco/mco.html.

e.g..

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

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

Then in 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'])]), '.'])

For more info, please see demo on:


On Windows, please follow the print-screen steps CAREFULLY: https://github.com/nltk/nltk/issues/1294#issuecomment-189831647

To summarize the Windows steps:

  • Install Conda (DO NOT INSTALL NLTK FIRST)
  • Install Git
  • Install Java
  • Install NLTK with pip install -U https://github.com/nltk/nltk.git (DO NOT USE conda install nltk, until they've updated their package to NLTK v3.2 !!!)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...