我正在尝试创建一个通过将文档切分成句子,然后对句子中的每个单词进行词形还原来进行分类的Spark作业。然而,我发现斯坦福的注释类在我的Spark作业中造成了严重的瓶颈(处理50万个文档需要20分钟)。
以下是我当前用于句子解析和分类的代码
句子解析:
def prepSentences(text: String): List[CoreMap] = { val mod = text.replace("Sr.", "Sr") // 处理一个边缘情况 val doc = new Annotation(mod) pipeHolder.get.annotate(doc) val sentences = doc.get(classOf[SentencesAnnotation]).toList sentences}
然后我对每个CoreMap进行如下处理以获取词形还原结果
def coreMapToLemmas(map:CoreMap):Seq[String] = { map.get(classOf[TokensAnnotation]).par.foldLeft(Seq[String]())( (a, b) => { val lemma = b.get(classOf[ListAnnotation]) if (!(stopWords.contains(b.lemma().toLowerCase) || puncWords.contains(b.originalText()))) a :+ lemma.toLowerCase else a })}
或许有只涉及部分处理的类?
回答:
尝试使用CoreNLP的Shift Reduce解析器实现。
一个基本示例(未经编译器输入):
val p = new Properties()p.put("annotators", "tokenize ssplit pos parse lemma sentiment")// 使用带有波束搜索的Shift-Reduce解析器// http://nlp.stanford.edu/software/srparser.shtmlp.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)
我在一个使用CoreNLP的生产系统中工作,该系统在Spark处理管道中使用CoreNLP。使用带有波束搜索的Shift Reduce解析器使我的管道的解析速度提高了16倍,并减少了解析所需的工作内存量。Shift Reduce解析器在运行时间复杂度上是线性的,这比标准的词法化PCFG解析器更好。
要使用Shift Reduce解析器,您需要Shift Reduce模型的jar文件,您应该将其放置在类路径中(您可以从CoreNLP的网站上单独下载)。