Highlights the difference between two texts using color. Has to account for deletions and insertions from original text to perturbed. Relies on the index map stored in ``self.original_result.attacked_text.attack_attrs["original_index_map"]``.
(self, color_method=None)
| 79 | return orig_colored + " --> " + pert_colored |
| 80 | |
| 81 | def diff_color(self, color_method=None): |
| 82 | """Highlights the difference between two texts using color. |
| 83 | |
| 84 | Has to account for deletions and insertions from original text to |
| 85 | perturbed. Relies on the index map stored in |
| 86 | ``self.original_result.attacked_text.attack_attrs["original_index_map"]``. |
| 87 | """ |
| 88 | t1 = self.original_result.attacked_text |
| 89 | t2 = self.perturbed_result.attacked_text |
| 90 | |
| 91 | if detect(t1.text) == "zh-cn" or detect(t1.text) == "ko": |
| 92 | return t1.printable_text(), t2.printable_text() |
| 93 | |
| 94 | if color_method is None: |
| 95 | return t1.printable_text(), t2.printable_text() |
| 96 | |
| 97 | color_1 = self.original_result.get_text_color_input() |
| 98 | color_2 = self.perturbed_result.get_text_color_perturbed() |
| 99 | |
| 100 | # iterate through and count equal/unequal words |
| 101 | words_1_idxs = [] |
| 102 | t2_equal_idxs = set() |
| 103 | original_index_map = t2.attack_attrs["original_index_map"] |
| 104 | for t1_idx, t2_idx in enumerate(original_index_map): |
| 105 | if t2_idx == -1: |
| 106 | # add words in t1 that are not in t2 |
| 107 | words_1_idxs.append(t1_idx) |
| 108 | else: |
| 109 | w1 = t1.words[t1_idx] |
| 110 | w2 = t2.words[t2_idx] |
| 111 | if w1 == w2: |
| 112 | t2_equal_idxs.add(t2_idx) |
| 113 | else: |
| 114 | words_1_idxs.append(t1_idx) |
| 115 | |
| 116 | # words to color in t2 are all the words that didn't have an equal, |
| 117 | # mapped word in t1 |
| 118 | words_2_idxs = list(sorted(set(range(t2.num_words)) - t2_equal_idxs)) |
| 119 | |
| 120 | # make lists of colored words |
| 121 | words_1 = [t1.words[i] for i in words_1_idxs] |
| 122 | words_1 = [utils.color_text(w, color_1, color_method) for w in words_1] |
| 123 | words_2 = [t2.words[i] for i in words_2_idxs] |
| 124 | words_2 = [utils.color_text(w, color_2, color_method) for w in words_2] |
| 125 | |
| 126 | t1 = self.original_result.attacked_text.replace_words_at_indices( |
| 127 | words_1_idxs, words_1 |
| 128 | ) |
| 129 | t2 = self.perturbed_result.attacked_text.replace_words_at_indices( |
| 130 | words_2_idxs, words_2 |
| 131 | ) |
| 132 | |
| 133 | key_color = ("bold", "underline") |
| 134 | return ( |
| 135 | t1.printable_text(key_color=key_color, key_color_method=color_method), |
| 136 | t2.printable_text(key_color=key_color, key_color_method=color_method), |
| 137 | ) |
no test coverage detected