1

I have created a UDF in Scala (that im using with Spark btw) in order to get as a parameter a string and output a BeiderMorseEncoder string. I am using the org.apache.commons.codec.language.bm.BeiderMorseEncoder Java function from Apache Commons

import org.apache.commons.codec.language.bm.BeiderMorseEncoder
class BeiderMorseEncode extends UDF1[String, String] {
  override def call(input: String): String = {
    val m = new BeiderMorseEncoder()
    m.encode(input)
  }
}

object BeiderMorseEncode {
  def apply(): BeiderMorseEncode = {
    new BeiderMorseEncode()
  }
}

It works great! However I want to also use the following function (click to see signature) org.apache.commons.codec.language.bm.Lang.guessLanguage

if I try to create a similar UDF for this function in Scala like this :

import org.apache.commons.codec.language.bm.Lang
class guessNameLanguage extends UDF1[String, String] {
  override def call(input: String): String = {


    val m = new Lang()
    m.guessLanguage(input)
  }
}

object guessNameLanguage {
  def apply(): guessNameLanguage = {
    new guessNameLanguage()
  }
}

I am getting

org.apache.commons.codec.language.bm.Lang does not have a constructor

Any ideas on how can I make this work? I undersntand that I need to instantiate an object that has a constructor first ... but having had a look on the class hierarchy I dont see what object that would be. (it is obviously not Lang)

Apologies for my cringy Scala.

mamonu
  • 392
  • 3
  • 19

1 Answers1

2

If you look closely at the Javadoc you will see that the class provides two static methods to get instances of it.

So your code should end up being like this:

import org.apache.commons.codec.language.bm.{Lang, NameType}

val m = Lang.instance(NameType.GENERIC)
m.guessLanguage(input)
  • Many thanks this did the trick. Also this will help me a bit more understanding how to access similar functions – mamonu Nov 05 '21 at 21:05