Maltparser giving error in NLTK

前端 未结 1 1351
长发绾君心
长发绾君心 2020-12-21 20:06

My COde is

from nltk.parse import malt
mp = malt.MaltParser(working_dir=\"/other/apps/maltparser-1.8.1\",mco=\"engmalt.poly-1.7.mco\",additional_java_args=[\         


        
相关标签:
1条回答
  • 2020-12-21 20:45

    The MaltParser API in NLTK just had a patch that fixes and stabilizes the problems that it used to have:

    • How to use malt parser in python nltk
    • Malt Parser throwing class not found exception
    • MaltParser Not Working in Python NLTK

    Here's an example of how to use MaltParser API in NLTK:

    # Upgrade your NLTK.
    alvas@ubi:~$ cd ~
    alvas@ubi:~$ pip install -U nltk
    
    # Get the latest MaltParser and model
    alvas@ubi:~$ wget http://maltparser.org/dist/maltparser-1.8.1.zip
    alvas@ubi:~$ unzip maltparser-1.8.1.zip 
    alvas@ubi:~$ wget http://www.maltparser.org/mco/english_parser/engmalt.poly-1.7.mco
    
    # In python, now you can do this:
    alvas@ubi:~$ python
    >>> from nltk.parse.malt import MaltParser
    >>> mp = MaltParser('/home/alvas/maltparser-1.8.1', '/home/alvas/engmalt.poly-1.7.mco')
    >>> sent1 = 'I shot an elephant in my pajamas .'.split()
    >>> print(mp.parse_one(sent1).tree())
    (shot I (elephant an (in (pajamas my))) .)
    

    (See here for more demo code or here for a more elaborated demo code)


    Note that you can also use the export features and you can escape the usage of full path when initializing the MaltParser object. But you have to still tell the object what is the name of the parser directory and model filename to look for, e.g.

    alvas@ubi:~$ export MALT_PARSER='/home/$UID/maltparser-1.8.1/'
    alvas@ubi:~$ export MALT_MODEL='/home/$UID/engmalt.poly-1.7.mco' 
    alvas@ubi:~$ python
    >>> from nltk.parse.malt import MaltParser
    >>> mp = MaltParser('maltparser-1.8.1', 'engmalt.poly-1.7.mco')
    >>> sent1 = 'I shot an elephant in my pajamas .'.split()
    >>> print(mp.parse_one(sent1).tree())
    (shot I (elephant an (in (pajamas my))) .)
    
    0 讨论(0)
提交回复
热议问题