Simplest method for text lemmatization in Scala and Spark

后端 未结 3 1936
梦谈多话
梦谈多话 2020-12-30 13:49

I want to use lemmatization on a text file:

surprise heard thump opened door small seedy man clasping package wrapped.

upgrading system found review spring          


        
3条回答
  •  天涯浪人
    2020-12-30 14:42

    I think @user52045 has the right idea. The only modification I would make would be to use mapPartitions instead of map -- this allows you to only do the potentially expensive pipeline creation once per partition. This may not be a huge hit on a lemmatization pipeline, but it will be extremely important if you want to do something that requires a model, like the NER portion of the pipeline.

    def plainTextToLemmas(text: String, stopWords: Set[String], pipeline:StanfordCoreNLP): Seq[String] = {
      val doc = new Annotation(text)
      pipeline.annotate(doc)
      val lemmas = new ArrayBuffer[String]()
      val sentences = doc.get(classOf[SentencesAnnotation])
      for (sentence <- sentences; token <- sentence.get(classOf[TokensAnnotation])) {
        val lemma = token.get(classOf[LemmaAnnotation])
        if (lemma.length > 2 && !stopWords.contains(lemma)) {
          lemmas += lemma.toLowerCase
        }
      }
      lemmas
    }
    
    val lemmatized = plainText.mapPartitions(strings => {
      val props = new Properties()
      props.put("annotators", "tokenize, ssplit, pos, lemma")
      val pipeline = new StanfordCoreNLP(props)
      strings.map(string => plainTextToLemmas(string, stopWords, pipeline))
    })
    lemmatized.foreach(println)
    

提交回复
热议问题