MCPcopy Index your code
hub / github.com/python/cpython / quick_ratio

Method quick_ratio

Lib/difflib.py:623–650  ·  view source on GitHub ↗

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.

(self)

Source from the content-addressed store, hash-verified

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.
625
626 This isn't defined beyond that it is an upper bound on .ratio(), and
627 is faster to compute.
628 """
629
630 # viewing a and b as multisets, set matches to the cardinality
631 # of their intersection; this counts the number of matches
632 # without regard to order, so is clearly an upper bound
633 if self.fullbcount is None:
634 self.fullbcount = fullbcount = {}
635 for elt in self.b:
636 fullbcount[elt] = fullbcount.get(elt, 0) + 1
637 fullbcount = self.fullbcount
638 # avail[x] is the number of times x appears in 'b' less the
639 # number of times we've seen it in 'a' so far ... kinda
640 avail = {}
641 matches = 0
642 for elt in self.a:
643 if elt in avail:
644 numb = avail[elt]
645 else:
646 numb = fullbcount.get(elt, 0)
647 avail[elt] = numb - 1
648 if numb > 0:
649 matches += 1
650 return _calculate_ratio(matches, len(self.a) + len(self.b))
651
652 def real_quick_ratio(self):
653 """Return an upper bound on ratio() very quickly.

Callers 2

get_close_matchesFunction · 0.95

Calls 2

_calculate_ratioFunction · 0.85
getMethod · 0.45

Tested by 1