malt parser gives assertion error when using it wi

2019-02-24 11:53发布

问题:

I am using malt parser with python nltk. I have successfully downloaded the training data and updated the latest nltk. When I call the malt parser it gives me an asertion error. Below is the code from python which includes the traceback as well.

 mp = MaltParser("C:/Users/mustufain/Desktop/Python Files/maltparser-1.8.1","C:/Users/mustufain/Desktop/Python Files/maltparser-1.7.2",additional_java_args=['-Xmx512m'])

Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    mp = MaltParser("C:/Users/mustufain/Desktop/Python Files/maltparser-1.8.1","C:/Users/mustufain/Desktop/Python Files/maltparser-1.7.2",additional_java_args=['-Xmx512m'])
  File "C:\Python34\lib\site-packages\nltk\parse\malt.py", line 131, in __init__
    self.malt_jars = find_maltparser(parser_dirname)
  File "C:\Python34\lib\site-packages\nltk\parse\malt.py", line 72, in find_maltparser
    assert malt_dependencies.issubset(_jars)
AssertionError
>>> 

回答1:

TL;DR (In PYTHON3!!):

import urllib.request
urllib.request.urlretrieve('http://www.maltparser.org/mco/english_parser/engmalt.poly-1.7.mco', 'C:\\Users\\mustufain\\Desktop\\engmalt.poly-1.7.mco')
urllib.request.urlretrieve('http://maltparser.org/dist/maltparser-1.8.1.zip', 'C:\\Users\\mustufain\\Desktop\\maltparser-1.8.1.zip')
zfile = zipfile.ZipFile('C:\\Users\\mustufain\\Desktop\\maltparser-1.8.1.zip')
zfile.extractall('C:\\Users\\mustufain\\Desktop\\maltparser-1.8.1\\')

Then:

from nltk.parse import malt
mp = malt.MaltParser('C:\\Users\\mustufain\\Desktop\\maltparser-1.8.1\\', "C:\\Users\\mustufain\\Desktop\\engmalt.poly-1.7.mco")
mp.parse_one('I shot an elephant in my pajamas .'.split()).tree()


回答2:

If all the download and environmental variable setup is done correctly, most probably it's how file/dir path are splitted in the nltk.parse.malt.py, at https://github.com/nltk/nltk/blob/develop/nltk/parse/malt.py#L69 that splits the directory and filename specifically for linux:

def find_maltparser(parser_dirname):
    """
    A module to find MaltParser .jar file and its dependencies.
    """
    if os.path.exists(parser_dirname): # If a full path is given.
        _malt_dir = parser_dirname
    else: # Try to find path to maltparser directory in environment variables.
        _malt_dir = find_dir(parser_dirname, env_vars=('MALT_PARSER',))
    # Checks that that the found directory contains all the necessary .jar
    malt_dependencies = ['','','']
    _malt_jars = set(find_jars_within_path(_malt_dir))
    _jars = set(jar.rpartition('/')[2] for jar in _malt_jars)
    malt_dependencies = set(['log4j.jar', 'libsvm.jar', 'liblinear-1.8.jar'])

    assert malt_dependencies.issubset(_jars)
    assert any(filter(lambda i: i.startswith('maltparser-') and i.endswith('.jar'), _jars))
    return list(_malt_jars)

The bug has been fixed and in the process of merging at https://github.com/nltk/nltk/pull/1292

Changing this line:

_jars = set(jar.rpartition('/')[2] for jar in _malt_jars)

to this should solve your problem =)

_jars = set(os.path.split(jar)[1] for jar in _malt_jars)

For the answer not related to the code itself but how you have setup the environment variables or downloaded and saved the malt parser directories or files, see https://github.com/nltk/nltk/issues/1294