5

I would like to compare two sentences (sentences A and B), such that the program would output the changes made on sentence B from sentence A. For example:

sentence A: It's a lovely day today.
sentence B: It's a very lovely day today, isnt it?

Output: It's a [I:very] lovely day today [C:./,] [I:isnt it?]

where:
I = INSERTED,
C = CHANGED

PS: I havent started coding yet since I want to gather some of your ideas on how to best implement this case.

Israel Sato
  • 199
  • 1
  • 2
  • 12

1 Answers1

2

I have come up with below code and for this problem.

Conditions Not considered

  1. Removed items from any of the list
  2. First char difference
  3. duplication of diff item

Please check and let me know if you have doubts.

public static void main(String[] args) {

    String str1 = "It's a lovely day today.";
    String str2 = "It's a very lovely day today, isnt it?";
    StringBuilder builder = new StringBuilder();
    StringBuilder added = new StringBuilder();
    StringBuilder changed = new StringBuilder();

    for (int i = 0; i < str1.length(); i++)
        for (int j = 0; j < str2.length(); j++) {
            if (str1.charAt(i) == str2.charAt(j)) {
                if (added.length() > 0) {
                    builder.append("[I:" + added.toString() + "]");
                    added = new StringBuilder();
                }
                if (changed.length() > 0) {
                    changed.append("[C:" + changed.toString() + "]");
                    changed = new StringBuilder();
                }
                // skip as there is no difference.
                builder.append(str1.charAt(i));
                i++;
                // check if index -1 chars are equal then there is
                // difference start
            } else if (str1.charAt(i - 1) == str2.charAt(j - 1)) {

                // check if end of line
                if ((i + 1 == str1.length())
                        || (str1.charAt(i + 1) == str2.charAt(j + 1))) {

                    changed.append(str1.charAt(i));
                    changed.append("/");
                    changed.append(str2.charAt(j));
                    j++;
                    // everything is added
                    if (i + 1 == str1.length()) {
                        while (j < str2.length() - 1)
                            added.append(str2.charAt(j++));
                    }

                    continue;
                }

                // Go until next equal found
                while (!(str1.charAt(i) == str2.charAt(j))
                        && j < str2.length() - 1) {
                    added.append(str2.charAt(j++));
                }
                j--;

            }
        }
    if (changed.length() > 0) {
        builder.append("[C:" + changed.toString() + "]");
    }
    if (added.length() > 0) {
        builder.append("[I:" + added.toString() + "]");
    }

    System.out.println(builder.toString());

}

Output

It's a [I:very ]lovely day today[C:./,][I: isnt it]
Amit Deshpande
  • 19,001
  • 4
  • 46
  • 72
  • Hey, this is working great! Thanks! Will there be a huge modification if I include the deletion of words, ie the output will also notify us for those words that were deleted in sentence B? – Israel Sato Sep 30 '12 at 23:26
  • Finding character removal is easy finding word removal is tricky. You can find removal the same way I find Insertion but this time outer loop will be on Second string and inner loop will be on first string. – Amit Deshpande Oct 01 '12 at 00:55
  • So if I include the deletion of words, I would have to do another nested looping aside from the loop above? – Israel Sato Oct 01 '12 at 02:37