- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
示例:
预期输出:敏捷的棕色狮子跳过懒惰的兔子。
我现在得到的。
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 的分隔符号,这样您就可以辨别字符串的结束位置。这当然还有不完善的几点需要考虑:
关于Java : Getting effected sentence in Google-diff-match-patch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28858139/
我在我们的系统中使用了多种不同的查询,我们想要获取分页的“总”记录。 我所遵循的所有地方都遵循这种结构 - var query1 = "select SQL_CALC_FOUND_ROWS ...."
我是 diff 的新手。我过去用过它。但我想知道,是否可以使用 diff 来跟踪希伯来语中两个文件之间的差异?我想比较具有元音点和重音符号的圣经希伯来文文件。 最佳答案 我不知道有任何“差异”(文件比
在Word中,有一个选项可以比较两个文档并找出两个文档之间的差异。我正在寻找一个类似的功能来比较两个Powerpoint文档。理想情况下,它将列出不同的单个幻灯片,每个幻灯片之间版本之间的差异以及进行
Bizzaro-Diff!!! 有没有办法做一个只显示一组文件中相同部分的 bizzaro/inverse-diff? (即远超过 three files ) 奇怪的问题,我知道...但我正在将某人的
我将 git 与默认的 Ubuntu 12.04 软件包一起使用: git --version git version 1.7.9.5 我找不到任何可以让 git diff 打开外部差异工具的机制,它
这是我的树 ├── test │ ├── dir1 │ └── dir2 │ ├── file │ └── file2 └── test2 └── dir2
我在 source forge ( cocoa.diff ) 上找到了这个补丁,这意味着我可以使用 cocoa.diff 文件进行补丁。但是,我似乎无法弄清楚如何使用 .diff 文件。 谢谢你的帮助
我修改了某个文件的第 494 行,并使用 cvs diff -u4 来查看我修改的内容,cvs 输出如下内容: @@ -490,9 +490,9 @@ if (!(hPtr->hSta
我需要知道这两个补丁是否有效相同。 我有一个旧补丁文件和使用 unix diff 命令创建的新补丁文件。由于补丁创建时的时间戳,只是比较补丁会报告差异。 有没有办法(有差异?)可以可靠地告诉我这两个补
我有这样的东西 src/sim/simulate.cc 41d40 public: > 61,62c60,61 a.patch 用于创建补丁文件,尽管也可能会抛出一些其他开关(-N?)
我想知道是否有工具可以显示与 debian 打包相关的补丁中使用的 *.diff 文件。我需要从该工具中获得的是它可以只读取 diff 文件并显示随更改的行更改的实际文件,就像 kdiff 或 mel
主要有3个git diff版本: git diff - difference between WORKING DIRECTORY & STAGE git diff --staged - differe
根据 Ender 的 Applied Econometric Time Series ,变量 y 的二阶差分定义为: Pandas 提供了 diff 函数,它接收“periods”作为参数。尽管如此,
这是我的差异的开始部分。 #!/usr/bin/env python import fileinput import difflib import subprocess import sys # fo
有没有办法在 svn diff 或任何其他工具(基于 linux)到 仅显示空格/制表符更改 ?. 目的,我不希望 checkin 这些差异。如果工具可以捕获这些差异,我可以在 checkin 之前将
我正在尝试比较忽略回车符的文件 - diff -b 在任何其他 unix 上的表现令人钦佩。但是在这个 AIX 5.3 机器上: tst1:tst2$ od -c testfile 00000
这个问题已经有答案了: What does the “@@…@@” meta line with at signs in svn diff or git diff mean? (3 个回答) 已关闭
我在使用 git diff --word-diff 时遇到了问题。问题是当 diff 获取没有换行符的文件(单行文件)时,它会逐行区分。我想逐字区分。 以下是当我在没有换行符的情况下比较文件时发生的情
这个问题在这里已经有了答案: Highlight changed lines and changed bytes in each changed line (12 个答案) 关闭 7 年前。 给定一
我正在尝试使用两个大文件夹(〜7GB)创建一个补丁。 这是我的做法: $ diff -Naurbw . ../other-folder > file.patch 但可能由于文件大小而导致未创建补丁并给
我是一名优秀的程序员,十分优秀!