Skip to content

Commit 9f2bf89

Browse files
authored
Merge pull request sergi#37 from sergi/19-DiffCleanupSemantic
Adapt DiffCleanupSemantic to be equal to the Python version
2 parents 17df971 + ebe5b83 commit 9f2bf89

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

diffmatchpatch/dmp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,7 @@ func (dmp *DiffMatchPatch) DiffCleanupSemantic(diffs []Diff) []Diff {
967967
float64(overlapLength2) >= float64(len(insertion))/2 {
968968
// Reverse overlap found.
969969
// Insert an equality and swap and trim the surrounding edits.
970-
overlap := Diff{DiffEqual, insertion[len(insertion)-overlapLength2:]}
970+
overlap := Diff{DiffEqual, deletion[:overlapLength2]}
971971
diffs = append(
972972
diffs[:pointer],
973973
append([]Diff{overlap}, diffs[pointer:]...)...)

diffmatchpatch/dmp_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,21 @@ func Test_diffCleanupSemantic(t *testing.T) {
680680
Diff{DiffDelete, "A"},
681681
Diff{DiffEqual, "3"},
682682
Diff{DiffInsert, "BC"}}, diffs)
683+
684+
// Test case for adapting DiffCleanupSemantic to be equal to the Python version #19
685+
diffs = dmp.DiffMain("James McCarthy close to signing new Everton deal", "James McCarthy signs new five-year deal at Everton", false)
686+
diffs = dmp.DiffCleanupSemantic(diffs)
687+
assertDiffEqual(t, []Diff{
688+
Diff{DiffEqual, "James McCarthy "},
689+
Diff{DiffDelete, "close to "},
690+
Diff{DiffEqual, "sign"},
691+
Diff{DiffDelete, "ing"},
692+
Diff{DiffInsert, "s"},
693+
Diff{DiffEqual, " new "},
694+
Diff{DiffInsert, "five-year deal at "},
695+
Diff{DiffEqual, "Everton"},
696+
Diff{DiffDelete, " deal"},
697+
}, diffs)
683698
}
684699

685700
func Test_diffCleanupEfficiency(t *testing.T) {

0 commit comments

Comments
 (0)