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 !!!)