0

I am working on a Java application which is for note-taking. Now, whenever the user edits the text in the note, I want to find the difference between the oldText and the newText so I can add it to history of that note.

For this I am dividing each paragraph into multiple strings by splitting them at dot. Then I am comparing the sentences in that String-list using diff-match-patch.

As of now it works fine for add, edit in the text, but as soon as I delete a sentence, it gives a problem.

Situation is

old text : sentence1, sentence2, sentence3, sentence4
new Text : sentence1, sentence3, sentence4.

But because of that, the comparator sees that sentence2 is replaced by sentence3, sentence3 by sentence4 and so-on and so-forth.

This is not the desired behaviour, but I don't know how to remedy the situation. I will post my code, kindly let me know how I can just get differences between them properly.

GroupNoteHistory is the object in which I am saving the oldText and the newText changes only. I hope my code is understandable.

// Below is List of oldText and newText splitted at dot. 
       List<String> oldTextList = Arrays.asList(mnotes1.getMnotetext().split("(\\.|\\n)"));
            List<String> newTextList = Arrays.asList(mnotes.getMnotetext().split("(\\.|\\n)"));

// Calculating the size of loop.
            int counter = Math.max(oldTextList.size(), newTextList.size());
            String oldString;
            String newString;
            for (int current = 0; current < counter; current++) {
                oldString = "";
                newString = "";
                if (oldTextList.size() <= current) {
                    oldString = "";
                    newString = newTextList.get(current);

                } else if (newTextList.size() <= current) {
                    oldString = oldTextList.get(current);
                    newString = "";
                } else {
// isLineDifferent comes from diff_match_patch
                    if (isLineDifferent(oldTextList.get(current), newTextList.get(current))) {
                        noEdit = true;
                        groupNoteHistory.setWhatHasChanged("textchange");
                        oldString += oldTextList.get(current);
                        newString += newTextList.get(current);
                    }
                }

                if (oldString != null && newString != null) {
                    if (!(groupNoteHistory.getNewNoteText() == null)) {
                        if (!(newString.isEmpty())) {
                            groupNoteHistory.setNewNoteText(groupNoteHistory.getNewNoteText() + " " + newString);
                        }
                    } else {
                        groupNoteHistory.setNewNoteText(newString);
                    }
                    if (!(groupNoteHistory.getOldText() == null)) {
                        if (!(oldString.isEmpty())) {
                            groupNoteHistory.setOldText(groupNoteHistory.getOldText() + " " + oldString);
                        }
                    } else {
                        groupNoteHistory.setOldText(oldString);
                    }
                }

Kindly let me know what I can do. Thanks a lot. :-)

We are Borg
  • 5,117
  • 17
  • 102
  • 225

1 Answers1

0

Instead of reinventing the wheel you can use a library, i.e this one: https://code.google.com/p/java-diff-utils/

You could use it's DiffUtils.diff method adding sentences as input, it should do exactly what you want to achieve, see test below.

import difflib.Delta;
import difflib.DiffUtils;
import difflib.Patch;
import org.junit.Test;

import java.util.Arrays;

public class DiffUtilsTest {
    public String note1 = "Sentence 1, sentence 2, sentence 3, sentence 4";
    public String note2 = "Sentence 1, sentence 3, sentence 5";

    @Test
    public void testDiff() {
        Patch<String> patch = DiffUtils.diff(Arrays.asList(note1.split("[\\.,]")), Arrays.asList(note2.split("[\\.,]")));

        for (Delta<String> delta: patch.getDeltas()) {
            System.out.println(delta);
        };
        //outputs
        //[DeleteDelta, position: 1, lines: [ sentence 2]]
        //[ChangeDelta, position: 3, lines: [ sentence 4] to [ sentence 5]]

    }
}
Mariusz Sakowski
  • 3,232
  • 14
  • 21
  • Which libraries you have for Patch and Delta? – We are Borg Oct 27 '15 at 16:26
  • they are all contained in java-diff-utils library I linked to in the beginning of the answer. I added import list to code snippet for clarity. – Mariusz Sakowski Oct 27 '15 at 16:27
  • Ok, I needed a recent library for that. Okay, but I don't want to it in some delta format, but rather what has changed so I can insert it into two different variables for History. Can you advice me what I should change in your code for that. Thank you. – We are Borg Oct 27 '15 at 16:32
  • I printed the delta, the original, the revised, but It has a lot of extra information which as far as I understand cannot be removed. The only thing I am looking for is difference in sentences directly. I hope I am not intruding. – We are Borg Oct 27 '15 at 16:39