Maltparser 在 NLTK 中给出错误
Maltparser giving error in NLTK
我的密码是
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=['-Xmx1024m'])
print mp.raw_parse("Hello World")
错误是
Traceback (most recent call last):
File "malt.py", line 13, in <module>
print mp.raw_parse("Hello World")
File "/usr/local/lib/python2.7/dist-packages/nltk/parse/malt.py", line 139, in raw_parse
return self.parse(words, verbose)
File "/usr/local/lib/python2.7/dist-packages/nltk/parse/malt.py", line 126, in parse
return self.parse_sents([sentence], verbose)[0]
File "/usr/local/lib/python2.7/dist-packages/nltk/parse/malt.py", line 114, in parse_sents
return self.tagged_parse_sents(tagged_sentences, verbose)
File "/usr/local/lib/python2.7/dist-packages/nltk/parse/malt.py", line 194, in tagged_parse_sents
"code %d" % (' '.join(cmd), ret))
Exception: MaltParser parsing (java -Xmx1024m -jar /usr/local/bin/malt.jar -w /other/apps/maltparser-1.8.1 -c engmalt.poly-1.7.mco -i /other/apps/maltparser-1.8.1/malt_input.conlljOba1P -o /other/apps/maltparser-1.8.1/malt_output.conllPLcoTu -m parse) failed with exit code 1
NLTK 中的 MaltParser API 刚刚 a patch 修复并稳定了它过去存在的问题:
- How to use malt parser in python nltk
- Malt Parser throwing class not found exception
- MaltParser Not Working in Python NLTK
下面是如何在 NLTK 中使用 MaltParser API 的示例:
# 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))) .)
(请参阅 here for more demo code or here 以获得更详细的演示代码)
请注意,您还可以使用导出功能,并且可以在初始化 MaltParser
对象时避免使用完整路径。但是你仍然必须告诉对象解析器目录的名称和要查找的模型文件名,例如
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))) .)
我的密码是
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=['-Xmx1024m'])
print mp.raw_parse("Hello World")
错误是
Traceback (most recent call last):
File "malt.py", line 13, in <module>
print mp.raw_parse("Hello World")
File "/usr/local/lib/python2.7/dist-packages/nltk/parse/malt.py", line 139, in raw_parse
return self.parse(words, verbose)
File "/usr/local/lib/python2.7/dist-packages/nltk/parse/malt.py", line 126, in parse
return self.parse_sents([sentence], verbose)[0]
File "/usr/local/lib/python2.7/dist-packages/nltk/parse/malt.py", line 114, in parse_sents
return self.tagged_parse_sents(tagged_sentences, verbose)
File "/usr/local/lib/python2.7/dist-packages/nltk/parse/malt.py", line 194, in tagged_parse_sents
"code %d" % (' '.join(cmd), ret))
Exception: MaltParser parsing (java -Xmx1024m -jar /usr/local/bin/malt.jar -w /other/apps/maltparser-1.8.1 -c engmalt.poly-1.7.mco -i /other/apps/maltparser-1.8.1/malt_input.conlljOba1P -o /other/apps/maltparser-1.8.1/malt_output.conllPLcoTu -m parse) failed with exit code 1
NLTK 中的 MaltParser API 刚刚 a patch 修复并稳定了它过去存在的问题:
- How to use malt parser in python nltk
- Malt Parser throwing class not found exception
- MaltParser Not Working in Python NLTK
下面是如何在 NLTK 中使用 MaltParser API 的示例:
# 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))) .)
(请参阅 here for more demo code or here 以获得更详细的演示代码)
请注意,您还可以使用导出功能,并且可以在初始化 MaltParser
对象时避免使用完整路径。但是你仍然必须告诉对象解析器目录的名称和要查找的模型文件名,例如
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))) .)