Java:在Google diff match补丁中获得有效的句子 我正在开发一个Java应用程序,我想在其中比较2 段落,得到两个字符串中不同的句子 可供比较。现在我可以得到插入的内容和 删除。我面临的问题是,我想得到 而不仅仅是文字

Java:在Google diff match补丁中获得有效的句子 我正在开发一个Java应用程序,我想在其中比较2 段落,得到两个字符串中不同的句子 可供比较。现在我可以得到插入的内容和 删除。我面临的问题是,我想得到 而不仅仅是文字,java,regex,string,string-comparison,google-diff-match-patch,Java,Regex,String,String Comparison,Google Diff Match Patch,例如: 老串:敏捷的棕色狐狸跳过了懒惰的兔子。好奇心害死了猫 新串:敏捷的棕色狮子跳过了懒惰的兔子。好奇心害死了猫 预期输出:敏捷的棕色狮子跳过懒惰的兔子 我现在得到的 Diff(DELETE,"fox") Diff(INSERT,"lion") 所以,我没有上下文来解释狐狸这个词是在哪里被删除的,狮子是在哪里被添加的。所以,即使有一些操作的字符左右各有15个字符也可以。 我现在拥有的代码是: diff_match_patch diffMatchPatch = new diff_match_p

例如:

  • 老串:敏捷的棕色狐狸跳过了懒惰的兔子。好奇心害死了猫
  • 新串:敏捷的棕色狮子跳过了懒惰的兔子。好奇心害死了猫 预期输出:敏捷的棕色狮子跳过懒惰的兔子

    我现在得到的

    Diff(DELETE,"fox")
    Diff(INSERT,"lion")
    
    所以,我没有上下文来解释狐狸这个词是在哪里被删除的,狮子是在哪里被添加的。所以,即使有一些操作的字符左右各有15个字符也可以。 我现在拥有的代码是:

    diff_match_patch diffMatchPatch = new diff_match_patch();
                LinkedList<diff_match_patch.Diff> deltas = diffMatchPatch.diff_main(oldText,newText);
                for(diff_match_patch.Diff d : deltas){
                    if((d.operation == diff_match_patch.Operation.DELETE) || (d.operation== diff_match_patch.Operation.INSERT)) {
                        System.out.println(d);
                    }
                }
    
    diff_match_patch diffMatchPatch=新的diff_match_patch();
    LinkedList Delta=diffMatchPatch.diff_main(旧文本,新文本);
    对于(差异匹配补丁。差异d:三角洲){
    if((d.operation==diff_match_patch.operation.DELETE)| |(d.operation==diff_match_patch.operation.INSERT)){
    系统输出打印ln(d);
    }
    }
    
    任何帮助都很好。非常感谢。:-)如果对我解释的方式有任何疑问,请告诉我

    编辑 从答案中添加了新代码:

     diff_match_patch diffMatchPatch = new diff_match_patch();
                LinkedList<diff_match_patch.Diff> deltas = diffMatchPatch.diff_main(notes1.getNotetext(),notes.getNotetext());
                for(diff_match_patch.Diff d : deltas) {
                    if ((d.operation == diff_match_patch.Operation.DELETE) || (d.operation == diff_match_patch.Operation.INSERT)) {
                        Pattern myPattern = Pattern.compile("(\\. |^)(.*" + d.text + ".*)(\\. )");
                        Matcher m = myPattern.matcher(notes1.getNotetext());
                        while (m.find()) {
                            System.out.println("Found " + d.operation + " of: " + d.text + " in sentence: " + m.group());
                        }
                    }
                }
    
    The output I am getting is wrong, something like this I am getting,
    Found DELETE of: I  in sentence: I yoyo am also working on a webapp in which the user can make changes to a text area. In this, he can either write one paragraph, one sentence. So what I am currently trying to do is to split the whole paragraph by a dot separator. Once that is done, I would like to check which sentences have changed. I am currently doing it using for loop, which is not accurate as I have to length of array to Math.minimum of both String arrays. But it is not working, I am getting zero String modified from it. Kindly let me know what I am doing wrong. 
    Found DELETE of: oyo am in sentence: I yoyo am also working on a webapp in which the user can make changes to a text area. In this, he can either write one paragraph, one sentence. So what I am currently trying to do is to split the whole paragraph by a dot separator. Once that is done, I would like to check which sentences have changed. I am currently doing it using for loop, which is not accurate as I have to length of array to Math.minimum of both String arrays. But it is not working, I am getting zero String modified from it. Kindly let me know what I am doing wrong. 
    Found DELETE of: a in sentence: akshay also working on a webapp in which the user can make changes to a text area. In this, he can either write one paragraph, one sentence. So what I am currently trying to do is to split the whole paragraph by a dot separator. Once that is done, I would like to check which sentences have changed. I am currently doing it using for loop, which is not accurate as I have to length of array to Math.minimum of both String arrays. But it is not working, I am getting zero String modified from it. Kindly let me know what I am doing wrong. 
    Found INSERT of: a in sentence: kshay also working on a webapp in which the user can make changes to a text area. In this, he can either write one paragraph, one sentence. So what I am currently trying to do is to split the whole paragraph by a dot separator. Once that is done, I would like to check which sentences have changed. I am currently doing it using for loop, which is not accurate as I have to length of array to Math.minimum of both String arrays. But it is not working, I am getting zero String modified from it. Kindly let me know what I am doing wrong. 
    Found INSERT of: r in sentence: akshay also working on a webapp in which the user can make changes to a text area. In this, he can either write one paragraph, one sentence. So what I am currently trying to do is to split the whole paragraph by a dot separator. Once that is done, I would like to check which sentences have changed. I am currently doing it using for loop, which is not accurate as I have to length of array to Math.minimum of both String arrays. But it is not working, I am getting zero String modified from it. Kindly let me know what I am doing wrong. 
    Found DELETE of: ks in sentence: akshay also working on a webapp in which the user can make changes to a text area. In this, he can either write one paragraph, one sentence. So what I am currently trying to do is to split the whole paragraph by a dot separator. Once that is done, I would like to check which sentences have changed. I am currently doing it using for loop, which is not accurate as I have to length of array to Math.minimum of both String arrays. But it is not working, I am getting zero String modified from it. Kindly let me know what I am doing wrong. 
    Found DELETE of: ay in sentence: akshay also working on a webapp in which the user can make changes to a text area. In this, he can either write one paragraph, one sentence. So what I am currently trying to do is to split the whole paragraph by a dot separator. Once that is done, I would like to check which sentences have changed. I am currently doing it using for loop, which is not accurate as I have to length of array to Math.minimum of both String arrays. But it is not working, I am getting zero String modified from it. Kindly let me know what I am doing wrong. 
    Found INSERT of: ul in sentence: akshay also working on a webapp in which the user can make changes to a text area. In this, he can either write one paragraph, one sentence. So what I am currently trying to do is to split the whole paragraph by a dot separator. Once that is done, I would like to check which sentences have changed. I am currently doing it using for loop, which is not accurate as I have to length of array to Math.minimum of both String arrays. But it is not working, I am getting zero String modified from it. Kindly let me know what I am doing wrong. 
    Found DELETE of: In this, in sentence: rahul also working on a webapp in which the user can make changes to a text area. In this, he can either write one paragraph, one sentence. So what I am currently trying to do is to split the whole paragraph by a dot separator. Once that is done, I would like to check which sentences have changed. I am currently doing it using for loop, which is not accurate as I have to length of array to Math.minimum of both String arrays. But it is not working, I am getting zero String modified from it. Kindly let me know what I am doing wrong. 
    Found DELETE of: ang in sentence: rahul also working on a webapp in which the user can make changes to a text area.  he can either write one paragraph, one sentence. So what I am currently trying to do is to split the whole paragraph by a dot separator. Once that is done, I would like to check which sentences have changed. I am currently doing it using for loop, which is not accurate as I have to length of array to Math.minimum of both String arrays. But it is not working, I am getting zero String modified from it. Kindly let me know what I am doing wrong. 
    Found DELETE of: s in sentence: rahul also working on a webapp in which the user can make changes to a text area.  he can either write one paragraph, one sentence. So what I am currently trying to do is to split the whole paragraph by a dot separator. Once that is done, I would like to check which sentences have changed. I am currently doing it using for loop, which is not accurate as I have to length of array to Math.minimum of both String arrays. But it is not working, I am getting zero String modified from it. Kindly let me know what I am doing wrong. 
    Found INSERT of: ck in sentence: rahul also working on a webapp in which the user can make changes to a text area.  he can either write one paragraph, one sentence. So what I am currently trying to do is to split the whole paragraph by a dot separator. Once that is done, I would like to check which sentences have changed. I am currently doing it using for loop, which is not accurate as I have to length of array to Math.minimum of both String arrays. But it is not working, I am getting zero String modified from it. Kindly let me know what I am doing wrong. 
    Found DELETE of: rahul  in sentence: rahul also working on a webapp in which the user can make check to a text area.  he can either write one paragraph, one sentence. So what I am currently trying to do is to split the whole paragraph by a dot separator. Once that is done, I would like to check which sentences have changed. I am currently doing it using for loop, which is not accurate as I have to length of array to Math.minimum of both String arrays. But it is not working, I am getting zero String modified from it. Kindly let me know what I am doing wrong. 
    
    diff_match_patch diffMatchPatch=新的diff_match_patch();
    LinkedList delta=diffMatchPatch.diff_main(notes1.getNotetext(),notes.getNotetext());
    对于(差异匹配补丁。差异d:三角洲){
    if((d.operation==diff_match_patch.operation.DELETE)| |(d.operation==diff_match_patch.operation.INSERT)){
    Pattern myPattern=Pattern.compile(“(\\.\124;^)(.*+d.text+”)(\\.)”;
    Matcher m=myPattern.Matcher(notes1.getNotetext());
    while(m.find()){
    System.out.println(“+m.group()语句中的“+d.text+”中的“+d.operation+”);
    }
    }
    }
    我得到的输出是错误的,像这样,
    在句子:I yoyo中发现删除:I也正在开发一个Web应用程序,用户可以在其中更改文本区域。在这方面,他可以写一段,一句话。所以我现在要做的是用一个点分隔符把整个段落分开。完成后,我想检查一下哪些句子变了。我目前正在使用for循环,这是不准确的,因为我必须将数组的长度转换为Math.minimum两个字符串数组的长度。但它不工作,我得到零字符串修改它。请让我知道我做错了什么。
    在句子:I yoyo中发现删除:oyo am也在开发一个Web应用程序,用户可以在其中更改文本区域。在这方面,他可以写一段,一句话。所以我现在要做的是用一个点分隔符把整个段落分开。完成后,我想检查一下哪些句子变了。我目前正在使用for循环,这是不准确的,因为我必须将数组的长度转换为Math.minimum两个字符串数组的长度。但它不工作,我得到零字符串修改它。请让我知道我做错了什么。
    在句子:akshay中发现删除:a也在使用Web应用程序,用户可以在其中更改文本区域。在这方面,他可以写一段,一句话。所以我现在要做的是用一个点分隔符把整个段落分开。完成后,我想检查一下哪些句子变了。我目前正在使用for循环,这是不准确的,因为我必须将数组的长度转换为Math.minimum两个字符串数组的长度。但它不工作,我得到零字符串修改它。请让我知道我做错了什么。
    在句子:kshay中发现插入:a也在使用一个webapp,用户可以在其中更改文本区域。在这方面,他可以写一段,一句话。所以我现在要做的是用一个点分隔符把整个段落分开。完成后,我想检查一下哪些句子变了。我目前正在使用for循环,这是不准确的,因为我必须将数组的长度转换为Math.minimum两个字符串数组的长度。但它不工作,我得到零字符串修改它。请让我知道我做错了什么。
    在句子:akshay中找到了插入:r的内容,并且正在开发一个webapp,用户可以在其中更改文本区域。在这方面,他可以写一段,一句话。所以我现在要做的是用一个点分隔符把整个段落分开。完成后,我想检查一下哪些句子变了。我目前正在使用for循环,这是不准确的,因为我必须将数组的长度转换为Math.minimum两个字符串数组的长度。但它不工作,我得到零字符串修改它。请让我知道我做错了什么。
    在句子:akshay中发现删除了:ks也正在使用一个webapp,用户可以在其中更改文本区域。在这方面,他可以写一段,一句话。所以我现在要做的是用一个点分隔符把整个段落分开。完成后,我想检查一下哪些句子变了。我目前正在使用for循环,这是不准确的,因为我必须将数组的长度转换为Math.minimum两个字符串数组的长度。但它不工作,我得到零字符串修改它。请让我知道我做错了什么。
    在句子:akshay中发现删除:ay也在使用一个Web应用程序,用户可以在其中更改文本区域。在这方面,他可以写一段,一句话。所以我现在要做的是用一个点分隔符把整个段落分开。一旦完成,我想
    
    //-------------------------Example Strings---------------------------------------------
      private static String oldText = "I yoyo am also working on a \n webapp in which the user can make changes to a text area. " +
          "In this, he can either write one paragraph, one sentence." +
          " So what I am currently trying to do is to split the whole paragraph by a dot separator. " +
          "Once that is done, I would like to check which sentences have changed." +
          " I am currently doing it using for loop, which is not accurate as I have to length of array to Math.minimum of both String arrays." +
          " But it is not working, I am getting zero String modified from it."+
          " Kindly let me know what I am doing wrong.";
    
      private static String newText = "akshay is also working on a \n webapp in which the user can make changes to a text area. " +
          "He can either write one paragraph, one sentence." +
          " So what I am currently trying to do is to split the whole paragraph by a dot separator. " +
          "Once that is done, I would like to check which sentences have changed." +
          " I am currently doing it using for loop, which is not accurate as I have to length of array to Math.minimum of both String arrays." +
          " But it is not working, I am getting zero String modified from it.";
      //-------------------------Example Strings end --------------------------------------
    
      private static diff_match_patch diffMatchPatch;
    
      public static void main(String[] args) {
    
        diffMatchPatch = new diff_match_patch();
        //Split text into List of strings
        List<String> oldTextList = Arrays.asList(oldText.split("(\\.|\\n)"));
        List<String> newTextList = Arrays.asList(newText.split("(\\.|\\n)"));
    
        //If we have different length
        int counter = Math.max(oldTextList.size(), newTextList.size()); 
        StringBuilder sb = new StringBuilder();
    
        for(int current = 0; current < counter; current++){
          String oldString = null;
          String newString = null;
    
          if(oldTextList.size() <= current){
            oldString = "";
            newString = newTextList.get(current);
    
          } else if (newTextList.size() <= current){
            oldString = oldTextList.get(current);
            newString = "";
          } else {
            if (isLineDifferent(oldTextList.get(current), newTextList.get(current))){
              oldString = oldTextList.get(current);
              newString = newTextList.get(current);
            }
          }
          if(oldString != null && newString != null) {
            //---- Insert into database here -----
            sb.append("Changes for Line: " + current + "\n");
            sb.append("Old: " + oldString + "; New: " + newString +";\n");
          }
        }
    
        System.out.println(sb.toString());
      }
    
      private static boolean isLineDifferent(String oldString, String newString) {
        LinkedList<diff_match_patch.Diff> deltas = diffMatchPatch.diff_main(oldString,newString);
        for(diff_match_patch.Diff d : deltas){
          if (d.operation == diff_match_patch.Operation.EQUAL) continue;
          return true;
          }
        return false;
        }
      }
    
    Changes for Line: 0
    Old: I yoyo am also working on a ; New: akshay is also working on a ;
    Changes for Line: 2
    Old:  In this, he can either write one paragraph, one sentence; New:  He can either write one paragraph, one sentence;
    Changes for Line: 8
    Old:  Kindly let me know what I am doing wrong; New: ;