Java : Getting effected sentence in Google-diff-match-patch

标签 java regex string string-comparison google-diff-match-patch

  • 我正在开发一个 Java 应用程序,我想在其中比较 2 段落并获得2个字符串中不同的句子 进行比较。现在我可以得到插入的东西和是什么 删除。我面临的问题是,我想得到这句话 受到影响,而不仅仅是言语。

示例:

  1. Old String:敏捷的棕色狐狸跳过了懒惰的兔子。好奇害死猫。
  2. 新字符串:敏捷的棕色狮子跳过了懒惰的兔子。好奇害死猫。

预期输出:敏捷的棕色狮子跳过懒惰的兔子。

我现在得到的。

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 = 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. 

我想知道整个单词/句子何时被删除插入,以便我可以将其正确保存在数据库中。你能帮忙的话,我会很高兴。多谢。 :-)

编辑 下面提到的答案完美地获得了 2 个可以保存在数据库中的独立字符串。

最佳答案

经过广泛的重新考虑,我认为这不是正则表达式的情况。相同的更改会出现在多行中,因此您必须像这样逐行检查您的输入:

//-------------------------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: ;

请注意,我只添加了“;”作为 Stringbuilder 的分隔符号,这样您就可以辨别字符串的结束位置。这当然还有不完善的几点需要考虑:

  • 此代码针对文本中的每个点 ('.') 进行拆分。如果你不知何故有一个点不是句子的结尾,你的结果就会有偏差。
  • 与所有其他差异工具一样,行顺序中的一个开关被注册为一系列删除和插入
  • 如果您逐行阅读文本,您可能应该在行出现时输入行,而不是将它们全部收集起来然后再拆分。 (看this example)
  • 正如您在上面链接中的示例中看到的那样,存在一个更新的库,我认为它更适合您的用例。

关于Java : Getting effected sentence in Google-diff-match-patch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28858139/

相关文章:

java - 自动化服务器无法创建对象 - Microsoft JScript 运行时错误

javascript - 正则表达式中字符类中的小数转义

regex - 正则表达式的否定?

string - fish shell : validate if string contains substring

java - cookie 被阻止时 JCaptcha 失败

java - 变换+缩放一组点

string - 如何在 Golang 中将映射解压缩为字符串格式的关键字参数?

java - 如何在java中检查1是质数并且Hashmap的输出对于String类型是错误的

java - 为什么我的比较方法会抛出异常——比较方法违反了它的一般契约!

regex - Perl 正则表达式匹配具有特殊字符的字符串