斯坦福POSTagger,Java堆内存空间

5 投票
3 回答
1810 浏览
提问于 2025-04-28 12:47

我在Ubuntu 14.04上使用Python3,并且正在对67篇原始文本文章运行斯坦福的词性标注器(POSTagger),下面是我简化后的Python脚本:

from nltk.tag.stanford import POSTagger

with open('the_file.txt','r') as file:
    G=file.readlines()

stan=[]

english_postagger = POSTagger('models/english-bidirectional-distsim.tagger', 'stanford-postagger.jar')

for line in g:
    stan.append(english_postagger.tag(tokenize_fast(line)))

经过几次尝试后,我遇到了以下错误:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at edu.stanford.nlp.sequences.ExactBestSequenceFinder.bestSequence(ExactBestSequenceFinder.java:109)
at edu.stanford.nlp.sequences.ExactBestSequenceFinder.bestSequence(ExactBestSequenceFinder.java:31)
at edu.stanford.nlp.tagger.maxent.TestSentence.runTagInference(TestSentence.java:322)
at edu.stanford.nlp.tagger.maxent.TestSentence.testTagInference(TestSentence.java:312)
at edu.stanford.nlp.tagger.maxent.TestSentence.tagSentence(TestSentence.java:135)
at edu.stanford.nlp.tagger.maxent.MaxentTagger.tagSentence(MaxentTagger.java:998)
at edu.stanford.nlp.tagger.maxent.MaxentTagger.tagCoreLabelsOrHasWords(MaxentTagger.java:1788)
at edu.stanford.nlp.tagger.maxent.MaxentTagger.tagAndOutputSentence(MaxentTagger.java:1798)
at edu.stanford.nlp.tagger.maxent.MaxentTagger.runTagger(MaxentTagger.java:1709)
at edu.stanford.nlp.tagger.maxent.MaxentTagger.runTagger(MaxentTagger.java:1770)
at edu.stanford.nlp.tagger.maxent.MaxentTagger.runTagger(MaxentTagger.java:1543)
at edu.stanford.nlp.tagger.maxent.MaxentTagger.runTagger(MaxentTagger.java:1499)
at edu.stanford.nlp.tagger.maxent.MaxentTagger.main(MaxentTagger.java:1842)

我也在命令行中运行了斯坦福词性标注器,命令如下:

java -mx300m -classpath stanford-postagger.jar   edu.stanford.nlp.tagger.maxent.MaxentTagger -model models/wsj-0-18-bidirectional-distsim.tagger -textFile sample-input.txt > sample-tagged.txt

结果也出现了类似的错误。我甚至给Java分配了2GB的内存,但还是没有解决问题。

如果你有任何想法、建议或者一些“黑科技”的解决方案,我非常欢迎!

感谢@nsanglar的提醒,我尝试了:

java -Xmx2g -classpath stanford-postagger.jar   edu.stanford.nlp.tagger.maxent.MaxentTagger -model models/wsj-0-18-bidirectional-distsim.tagger -textFile raw_text.txt > sample-tagged.txt

我收到了一个错误日志消息,头部信息如下:

# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (malloc) failed to allocate 283639808 bytes for committing   reserved memory.
# Possible reasons:
#   The system is out of physical RAM or swap space
#   In 32 bit mode, the process size limit was hit
# Possible solutions:
#   Reduce memory load on the system
#   Increase physical memory or swap space
#   Check if swap backing store is full
#   Use 64 bit Java on a 64 bit OS
#     Decrease Java heap size (-Xmx/-Xms)
#   Decrease number of Java threads
#   Decrease Java thread stack sizes (-Xss)
#   Set larger code cache with -XX:ReservedCodeCacheSize=
# This output file may be truncated or incomplete.

#  Out of Memory Error (os_linux.cpp:2798), pid=25677, tid=140571167794944

# JRE version: OpenJDK Runtime Environment (7.0_65-b32) (build 1.7.0_65-b32)
# Java VM: OpenJDK 64-Bit Server VM (24.65-b04 mixed mode linux-amd64 compressed oops)
# Derivative: IcedTea 2.5.2
# Distribution: Ubuntu 14.04 LTS, package 7u65-2.5.2-3~14.04
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try  "ulimit -c unlimited" before starting Java again
暂无标签

3 个回答

0

在Python中,集合(set)是一种数据结构,用来存储一组不重复的元素。你可以把它想象成一个装东西的箱子,箱子里只能放一种东西,不能放重复的。

集合的特点是:它不允许有重复的元素,而且集合中的元素是无序的,也就是说你不能依赖元素的顺序。

你可以用大括号来创建一个集合,比如:

nltk.internals.config_java(options='-Xmx3024m')
1

你应该使用 -Xmx1024m。我觉得你可能打错字了,因为你现在用的是 -mx :)

2

原来是内存的问题,我的电脑内存不够,无法执行这个命令。把程序放在服务器上运行就解决了这个问题。

撰写回答