2929 'Differ' ,'IS_CHARACTER_JUNK' , 'IS_LINE_JUNK' , 'context_diff' ,
3030 'unified_diff' ]
3131
32+ def _calculate_ratio (matches , length ):
33+ if length :
34+ return 2.0 * matches / length
35+ return 1.0
36+
3237class SequenceMatcher :
3338
3439 """
@@ -611,7 +616,7 @@ def ratio(self):
611616
612617 matches = reduce (lambda sum , triple : sum + triple [- 1 ],
613618 self .get_matching_blocks (), 0 )
614- return 2.0 * matches / ( len (self .a ) + len (self .b ))
619+ return _calculate_ratio ( matches , len (self .a ) + len (self .b ))
615620
616621 def quick_ratio (self ):
617622 """Return an upper bound on ratio() relatively quickly.
@@ -640,7 +645,7 @@ def quick_ratio(self):
640645 avail [elt ] = numb - 1
641646 if numb > 0 :
642647 matches = matches + 1
643- return 2.0 * matches / ( len (self .a ) + len (self .b ))
648+ return _calculate_ratio ( matches , len (self .a ) + len (self .b ))
644649
645650 def real_quick_ratio (self ):
646651 """Return an upper bound on ratio() very quickly.
@@ -652,7 +657,7 @@ def real_quick_ratio(self):
652657 la , lb = len (self .a ), len (self .b )
653658 # can't have more matches than the number of elements in the
654659 # shorter sequence
655- return 2.0 * min (la , lb ) / ( la + lb )
660+ return _calculate_ratio ( min (la , lb ), la + lb )
656661
657662def get_close_matches (word , possibilities , n = 3 , cutoff = 0.6 ):
658663 """Use SequenceMatcher to return list of the best "good enough" matches.
0 commit comments