coreNLP显着减缓了火花工作

Dan*_*man 2 scala machine-learning stanford-nlp apache-spark

我试图通过将文档剪切成句子来进行分类,然后将句子中的每个单词进行逻辑回归以进行逻辑回归.但是,我发现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
}
Run Code Online (Sandbox Code Playgroud)

然后,我将采用每个coremap并按如下方式处理引理

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
  }
)
}
Run Code Online (Sandbox Code Playgroud)

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

Tay*_*r R 7

尝试使用CoreNLP的Shift Reduce解析器实现.

一个基本的例子(没有编译器输入):

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)
Run Code Online (Sandbox Code Playgroud)

我在一个生产系统上工作,该系统在Spark处理管道中使用CoreNLP.使用具有Beam搜索的Shift Reduce解析器将我的管道的解析速度提高了16倍,并减少了解析所需的工作内存量.Shift Reduce解析器在运行时复杂度上是线性的,这比标准的词汇化PCFG解析器更好.

要使用shift reduce解析器,你需要你应该放在classpath上的shift reduce models jar(你可以从CoreNLP的网站上单独下载).