GNU Info

Info Node: (python2.1-lib.info)SequenceMatcher Objects

(python2.1-lib.info)SequenceMatcher Objects


Next: Examples 2 Prev: difflib Up: difflib
Enter node , (file) or (file)node

SequenceMatcher Objects
-----------------------

`SequenceMatcher([isjunk[, a[, b]]])'
     Optional argument ISJUNK must be `None' (the default) or a
     one-argument function that takes a sequence element and returns
     true if and only if the element is "junk" and should be ignored.
     `None' is equivalent to passing `lambda x: 0', i.e. no elements
     are ignored.  For example, pass

          lambda x: x in " \t"

     if you're comparing lines as sequences of characters, and don't
     want to synch up on blanks or hard tabs.

     The optional arguments A and B are sequences to be compared; both
     default to empty strings.  The elements of both sequences must be
     hashable.

`SequenceMatcher' objects have the following methods:

`set_seqs(a, b)'
     Set the two sequences to be compared.

`SequenceMatcher' computes and caches detailed information about the
second sequence, so if you want to compare one sequence against many
sequences, use `set_seq2()' to set the commonly used sequence once and
call `set_seq1()' repeatedly, once for each of the other sequences.

`set_seq1(a)'
     Set the first sequence to be compared.  The second sequence to be
     compared is not changed.

`set_seq2(b)'
     Set the second sequence to be compared.  The first sequence to be
     compared is not changed.

`find_longest_match(alo, ahi, blo, bhi)'
     Find longest matching block in `A[ALO:AHI]' and `B[BLO:BHI]'.

     If ISJUNK was omitted or `None', `get_longest_match()' returns
     `(I, J, K)' such that `A[I:I+K]' is equal to `B[J:J+K]', where
     `ALO <= I <= I+K <= AHI' and `BLO <= J <= J+K <= BHI'.  For all
     `(I', J', K')' meeting those conditions, the additional conditions
     `K >= K'', `I <= I'', and if `I == I'', `J <= J'' are also met.
     In other words, of all maximal matching blocks, return one that
     starts earliest in A, and of all those maximal matching blocks
     that start earliest in A, return the one that starts earliest in B.

          >>> s = SequenceMatcher(None, " abcd", "abcd abcd")
          >>> s.find_longest_match(0, 5, 0, 9)
          (0, 4, 5)

     If ISJUNK was provided, first the longest matching block is
     determined as above, but with the additional restriction that no
     junk element appears in the block.  Then that block is extended as
     far as possible by matching (only) junk elements on both sides.
     So the resulting block never matches on junk except as identical
     junk happens to be adjacent to an interesting match.

     Here's the same example as before, but considering blanks to be
     junk.  That prevents `' abcd'' from matching the `' abcd'' at the
     tail end of the second sequence directly.  Instead only the
     `'abcd'' can match, and matches the leftmost `'abcd'' in the
     second sequence:

          >>> s = SequenceMatcher(lambda x: x==" ", " abcd", "abcd abcd")
          >>> s.find_longest_match(0, 5, 0, 9)
          (1, 0, 4)

     If no blocks match, this returns `(ALO, BLO, 0)'.

`get_matching_blocks()'
     Return list of triples describing matching subsequences.  Each
     triple is of the form `(I, J, N)', and means that `A[I:I+N] ==
     B[J:J+N]'.  The triples are monotonically increasing in I and J.

     The last triple is a dummy, and has the value `(len(A), len(B),
     0)'.  It is the only triple with `N == 0'.

          >>> s = SequenceMatcher(None, "abxcd", "abcd")
          >>> s.get_matching_blocks()
          [(0, 0, 2), (3, 2, 2), (5, 4, 0)]

`get_opcodes()'
     Return list of 5-tuples describing how to turn A into B.  Each
     tuple is of the form `(TAG, I1, I2, J1, J2)'.  The first tuple has
     `I1 == J1 == 0', and remaining tuples have I1 equal to the I2 from
     the preceeding tuple, and, likewise, J1 equal to the previous J2.

     The TAG values are strings, with these meanings:

     Value                              Meaning
     ------                             -----
     'replace'                          `A[I1:I2]' should be replaced by
                                        `B[J1:J2]'.
     'delete'                           `A[I1:I2]' should be deleted.
                                        Note that `J1 == J2' in this case.
     'insert'                           `B[J1:J2]' should be inserted at
                                        `A[I1:I1]'.  Note that `I1 == I2'
                                        in this case.
     'equal'                            `A[I1:I2] == B[J1:J2]' (the
                                        sub-sequences are equal).

     For example:

          >>> a = "qabxcd"
          >>> b = "abycdf"
          >>> s = SequenceMatcher(None, a, b)
          >>> for tag, i1, i2, j1, j2 in s.get_opcodes():
          ...    print ("%7s a[%d:%d] (%s) b[%d:%d] (%s)" %
          ...           (tag, i1, i2, a[i1:i2], j1, j2, b[j1:j2]))
           delete a[0:1] (q) b[0:0] ()
            equal a[1:3] (ab) b[0:2] (ab)
          replace a[3:4] (x) b[2:3] (y)
            equal a[4:6] (cd) b[3:5] (cd)
           insert a[6:6] () b[5:6] (f)

`ratio()'
     Return a measure of the sequences' similarity as a float in the
     range [0, 1].

     Where T is the total number of elements in both sequences, and M is
     the number of matches, this is 2.0*M / T. Note that this is `1.'
     if the sequences are identical, and `0.' if they have nothing in
     common.

     This is expensive to compute if `get_matching_blocks()' or
     `get_opcodes()' hasn't already been called, in which case you may
     want to try `quick_ratio()' or `real_quick_ratio()' first to get
     an upper bound.

`quick_ratio()'
     Return an upper bound on `ratio()' relatively quickly.

     This isn't defined beyond that it is an upper bound on `ratio()',
     and is faster to compute.

`real_quick_ratio()'
     Return an upper bound on `ratio()' very quickly.

     This isn't defined beyond that it is an upper bound on `ratio()',
     and is faster to compute than either `ratio()' or `quick_ratio()'.

The three methods that return the ratio of matching to total characters
can give different results due to differing levels of approximation,
although `quick_ratio()' and `real_quick_ratio()' are always at least
as large as `ratio()':

     >>> s = SequenceMatcher(None, "abcd", "bcde")
     >>> s.ratio()
     0.75
     >>> s.quick_ratio()
     0.75
     >>> s.real_quick_ratio()
     1.0


automatically generated by info2www version 1.2.2.9