coreNLP显着减缓了火花工作

时间:2015-10-21 22:05:03

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

我正在尝试通过将文档剪切成句子来进行分类,然后将句子中的每个单词进行逻辑回归以进行逻辑回归。但是,我发现stanford的注释类在我的火花工作中造成了严重的瓶颈(它只花了20分钟来处理500k文件)

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

句子解析:

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
  }
)
}

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

1 个答案:

答案 0 :(得分:7)

尝试使用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。使用具有Beam搜索的Shift Reduce解析器将我的管道的解析速度提高了16倍,并减少了解析所需的工作内存量。 Shift Reduce解析器在运行时复杂度上是线性的,这比标准的词汇化PCFG解析器更好。

要使用shift reduce解析器,您需要在类路径中放置shift shift模型jar(可以从CoreNLP网站单独下载)。