java - 将java代码转换为c#

标签 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;
    }

这是 Java 代码,我想在 C# 中执行此代码 将代码转换成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)

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

最佳答案

将此功能添加到您的项目中

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];
        }

Source

并将.Length()更改为.Length因为String.Length是属性而不是方法

关于java - 将java代码转换为c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39850171/

相关文章:

java - 如何显示信德语字体?

java - 如何在java编程中将最近的值存储在变量中?

c# - 对分数和带分数列表进行排序

c# - 带有 ref/out 参数的方法的异步版本的元组与自定义结构

java - 在 Java 中使用套接字解析和发送 HTTP 请求的正确方法是什么?

java - 如何在 Visual Studio Code 中找到 Java 的类路径?

C# - 我应该如何将 datagridview 组合框添加到数据表并在 datagridview 中预览它?

c# - OracleParameter 和 IN 子句

c# - 为什么这种明显的无限递归不给出编译器警告?

java - Java 中的 SSL - 没有 RSA?