【问题标题】:Running out of Memory with POS Tagging in R在 R 中使用 POS 标记耗尽内存
【发布时间】:2017-05-16 00:53:29
【问题描述】:

我希望有更好的方法来做到这一点。我有一个带有推文的 data.table:其中 8018 条。我正在使用 data.table 函数对每条推文进行 POS 标记。

数据表:

Tweet
Sample Tweet 1 :)
sample tweet 2

词性标注功能:

tagPOS =  function(x) {
   s <- as.String(x)
   sent_token_annotator = Maxent_Sent_Token_Annotator()
   word_token_annotator = Maxent_Word_Token_Annotator()
   a2 = annotate(s, list(sent_token_annotator, word_token_annotator))
   pos_tag_annotator = Maxent_POS_Tag_Annotator()
   a3 = annotate(s, pos_tag_annotator, a2)
   a3w = subset(a3, type == "word")
   POStags = unlist(lapply(a3w$features, `[[`, "POS"))
   return(paste(POStags,collapse = " "))
}

获取 POS:

dat[,c("ID"):= .I]
options( java.parameters = "-Xmx10g" )
dat[,c("POS"):= tagPOS(Tweet),by = .(ID)]

即使设置了内存,我也会收到此错误:

Error in .jnew("opennlp.tools.postag.POSModel",    .jcast(.jnew("java.io.FileInputStream",  : 
java.lang.OutOfMemoryError: GC overhead limit exceeded 

【问题讨论】:

  • @lukeA 你只是在指出添加选项options( java.parameters = "-Xmx10g" ) 吗?我的代码中已经有了它,但它仍然无法正常工作......
  • 您是否在任何库调用之前放置了选项语句?
  • @lukeA 是的,还是不行。

标签: r twitter nlp data.table


【解决方案1】:

试试这个:

options(java.parameters = "- Xmx3000m")
library(rJava)
library(NLP)
library(openNLP)
library(data.table)
tagPOS <-  function(x) {
  s <- as.String(x)
  sent_token_annotator = Maxent_Sent_Token_Annotator()
  word_token_annotator = Maxent_Word_Token_Annotator()
  a2 = annotate(s, list(sent_token_annotator, word_token_annotator))
  pos_tag_annotator = Maxent_POS_Tag_Annotator()
  a3 = annotate(s, pos_tag_annotator, a2)
  a3w = subset(a3, type == "word")
  POStags = unlist(lapply(a3w$features, `[[`, "POS"))
  gc()
  return(paste(POStags,collapse = " "))
}
dat <- data.table(Tweet = rep("This is a tweet.", 10000L))
dat[,c("ID"):= .I]
dat[,c("POS"):= tagPOS(Tweet),by = .(ID)]

【讨论】:

  • 所以看起来添加 gc() 解决了这个问题。虽然速度非常慢。我大约 10 分钟前启动了该程序,它仍在运行。不知道有没有更高效的方法想运行这个?
  • 嗯,终于跑完了。它确实有效,但需要 1.5 小时才能运行。我想这在技术上已经解决了,所以我可以结束这个问题,但是如果你知道如何提高速度,这对我有很大帮助。
  • 没关系,改一下函数,通过Maxent,现在9分钟就可以运行了。
  • 提高速度的一种方法是并行运行。您究竟是如何更改功能以加快速度的?
  • @Kristofersen ,通过 Maxent 是什么意思?我正在处理一个类似的问题,我真的很想加快我的代码速度。
猜你喜欢
  • 2013-07-13
  • 1970-01-01
  • 1970-01-01
  • 2013-07-22
  • 2021-02-24
  • 1970-01-01
  • 2019-09-09
  • 2015-08-23
  • 2012-11-10
相关资源
最近更新 更多