Return a measure of the sequences' similarity (float in [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.
(self)
| 596 | yield group |
| 597 | |
| 598 | def ratio(self): |
| 599 | """Return a measure of the sequences' similarity (float in [0,1]). |
| 600 | |
| 601 | Where T is the total number of elements in both sequences, and |
| 602 | M is the number of matches, this is 2.0*M / T. |
| 603 | Note that this is 1 if the sequences are identical, and 0 if |
| 604 | they have nothing in common. |
| 605 | |
| 606 | .ratio() is expensive to compute if you haven't already computed |
| 607 | .get_matching_blocks() or .get_opcodes(), in which case you may |
| 608 | want to try .quick_ratio() or .real_quick_ratio() first to get an |
| 609 | upper bound. |
| 610 | |
| 611 | >>> s = SequenceMatcher(None, "abcd", "bcde") |
| 612 | >>> s.ratio() |
| 613 | 0.75 |
| 614 | >>> s.quick_ratio() |
| 615 | 0.75 |
| 616 | >>> s.real_quick_ratio() |
| 617 | 1.0 |
| 618 | """ |
| 619 | |
| 620 | matches = sum(triple[-1] for triple in self.get_matching_blocks()) |
| 621 | return _calculate_ratio(matches, len(self.a) + len(self.b)) |
| 622 | |
| 623 | def quick_ratio(self): |
| 624 | """Return an upper bound on ratio() relatively quickly. |