【问题标题】:Edit Distance between the words comparing between two Strings编辑两个字符串之间比较的单词之间的距离
【发布时间】:2019-02-13 18:38:20
【问题描述】:

我从互联网上看到了很多资源,但找不到确切的帮助。我试图找出两个字符串示例之间的编辑距离: String a = "把回车放在段落之间 gioo"; String b = "在电话 gio 之间打个招呼"; 在这里,我总是将字符串 a 与另一个字符串进行比较,所以这里的编辑距离应该是 4。 我已经完成了一些代码执行,它将我与字符串中的每个字符进行了比较。

                           int len1 = row10.length();
                            int len2 = row01.length();
                            int[][] dp = new int[len1 + 1][len2 + 1];

                            for (int i = 0; i <= len1; i++) {
                                dp[i][0] = i;
                            }

                            for (int j = 0; j <= len2; j++) {
                                dp[0][j] = j;
                            }

                            for (int i = 0; i < len1; i++) {
                                char c1 = row10.charAt(i);
                                for (int j = 0; j < len2; j++) {
                                    char c2 = row01.charAt(j);
                                    if (c1 == c2) {
                                        dp[i + 1][j + 1] = dp[i][j];
                                    } else {
                                        int replace = dp[i][j] + 1;
                                        int insert = dp[i][j + 1] + 1;
                                        int delete = dp[i + 1][j] + 1;
                                        int min = replace > insert ? insert : replace;
                                        min = delete > min ? min : delete;
                                        dp[i + 1][j + 1] = min;
                                    }
                                }
                            }
                            System.out.println(dp[len1][len2]);

【问题讨论】:

  • 但问题是什么?
  • 我发布的程序是比较两个字符串的完整编辑距离,我只想要单词。例如:字符串 a = "hello elina",字符串 b = "hello gordon"。我只需要 1 个编辑,但程序给我 7 个空格。我只需要没有空格的单词的编辑距离。
  • 我正在使用 excel 表格列输入它有大量数据,所以我不能将所有空格都放入空字符串
  • 解决方案的基本概述是使用split 将每个输入划分为一个单词数组,然后以与现有逻辑遍历字符完全相同的方式遍历这些单词,但使用String 类的equals 方法而不是== 来比较它们。
  • @Juke,所以你想计算第一次不匹配之前的单词数?例如。 Sent 1 = My name is XYZ. &amp; Sent 2 = My name ABC 。那么输出应该是 2。我猜对了吗?

标签: java arrays


【解决方案1】:

制作了一个示例函数。它并没有真正考虑到极端情况,但它确实有效。另外,请考虑单词的区分大小写。

package test;

public class CalcWordDiff {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String a = "My name is ABC.";
        String b = "My name xyz.";
        System.out.println("Edit distance will be : "+calcDistanceBetweenWords(a,b));
    }

    public static int calcDistanceBetweenWords(String first, String second)
    {
        int res = 0;
        String[] words_string_first = first.trim().split(" "); // By trim, I removed the Whitespaces if they exist
        String[] words_string_second = second.trim().split(" ");
        //Check the length of both the arrays
        System.out.println("Size of arrays first is : "+words_string_first.length);
        System.out.println("Size of arrays second is : "+words_string_second.length);
        int lowerWordSentSize = 0;
        if(words_string_first.length<=words_string_second.length)
        {
            lowerWordSentSize = words_string_first.length;
        }
        else
        {
            lowerWordSentSize = words_string_second.length;
        }
        //Now iterate through the array of lower size
        for(int i = 0; i< lowerWordSentSize; i++)
        {
            if(words_string_first[i].equals(words_string_second[i]))
            {
                //Do nothing, it means both the words are same
            }
            else
            {
                System.out.println("Words mismatched at "+(i+1)+" th Position.");
                res = i; 
            }
        }
        return res;
    }

}

【讨论】:

  • 在问题评论中,您提到了其他内容。这段代码就是按照那个来的。
  • 现在我明白了。上面的代码也可以,只需将输出减去长度即可。例如,Sent 1 = My name is ABC XYZ and Sent 2 = My name POI。现在上面的代码将返回 2,但您需要的是 3,因为您需要发送 2 来读取“MY name is ABC XYZ”,因此编辑距离为 3。为了得到它,只需将输出减去更大句子的长度。所以它会是5-2 = 3,你想要的输出。如果你明白了,请告诉我。
  • 它实际上抛出了 arrayIndexOutOfBound 异常,我认为我必须通过 res++ 捕获异常,但它显示错误的输出
  • 它工作了我用try catch包围它并使用了highestWordSentSize
猜你喜欢
  • 1970-01-01
  • 2015-05-06
  • 1970-01-01
  • 2020-08-05
  • 2017-08-11
  • 1970-01-01
  • 2013-04-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多