I am working on a Java 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.
Code :
String oldText = "";
String newText = "";
if(!(oldNotes.getNotetext().equals(newNotes.getNotetext()))){
String[] strings = newNotes.getNotetext().split(".");
if(strings.length>0){
String[] oldNotesArray = oldNotes.getNotetext().split(".");
if(oldNotesArray.length>0){
for(int i=0; i<Math.min(strings.length,oldNotesArray.length);i++){
if(!(strings[i].contains(oldNotesArray[i]))){
oldText += oldNotesArray[i];
newText += strings[i];
}
}
}
noteHistory.setNewText(newText);
noteHistory.setHistoryText(oldText);
} else {
noteHistory.setNewText(newNotes.getNotetext());
noteHistory.setHistoryText(oldNotes.getNotetext());
}
}
So basically in both the Strings I would just like to save which sentences have been changed. That's all. Kindly let me know. Thanks. I am just using a dot separator for convenience, if there is any other advanced Regular Expression, I don't mind using it. Thanks a lot. :-)
Edit As per the suggestions, I am trying to use the google diff-match-patch library. This is what I was able to find, but still no success in isolating the differences. I have to persist them in database, so I will have to mark them as well, but I don't have them yet. My code :
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)
oldText += d.text;
else if(d.operation== diff_match_patch.Operation.INSERT)
newText += d.text;
else
{
oldText += d.text;
newText += d.text;
}
}
System.out.println(oldText);
System.out.println(newText);
Now when I print the text, I can see the whole 2 paragraphs. What am I doing wrong?