scala - coreNLP 显着减慢了 Spark 作业的速度`

标签 scala machine-learning apache-spark stanford-nlp

我正在尝试做一个 Spark 工作,通过将文档切割成句子来进行分类,然后对句子中的每个单词进行词形还原以进行逻辑回归。然而,我发现 stanford 的注释类在我的 Spark 作业中造成了严重的瓶颈(仅处理 500k 文档需要 20 分钟)

这是我当前用于句子解析和分类的代码

句子解析:

def prepSentences(text: String): List[CoreMap] = {
    val mod = text.replace("Sr.", "Sr") // deals with an edge case
    val doc = new Annotation(mod)
    pipeHolder.get.annotate(doc)
    val sentences = doc.get(classOf[SentencesAnnotation]).toList
    sentences
}

然后,我获取每个核心映射并按如下方式处理引理

def coreMapToLemmas(map:CoreMap):Seq[String] = {
      map.get(classOf[TokensAnnotation]).par.foldLeft(Seq[String]())(
    (a, b) => {
        val lemma = b.get(classOf[LemmaAnnotation])
        if (!(stopWords.contains(b.lemma().toLowerCase) || puncWords.contains(b.originalText())))
      a :+ lemma.toLowerCase
    else a
  }
)
}

也许有一个类只涉及一些处理?

最佳答案

尝试使用CoreNLP's Shift Reduce parser implementation .

一个基本示例(无需编译器输入):

val p = new Properties()
p.put("annotators", "tokenize ssplit pos parse lemma sentiment")
// use Shift-Reduce Parser with beam search
// http://nlp.stanford.edu/software/srparser.shtml
p.put("parse.model", "edu/stanford/nlp/models/srparser/englishSR.beam.ser.gz")
val corenlp = new StanfordCoreNLP(props)

val text = "text to annotate"
val annotation = new Annotation(text)
corenlp.annotate(text)

我正在开发一个在 Spark 处理管道中使用 CoreNLP 的生产系统。将 Shift Reduce 解析器与 Beam search 结合使用,将管道的解析速度提高了 16 倍,并减少了解析所需的工作内存量。 Shift Reduce 解析器在运行时复杂度上是线性的,这比标准词法化 PCFG 解析器要好。

要使用移位归约解析器,您需要将移位归约模型 jar 放在您的类路径中(您可以从 CoreNLP 的网站单独下载)。

关于scala - coreNLP 显着减慢了 Spark 作业的速度`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33270080/

相关文章:

python - 我是否错误地使用了 LMDB?它说在 0 次插入后达到环境映射大小限制

json - 将对象的 json 数组解析为其适当的案例类

performance - TFJS 中 eager 风格的性能成本是多少?

python - 如何通过拆分为列将列表另存为 csv

mysql - 在spark sql中转换两个数据帧

hadoop - 在相对较大的输入上运行 Spark 作业时出现内存问题

scala - 让 sbt 在文件更改时重新运行 - `~ compile` 相当于 `run`

java - 为在 yarn 模式下运行的每个 spark 作业配置 log4j

scala - `toString`中的 `scala.Enumeration$Value`是如何实现的?

scala - 将结构数组分解为 Spark 中的列