regex - Java : Checking if 2 text-paragraphs are different giving error at delete sentence -
i working on java application note-taking. now, whenever user edits text in note, want find difference between oldtext , newtext can add history of note.
for dividing each paragraph multiple strings splitting them @ dot. comparing sentences in string-list using diff-match-patch.
as of works fine add, edit in text, delete sentence, gives problem.
situation
old text : sentence1, sentence2, sentence3, sentence4 new text : sentence1, sentence3, sentence4.
but because of that, comparator sees sentence2 replaced sentence3, sentence3 sentence4 , so-on , so-forth.
this not desired behaviour, don't know how remedy situation. post code, kindly let me know how can differences between them properly.
groupnotehistory object in saving oldtext , newtext changes only. hope code understandable.
// below list of oldtext , newtext splitted @ dot. list<string> oldtextlist = arrays.aslist(mnotes1.getmnotetext().split("(\\.|\\n)")); list<string> newtextlist = arrays.aslist(mnotes.getmnotetext().split("(\\.|\\n)")); // calculating size of loop. int counter = math.max(oldtextlist.size(), newtextlist.size()); string oldstring; string newstring; (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 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 can do. lot. :-)
instead of reinventing wheel can use library, i.e one: https://code.google.com/p/java-diff-utils/
you use it's diffutils.diff method adding sentences input, should want 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("[\\.,]"))); (delta<string> delta: patch.getdeltas()) { system.out.println(delta); }; //outputs //[deletedelta, position: 1, lines: [ sentence 2]] //[changedelta, position: 3, lines: [ sentence 4] [ sentence 5]] } }
Comments
Post a Comment