def _fancy_replace(self, a, alo, ahi, b, blo, bhi):
<EOL>best_ratio, cutoff = <NUM_LIT>, <NUM_LIT><EOL>cruncher = SequenceMatcher(self.charjunk)<EOL>eqi, eqj = None, None   <EOL>for j in range(blo, bhi):<EOL><INDENT>bj = b[j]<EOL>cruncher.set_seq2(bj)<EOL>for i in range(alo, ahi):<EOL><INDENT>ai = a[i]<EOL>if ai == bj:<EOL><INDENT>if eqi is None:<EOL><INDENT>eqi, eqj = i, j<EOL><DEDENT>continue<EOL><DEDENT>cruncher.set_seq1(ai)<EOL>if cruncher.real_quick_ratio() > best_ratio andcruncher.quick_ratio() > best_ratio andcruncher.ratio() > best_ratio:<EOL><INDENT>best_ratio, best_i, best_j = cruncher.ratio(), i, j<EOL><DEDENT><DEDENT><DEDENT>if best_ratio < cutoff:<EOL><INDENT>if eqi is None:<EOL><INDENT>for line in self._plain_replace(a, alo, ahi, b, blo, bhi):<EOL><INDENT>yield line<EOL><DEDENT>return<EOL><DEDENT>best_i, best_j, best_ratio = eqi, eqj, <NUM_LIT:1.0><EOL><DEDENT>else:<EOL><INDENT>eqi = None<EOL><DEDENT>for line in self._fancy_helper(a, alo, best_i, b, blo, best_j):<EOL><INDENT>yield line<EOL><DEDENT>aelt, belt = a[best_i], b[best_j]<EOL>if eqi is None:<EOL><INDENT>atags = btags = "<STR_LIT>"<EOL>cruncher.set_seqs(aelt, belt)<EOL>for tag, ai1, ai2, bj1, bj2 in cruncher.get_opcodes():<EOL><INDENT>la, lb = ai2 - ai1, bj2 - bj1<EOL>if tag == '<STR_LIT:replace>':<EOL><INDENT>atags += '<STR_LIT>' * la<EOL>btags += '<STR_LIT>' * lb<EOL><DEDENT>elif tag == '<STR_LIT>':<EOL><INDENT>atags += '<STR_LIT:->' * la<EOL><DEDENT>elif tag == '<STR_LIT>':<EOL><INDENT>btags += '<STR_LIT:+>' * lb<EOL><DEDENT>elif tag == '<STR_LIT>':<EOL><INDENT>atags += '<STR_LIT:U+0020>' * la<EOL>btags += '<STR_LIT:U+0020>' * lb<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>' % (tag,))<EOL><DEDENT><DEDENT>for line in self._qformat(aelt, belt, atags, btags):<EOL><INDENT>yield line<EOL><DEDENT><DEDENT>else:<EOL><INDENT>yield '<STR_LIT:U+0020>' + aelt<EOL><DEDENT>for line in self._fancy_helper(a, best_i+<NUM_LIT:1>, ahi, b, best_j+<NUM_LIT:1>, bhi):<EOL><INDENT>yield line<EOL><DEDENT>
r"""
        When replacing one block of lines with another, search the blocks
        for *similar* lines; the best-matching pair (if any) is used as a
        synch point, and intraline difference marking is done on the
        similar pair. Lots of work, but often worth it.

        Example:

        >>> d = Differ()
        >>> results = d._fancy_replace(['abcDefghiJkl\n'], 0, 1,
        ...                            ['abcdefGhijkl\n'], 0, 1)
        >>> print ''.join(results),
        - abcDefghiJkl
        ?    ^  ^  ^
        + abcdefGhijkl
        ?    ^  ^  ^