3

I have found Apache's impelementation of Soundex and Metaphone in Java but I would prefer to keep the text comparison libraries I am using in Scala only if possible. Google searches have yielded me nothing useful in finding either of these algorithms in Scala.

Worst case scenario I can translate these algorithms into Scala but that is less than ideal.

http://commons.apache.org/codec/

Commander
  • 1,322
  • 2
  • 13
  • 29
  • Java libraries are 100% compatible with Java, you want your project to be "purely" Scala or something is not working? – Tomasz Nurkiewicz Nov 10 '12 at 19:55
  • 1
    It's a great question. Two things: 1. The Java library I found has dependencies, and those have dependencies. I don't want an entire 100 file library in my project just for 2 functions if I don't have to 2. I feel like things can get messy fast if our team has some related functions in java and some in scala all scattered around. I would rather reserve doing that for times when we actually have to, not for something as well-known and common as metaphone or soundex. – Commander Nov 10 '12 at 19:59

2 Answers2

3

You are looking for Stringmetric from https://stackoverflow.com/users/554647/rocky-madden :

https://github.com/rockymadden/stringmetric

Community
  • 1
  • 1
jwinandy
  • 1,739
  • 12
  • 22
2

Not to answer my own question or anything but a viable option would be to utilize a Java library and create some companion objects in scala to help expose them more appropriately and to allow to code to document itself more effectively.

//Metaphone companion object for org.apache.commons.codec.language.Metaphone in /lib/commons-codec-1.7
object Metaphone {
  val metaphone = new Metaphone
  metaphone setMaxCodeLen 5

  def encode(str:String) : String = {
    metaphone encode str
  }
}

Implementation:

val str_meta = Metaphone encode "Starbucks"
Commander
  • 1,322
  • 2
  • 13
  • 29
  • 1
    +1 That's recommended when you want to interop with a Java lib. Never call the Java api directly. They are techniques to not even wrap object and still having a descent API. – jwinandy Nov 10 '12 at 21:40