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

    https://github.com/apache/spark/pull/10272#discussion_r47496959
  
    --- Diff: mllib/src/main/scala/org/apache/spark/ml/feature/Stemmer.scala ---
    @@ -0,0 +1,260 @@
    +/*
    + * 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.feature
    +
    +import org.apache.spark.annotation.{Experimental, Since}
    +import org.apache.spark.ml.UnaryTransformer
    +import org.apache.spark.ml.param.ParamMap
    +import org.apache.spark.ml.util.{DefaultParamsReadable, 
DefaultParamsWritable, Identifiable}
    +import org.apache.spark.sql.types.{ArrayType, DataType, StringType}
    +
    +/**
    + * :: Experimental ::
    + * Stemmer removes the commoner morphological and inflexional endings from 
words in English
    + */
    +@Experimental
    +@Since("1.7.0")
    +class Stemmer (override val uid: String)
    +  extends UnaryTransformer[Seq[String], Seq[String], Stemmer] with 
DefaultParamsWritable {
    +  def this() = this(Identifiable.randomUID("stemmer"))
    +
    +  override protected def createTransformFunc: Seq[String] => Seq[String] = 
{
    +    terms => terms.map(t => PorterStemmer(t))
    +  }
    +
    +  override protected def validateInputType(inputType: DataType): Unit = {
    +    require(inputType.sameType(ArrayType(StringType)),
    +      s"Input type must be ArrayType(StringType) but got $inputType.")
    +  }
    +
    +  override protected def outputDataType: DataType = new 
ArrayType(StringType, true)
    +
    +  override def copy(extra: ParamMap): Stemmer = defaultCopy(extra)
    +}
    +
    +@Since("1.7.0")
    +object Stemmer extends DefaultParamsReadable[Stemmer] {
    +
    +  @Since("1.7.0")
    +  override def load(path: String): Stemmer = super.load(path)
    +}
    +
    +/**
    + * :: Experimental ::
    + * Classical Porter stemmer, which is implemented referring to 
scalanlp/chalk
    + * 
[[https://github.com/scalanlp/chalk/blob/master/src/main/scala/chalk/text/analyze]].
    + * The details of PorterStemmer can be found at
    + * [[http://snowball.tartarus.org/algorithms/porter/stemmer.html]].
    + */
    +private[feature] object PorterStemmer {
    +
    +  def apply(w: String): String = {
    +    if (w.length < 3) w.toLowerCase
    +    else {
    +      val ret = w.toLowerCase.replaceAll("([aeiou])y", 
"$1Y").replaceAll("^y", "Y")
    +      step5(step4(step3(step2(step1(ret))))).toLowerCase
    +    }
    +  }
    +
    +  private def step1(w: String): String = step1c(step1b(step1a(w)))
    +
    +  private def step1a(w: String): String = {
    +    if (w.endsWith("sses") || w.endsWith("ies")) {
    +      w.substring(0, w.length - 2)
    +    }
    +    else if (w.endsWith("s") && w.charAt(w.length - 2) != 's') {
    +      w.substring(0, w.length - 1)
    +    }
    +    else w
    +  }
    +
    +  private def step1b(w: String): String = {
    +    def extra(w: String) = {
    +      if (w.endsWith("at") || w.endsWith("bl") || w.endsWith("iz")) w + 'e'
    +      else if (doublec(w) && !"lsz".contains(w.last)) w.substring(0, 
w.length - 1)
    +      else if (m(w) == 1 && cvc(w)) w + "e"
    +      else w
    +    }
    +
    +    if (w.endsWith("eed")) {
    +      if (m(w.substring(0, w.length - 3)) > 0) w.substring(0, w.length - 
1) else w
    +    } else if (w.endsWith("ed")) {
    +      if (w.indexWhere(isVowel) < (w.length - 2)) extra(w.substring(0, 
w.length - 2))
    +      else w
    +    } else if (w.endsWith("ing")) {
    +      if (w.indexWhere(isVowel) < (w.length - 3)) extra(w.substring(0, 
w.length - 3))
    +      else w
    +    } else w
    +  }
    +
    +  private def step1c(w: String): String = {
    +    if ((w.last == 'y' || w.last == 'Y') && w.indexWhere(isVowel) < 
w.length - 1) {
    +      w.substring(0, w.length - 1) + 'i'
    +    } else w
    +  }
    +
    +  private def step2(w: String): String = {
    +    if (w.length < 3) w
    +    else {
    +      val opt = w(w.length - 2) match {
    +        case 'a' => replaceSuffix(w, "ational", 
"ate").orElse(replaceSuffix(w, "tional", "tion"))
    +        case 'c' =>
    +          replaceSuffix(w, "enci", "ence").orElse(replaceSuffix(w, "anci", 
"ance"))
    +        case 'e' => replaceSuffix(w, "izer", "ize")
    +        case 'g' => replaceSuffix(w, "logi", "log")
    +        case 'l' => replaceSuffix(w, "bli", "ble")
    +          .orElse(replaceSuffix(w, "alli", "al"))
    +          .orElse ( replaceSuffix(w, "entli", "ent"))
    --- End diff --
    
    The different `orElse` need to be uniformized.
    Same thing goes for `step4`.


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to