我一直在寻找这个问题的答案,但是找不到适合我需要的答案。
我试图将nltk包装器用于MaltParser。似乎有很多人在过去曾经遇到过MaltParser的nltk包装器问题,但是我没有发现同样的错误或相同的修复。这是代码,这里是错误:
import nltk
maltParser = nltk.parse.malt.MaltParser(parser_dirname="/home/owen/maltparser-1.7.2",
model_filename="engmalt.linear-1.7",
additional_java_args=['-Xmx512m'])
result = maltParser.parse_one('This is a test sentence'.split())
print result.tree()
这是错误:
异常:MaltParser解析(java -Xmx512m -cp /home/owen/maltparser-1.7.2/lib/libsvm.jar:/home/owen/maltparser-1.7.2/malt.jar:/home/owen/ maltparser-1.7.2 / lib / log4j.jar:/home/owen/maltparser-1.7.2/maltparser-1.7.2.jar:/home/owen/maltparser-1.7.2/lib/liblinear-1.8.jar org .maltparser.Malt -c engmalt.linear-1.7 -i /tmp/malt_input.conll.ej6hqf -o /tmp/malt_output.conll.TZXpc2 -m parse)失败,退出代码为1
以下是我已经尝试过的各种不同组合的内容:
仅供参考:我已按照以下帖子中的步骤进行操作:
Step by step to getting malt parser in NLTK to work?
我可能还有其他一些尝试过。我无法记住。我真正想知道的是错误的含义以及可能导致错误的原因。我不能做它的头或尾。
以下是已请求的信息:
lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 12.04.5 LTS
Release: 12.04
Codename: precise
owen@termedev:~/malttesting$ python -c "import nltk; print nltk.__version__"
3.2.1
owen@termedev:~/malttesting$ java -version
java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)
owen@termedev:~/malttesting$ python -c "import os; print os.environ['MALT_PARSER']; print os.environ['MALT_MODEL']"
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python2.7/UserDict.py", line 23, in __getitem__
raise KeyError(key)
KeyError: 'MALT_PARSER'
感谢您的帮助
答案 0 :(得分:1)
有几件事可能出错:
首先检查您是否已安装并更新了NLTK :
$ pip install -U nltk
要在Ubuntu上更新您的Java ,请参阅http://tecadmin.net/install-oracle-java-8-jdk-8-ubuntu-via-ppa/#
尝试使用MaltParser版本&gt; 1.8使用相应的.mco
个文件(例如engmalt.linear-1.7
)
然后完成Step by step to getting malt parser in NLTK to work?
中TL;DR
指示中的步骤
简而言之,在终端上运行时,您应该看到没有错误:
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 && export MALT_PARSER=$HOME/maltparser-1.8.1/ && export MALT_MODEL=$HOME/engmalt.linear-1.7.mco
python -c "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()"
您必须更改到运行脚本的目录,然后在运行脚本之前在终端上设置环境变量:
cd /home/username/directory_where_you_are_going_to_run_your_script
export MALT_PARSER=$HOME/maltparser-1.8.1/
export MALT_MODEL=$HOME/engmalt.linear-1.7.mco
python yourscript.py
如果上述问题的解决方案无法解决您在NLTK中使用MaltParser API的问题,那么您最好
pip
[在终端上]:
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 && export MALT_PARSER=$HOME/maltparser-1.8.1/ && export MALT_MODEL=$HOME/engmalt.linear-1.7.mco
python -c "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()"
然后在设置环境变量后重新运行脚本,例如用:
cd /home/username/directory_where_you_are_going_to_run_your_script
export MALT_PARSER=$HOME/maltparser-1.8.1/
export MALT_MODEL=$HOME/engmalt.linear-1.7.mco
python yourscript.py