我正在尝试做一个 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/