文本比较算法或程序?

我有两个段落有句子,我想比较两个段落,并希望在UI上显示差异。

以下是我可以考虑的可能用例。 任何算法或代码方面的帮助都会很明显。

在此处输入图像描述

案例1:从str2删除了Word

String str1 = "Hello I am new How are you"; String str2 = "How are you Hello"; output : str1 = "Hello I am new How are you"; str2 = "How are you Hello" 

案例2:Word添加到str2

 String str1 = "Hello How are you what about you"; String str2 = "How are you I am fine what about you"; output : str1 = "Hello How are you what about you"; str2 = "How are you I am fine what about you" 

案例3:言语是平等的

  String str1 = "Hello How are you"; String str2 = "Hello How rea you"; output : str1 = "Hello How are you"; str2 = "Hello How rea you" 

您可以查看: https : //github.com/wumpz/java-diff-utils及其示例https://github.com/wumpz/java-diff-utils/wiki/Examples 。 包含特定标签而不是标记字符的修改很容易:例如

 DiffRowGenerator generator = DiffRowGenerator.create() .showInlineDiffs(true) .mergeOriginalRevised(true) .inlineDiffByWord(true) .newTag(f -> f?"":"") .oldTag(f -> f?"":"") .columnWidth(10000000) .build(); List rows = generator.generateDiffRows( Arrays.asList(lines.get(0)), Arrays.asList(lines.get(1))); System.out.println(rows.get(0).getOldLine());