文字比较算法还是程序? [英] Text comparison algorithm or program?
问题描述
我有两个带有句子的段落,我想比较两个段落并想显示UI的差异。
I am having Two paragraphs which are having sentences, I wanted to compare both the paragraphs and want to show the differences at UI.
以下是可能的用法-案件,我可以考虑。
Below are the possible use-cases, Which I can think about. Any help in algorithm or code will be appreciable.
案例1:从str2中删除单词
Case 1: Word Deleted from str2
String str1 = "Hello I am new How are you";
String str2 = "How are you Hello";
output :
str1 = "<del>Hello I am new</del> How are you";
str2 = "How are you <add>Hello</add>"
案例2:将单词添加到str2
Case 2: Word added to str2
String str1 = "Hello How are you what about you";
String str2 = "How are you I am fine what about you";
output :
str1 = "<del>Hello</del> How are you what about you";
str2 = "How are you <add>I am fine</add> what about you"
情况3:单词相等
String str1 = "Hello How are you";
String str2 = "Hello How rea you";
output :
str1 = "Hello How <missmatch>are</missmatch> you";
str2 = "Hello How <missmatch>rea</missmatch> you"
推荐答案
您可以例如查看: https://github.com/wumpz/java-diff-utils 及其示例 https://github.com/wumpz/java-diff- utils / wiki / Examples 。修改以包括您的特定标签而不是标记特征很容易:例如
You could e.g. look at: https://github.com/wumpz/java-diff-utils and to its examples https://github.com/wumpz/java-diff-utils/wiki/Examples. The modification to include your specific tags instead of markup charactars is easy: e.g.
DiffRowGenerator generator = DiffRowGenerator.create()
.showInlineDiffs(true)
.mergeOriginalRevised(true)
.inlineDiffByWord(true)
.newTag(f -> f?"<span style=\"background-color:#ffc6c6\">":"</span>")
.oldTag(f -> f?"<span style=\"background-color:#c4ffc3\">":"</span>")
.columnWidth(10000000)
.build();
List<DiffRow> rows = generator.generateDiffRows(
Arrays.asList(lines.get(0)),
Arrays.asList(lines.get(1)));
System.out.println(rows.get(0).getOldLine());
这篇关于文字比较算法还是程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!