Home > Back-end >  Scala: Calculate levenshtein distance between 2 strings
Scala: Calculate levenshtein distance between 2 strings

Time:02-14

I am trying to calculate Levenshteine distance between 2 strings.

I got the the Java code which is working fine as below

static int compute_Levenshtein_distance(String str1,
                                            String str2)
    {
        if (str1.isEmpty()) {
            return str2.length();
        }

        if (str2.isEmpty())
        {
            return str1.length();
        }

        int replace = compute_Levenshtein_distance(
                str1.substring(1), str2.substring(1))
                  NumOfReplacement(str1.charAt(0),str2.charAt(0));

        int insert = compute_Levenshtein_distance(
                str1, str2.substring(1))  1;


        int delete = compute_Levenshtein_distance(
                str1.substring(1), str2)  1;


        return minm_edits(replace, insert, delete);
    }

    static int NumOfReplacement(char c1, char c2)
    {

        return c1 == c2 ? 0 : 1;
    }

    static int minm_edits(int... nums)
    {
        return Arrays.stream(nums).min().orElse(
                Integer.MAX_VALUE);
    }

    public static void main(String args[])
    {
        String s1 = "glomax";
        String s2 = "folmax";

        System.out.println(compute_Levenshtein_distance(s1, s2));
    }

now I have converted above code to scala code as below but getting a below 2 error I have commented

def compute_Levenshtein_distance(str1: String, str2: String): Int = {
    if (str1.isEmpty) return str2.length

    if (str2.isEmpty) return str1.length

    val replace = compute_Levenshtein_distance(str1.substring(1), str2.substring(1))   NumOfReplacement(str1.charAt(0), str2.charAt(0))

    val insert = compute_Levenshtein_distance(str1, str2.substring(1))   1

    val delete = compute_Levenshtein_distance(str1.substring(1), str2)   1

    minm_edits(replace.toInt, insert.toInt, delete.toInt) // **Error1:- return type expected is INt but found Any**
  }

  def NumOfReplacement(c1: Char, c2: Char) = {

    if (c1 == c2) 0
    else 1
  }

    def minm_edits(nums: Int*) = { 
      Arrays.stream(nums).min.orElse(Integer.MAX_VALUE)  // **Error2:- Cannot resolve overloaded method 'Stream'** 
    }

  def main(args: Array[String]) = {
    val s1 = "glomax"
    val s2 = "folmax"
    System.out.println(compute_Levenshtein_distance(s1, s2))
  }

As I am at very beginner level in scala...I am not getting understand how could I resolve it.

Does anyone please help me out for this?

CodePudding user response:

The reason you have error #2 is that you are trying to pass a scala Seq[Int] to the java standard library Array.streams function which does not accept scala collections.

Because of this, the return type of minm_edits is not inferred as an Int so error #1 is telling you that although you have declared the return type of compute_Levenshtein_distance as an int, the scala compiler can not tell that minm_edits returns an int.

By fixing minm_edits (using the scala standard library rather than the Java one) both errors will go away.

def minm_edits(nums: Int*) = {
  nums.minOption.getOrElse(Integer.MAX_VALUE)
}
  • Related