Github user wangmiao1981 commented on a diff in the pull request:

    https://github.com/apache/spark/pull/14229#discussion_r74542484
  
    --- Diff: mllib/src/main/scala/org/apache/spark/ml/r/LDAWrapper.scala ---
    @@ -0,0 +1,210 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.ml.r
    +
    +import scala.collection.mutable
    +
    +import org.apache.hadoop.fs.Path
    +import org.json4s._
    +import org.json4s.JsonDSL._
    +import org.json4s.jackson.JsonMethods._
    +
    +import org.apache.spark.SparkException
    +import org.apache.spark.internal.Logging
    +import org.apache.spark.ml.{Pipeline, PipelineModel, PipelineStage}
    +import org.apache.spark.ml.clustering.{LDA, LDAModel}
    +import org.apache.spark.ml.feature.{CountVectorizer, CountVectorizerModel, 
RegexTokenizer, StopWordsRemover}
    +import org.apache.spark.ml.linalg.VectorUDT
    +import org.apache.spark.ml.util._
    +import org.apache.spark.sql.{DataFrame, Dataset}
    +import org.apache.spark.sql.functions._
    +import org.apache.spark.sql.types.StringType
    +
    +
    +private[r] class LDAWrapper private (
    +    val pipeline: PipelineModel,
    +    val logLikelihood: Double,
    +    val logPerplexity: Double,
    +    val vocabulary: Array[String]) extends MLWritable {
    +
    +  import LDAWrapper._
    +
    +  private val lda: LDAModel = pipeline.stages.last.asInstanceOf[LDAModel]
    +  private val preprocessor: PipelineModel =
    +    new PipelineModel(s"${Identifiable.randomUID(pipeline.uid)}", 
pipeline.stages.dropRight(1))
    +
    +  def transform(data: Dataset[_]): DataFrame = {
    +    pipeline.transform(data).drop(TOKENIZER_COL, STOPWORDS_REMOVER_COL, 
COUNT_VECTOR_COL)
    +  }
    +
    +  def computeLogPerplexity(data: Dataset[_]): Double = {
    +    lda.logPerplexity(preprocessor.transform(data))
    +  }
    +
    +  lazy val topicIndices: DataFrame = lda.describeTopics(10)
    +
    +  lazy val topics = if (vocabulary.isEmpty || vocabulary.length < 
vocabSize) {
    +    topicIndices
    +  } else {
    +    val index2term = udf { indices: mutable.WrappedArray[Int] => 
indices.map(i => vocabulary(i)) }
    +    topicIndices.select(col("topic"), 
index2term(col("termIndices")).as("term"), col("termWeights"))
    +  }
    +
    +  lazy val isDistributed: Boolean = lda.isDistributed
    +  lazy val vocabSize: Int = lda.vocabSize
    +  lazy val docConcentration: Array[Double] = 
lda.getEffectiveDocConcentration
    +  lazy val topicConcentration: Double = lda.getEffectiveTopicConcentration
    +
    +  override def write: MLWriter = new LDAWrapper.LDAWrapperWriter(this)
    +}
    +
    +private[r] object LDAWrapper extends MLReadable[LDAWrapper] with Logging {
    +
    +  val TOKENIZER_COL = s"${Identifiable.randomUID("rawTokens")}"
    +  val STOPWORDS_REMOVER_COL = s"${Identifiable.randomUID("tokens")}"
    +  val COUNT_VECTOR_COL = s"${Identifiable.randomUID("features")}"
    +
    +  private def getPreStages(
    +      features: String,
    +      customizedStopWords: Array[String],
    +      maxVocabSize: Int): Array[PipelineStage] = {
    +    val tokenizer = new RegexTokenizer()
    +      .setInputCol(features)
    +      .setOutputCol(TOKENIZER_COL)
    +    val stopWordsRemover = new StopWordsRemover()
    +      .setInputCol(TOKENIZER_COL)
    +      .setOutputCol(STOPWORDS_REMOVER_COL)
    +    stopWordsRemover.setStopWords(stopWordsRemover.getStopWords ++ 
customizedStopWords)
    +    val countVectorizer = new CountVectorizer()
    +      .setVocabSize(maxVocabSize)
    +      .setInputCol(STOPWORDS_REMOVER_COL)
    +      .setOutputCol(COUNT_VECTOR_COL)
    +
    +    Array(tokenizer, stopWordsRemover, countVectorizer)
    +  }
    +
    +  def fit(
    +      data: DataFrame,
    +      features: String,
    +      k: Int,
    +      maxIter: Int,
    +      optimizer: String,
    +      subsamplingRate: Double,
    +      topicConcentration: Double,
    +      docConcentration: Array[Double],
    +      customizedStopWords: Array[String],
    +      maxVocabSize: Int): LDAWrapper = {
    +
    +    val lda = new LDA()
    +      .setK(k)
    +      .setMaxIter(maxIter)
    +      .setSubsamplingRate(subsamplingRate)
    +
    +    val featureSchema = data.schema(features)
    +    val stages = featureSchema.dataType match {
    +      case d: StringType =>
    +        logDebug(s"Feature ($features) schema is StringType, use the 
built-in preprocessor.")
    --- End diff --
    
    Remove this debug message? Other wrappers do not have LogDebug messages.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to