samol
samol

Reputation: 20610

How to chain operations in idiomatic scala

I want to apply a list of regex to a string. My current approach is not very functional

My current code:

  val stopWords = List[String](
    "the",
    "restaurant",
    "bar",
    "[^a-zA-Z -]"
  )

  def CanonicalName(name: String): String = {
    var nameM = name        
    for (reg <- stopWords) {
      nameM = nameM.replaceAll(reg, "")
    }

    nameM = nameM.replaceAll(" +", " ").trim
    return nameM
  }

Upvotes: 1

Views: 330

Answers (2)

Joseph Guan
Joseph Guan

Reputation: 106

'replaceAll' has the possiblity to replace part of a word, for example: "the thermal & barbecue restaurant" is replaced to "rmal becue". If what you want is "thermal barbecue", you may split the name first and then apply your stopwords rules word by word:

def isStopWord(word: String): Boolean = stopWords.exists(word.matches)

def CanonicalName(name: String): String = 
    name.replaceAll(" +", " ").trim.split(" ").flatMap(n => if (isStopWord(n)) List() else List(n)).mkString(" ")

Upvotes: 0

jwvh
jwvh

Reputation: 51271

I think this does what you're looking for.

def CanonicalName(name: String): String = {
  val stopWords = List("the", "restaurant", "bar", "[^a-zA-Z -]")
  stopWords.foldLeft(name)(_.replaceAll(_, "")).replaceAll(" +"," ").trim
}

Upvotes: 2

Related Questions