转换c#中的java代码

时间:2016-10-04 10:40:46

标签: java c#

我正在使用此代码进行句子类似,代码在java上可用我想在c#中使用它。

public static int getWordChanges(String s1, String s2) {
        int similarityThreshold = 50;
        int wordChanges = 0;

        s1 = s1.toLowerCase().replace(".", "").replace(",", "").replace(";", "");
        s2 = s2.toLowerCase().replace(".", "").replace(",", "").replace(";", "");

        //Loop through each word in s1
        for (int i = 0; i < s1.split(" ").length; i++) {
            boolean exists = false;
            //Search for i'th word in s1 in s2
            for (int j = 0; j < s2.split(" ").length; j++) {
                //Is the word misspelled?
                if ((getLevenshteinDistance(s1.split(" ")[i], s2.split(" ")[j]) * 100 / s1.split(" ")[i].length()) < similarityThreshold) {
                    exists = true;
                    break;
                }
            }

            //If the word does not exist, increment wordChanges
            if (!exists) {
                wordChanges++;
            }
        }

        return wordChanges;
    }

这是我希望在c#中执行此代码的Java代码  转换c#

中的代码后
       public  int getWordChanges(String s1, String s2)
        {
            int similarityThreshold = 50;
            int wordChanges = 0;

            s1 = s1.ToLower().Replace(".", "").Replace(",", "").Replace(";", "");
            s2 = s2.ToLower().Replace(".", "").Replace(",", "").Replace(";", "");

            //Loop through each word in s1

            for (int i = 0; i < s1.Split(' ').Length; i++)
            {
                bool exists = false;
                //Search for i'th word in s1 in s2
                for (int j = 0; j < s2.Split(' ').Length; j++)
                {
                    //Is the word misspelled?
                    if ((getLevenshteinDistance(s1.Split(' ')[i], s2.Split(' ')[j]) * 100 / s1.Split(' ')[i].Length()) < similarityThreshold)
                    {
                        exists = true;
                        break;
                    }
                }

                //If the word does not exist, increment wordChanges
                if (!exists)
                {
                    wordChanges++;
                }
            }

            return wordChanges;
        }
    }
}

此行有错误

if ((getLevenshteinDistance(s1.Split(' ')[i], s2.Split(' ')[j]) * 100 / s1.Split(' ')[i].Length()) < similarityThreshold)

长度错误将显示我如何解决这个问题

1 个答案:

答案 0 :(得分:1)

将此功能添加到您的项目

test-test-test@2016-10-04.txt#

Source

并更改public static int getLevenshteinDistance(string s, string t) { int n = s.Length; int m = t.Length; int[,] d = new int[n + 1, m + 1]; // Step 1 if (n == 0) { return m; } if (m == 0) { return n; } // Step 2 for (int i = 0; i <= n; d[i, 0] = i++) { } for (int j = 0; j <= m; d[0, j] = j++) { } // Step 3 for (int i = 1; i <= n; i++) { //Step 4 for (int j = 1; j <= m; j++) { // Step 5 int cost = (t[j - 1] == s[i - 1]) ? 0 : 1; // Step 6 d[i, j] = Math.Min( Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1), d[i - 1, j - 1] + cost); } } // Step 7 return d[n, m]; } 因为String.Length是属性而不是方法