NLTK Stanford POS Tagger中的Java命令失败

问题描述 投票:1回答:2

我请求你帮助解决“Java命令失败”的错误,当我尝试标记大小为2兆字节的阿拉伯语语料库时,它会不断抛出。我搜索了网络和stanford POS标签邮件列表。但是,我没有找到解决方案。我读了一些与此类似的问题的帖子,并建议使用内存。我不确定。我还有19GB的可用内存。我尝试了所提供的所有可能的解决方案,但同样的错误仍然显示

我对Python有平均命令,在Linux上有良好的命令。我正在使用适用于阿拉伯语的LinuxMint17 KDE 64位,Python3.4,NLTK alpha和Stanford POS标记模型。这是我的代码:

import nltk
from nltk.tag.stanford import POSTagger
arabic_postagger = POSTagger("/home/mohammed/postagger/models/arabic.tagger", "/home/mohammed/postagger/stanford-postagger.jar", encoding='utf-8')

print("Executing tag_corpus.py...\n")


# Import corpus file
print("Importing data...\n")

file = open("test.txt", 'r', encoding='utf-8').read()
text = file.strip()

print("Tagging the corpus. Please wait...\n")

tagged_corpus = arabic_postagger.tag(nltk.word_tokenize(text))

如果企业规模小于1MB(= 100,000字),则不会出现错误。但是,当我尝试标记2MB CORPUS时,然后显示以下错误消息:

Traceback (most recent call last):
File "/home/mohammed/experiments/current/tag_corpus2.py", line 17, in <module>
tagged_lst = arabic_postagger.tag(nltk.word_tokenize(text))
File "/usr/local/lib/python3.4/dist-packages/nltk-3.0a3-py3.4.egg/nltk/tag/stanford.py", line 59, in tag
return self.batch_tag([tokens])[0]
File "/usr/local/lib/python3.4/dist-packages/nltk-3.0a3-py3.4.egg/nltk/tag/stanford.py", line 81, in batch_tag
stdout=PIPE, stderr=PIPE)
File "/usr/local/lib/python3.4/dist-packages/nltk-3.0a3-py3.4.egg/nltk/internals.py", line 171, in java
raise OSError('Java command failed!')
OSError: Java command failed!

我打算在我的博士学位中标注3亿字。研究项目。如果我一次标记10万个单词,我将不得不重复任务3000次。它会杀了我!

我非常感谢你的帮助。

java python-3.x nlp nltk pos-tagger
2个回答
4
投票

导入行添加此行后:

nltk.internals.config_java(options='-xmx2G')

这将增加java允许Stanford POS Tagger使用的最大RAM大小。 '-xmx2G'将最大允许RAM更改为2GB而不是默认512MB。

有关更多信息,请参阅What are the Xms and Xmx parameters when starting JVMs?


如果您对如何调试代码感兴趣,请继续阅读。

所以我们看到在处理大量数据时命令失败所以首先要看的是在调用Stanford标记器之前如何在NLTK中初始化Java,来自https://github.com/nltk/nltk/blob/develop/nltk/tag/stanford.py#L19

from nltk.internals import find_file, find_jar, config_java, java, _java_options

我们看到nltk.internals包正在处理不同的Java配置和参数。

然后我们看看https://github.com/nltk/nltk/blob/develop/nltk/internals.py#L65,我们看到为Java的内存分配添加了no值。


0
投票

在版本3.9.2中,StanfordTagger类构造函数接受一个名为java_options的参数,该参数可用于设置POSTagger和NERTagger的内存。

例如。 pos_tagger = StanfordPOSTagger('models/english-bidirectional-distsim.tagger', path_to_jar='stanford-postagger-3.9.2.jar', java_options='-mx3g')

我发现@alvas的答案不起作用,因为StanfordTagger用内置的默认值1000m覆盖了我的记忆设置。也许在初始化nltk.internals.config_java之后使用StanfordPOSTagger可能会有效,但我没有尝试过。

© www.soinside.com 2019 - 2024. All rights reserved.