<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals <EOL> import platform <EOL> import warnings <EOL> try : <EOL> from . StringMatcher import StringMatcher as SequenceMatcher <EOL> except ImportError : <EOL> if platform . python_implementation ( ) != "<STR_LIT>" : <EOL> warnings . warn ( '<STR_LIT>' ) <EOL> from difflib import SequenceMatcher <EOL> from . import utils <EOL> @ utils . check_for_none <EOL> @ utils . check_empty_string <EOL> def ratio ( s1 , s2 ) : <EOL> s1 , s2 = utils . make_type_consistent ( s1 , s2 ) <EOL> m = SequenceMatcher ( None , s1 , s2 ) <EOL> return utils . intr ( <NUM_LIT:100> * m . ratio ( ) ) <EOL> @ utils . check_for_none <EOL> @ utils . check_empty_string <EOL> def partial_ratio ( s1 , s2 ) : <EOL> """<STR_LIT>""" <EOL> s1 , s2 = utils . make_type_consistent ( s1 , s2 ) <EOL> if len ( s1 ) <= len ( s2 ) : <EOL> shorter = s1 <EOL> longer = s2 <EOL> else : <EOL> shorter = s2 <EOL> longer = s1 <EOL> m = SequenceMatcher ( None , shorter , longer ) <EOL> blocks = m . get_matching_blocks ( ) <EOL> scores = [ ] <EOL> for block in blocks : <EOL> long_start = block [ <NUM_LIT:1> ] - block [ <NUM_LIT:0> ] if ( block [ <NUM_LIT:1> ] - block [ <NUM_LIT:0> ] ) > <NUM_LIT:0> else <NUM_LIT:0> <EOL> long_end = long_start + len ( shorter ) <EOL> long_substr = longer [ long_start : long_end ] <EOL> m2 = SequenceMatcher ( None , shorter , long_substr ) <EOL> r = m2 . ratio ( ) <EOL> if r > <NUM_LIT> : <EOL> return <NUM_LIT:100> <EOL> else : <EOL> scores . append ( r ) <EOL> return utils . intr ( <NUM_LIT:100> * max ( scores ) ) <EOL> def _process_and_sort ( s , force_ascii ) : <EOL> """<STR_LIT>""" <EOL> tokens = utils . full_process ( s , force_ascii = force_ascii ) . split ( ) <EOL> sorted_string = u"<STR_LIT:U+0020>" . join ( sorted ( tokens ) ) <EOL> return sorted_string . strip ( ) <EOL> @ utils . check_for_none <EOL> def _token_sort ( s1 , s2 , partial = True , force_ascii = True ) : <EOL> sorted1 = _process_and_sort ( s1 , force_ascii ) <EOL> sorted2 = _process_and_sort ( s2 , force_ascii ) <EOL> if partial : <EOL> return partial_ratio ( sorted1 , sorted2 ) <EOL> else : <EOL> return ratio ( sorted1 , sorted2 ) <EOL> def token_sort_ratio ( s1 , s2 , force_ascii = True ) : <EOL> """<STR_LIT>""" <EOL> return _token_sort ( s1 , s2 , partial = False , force_ascii = force_ascii ) <EOL> def partial_token_sort_ratio ( s1 , s2 , force_ascii = True ) : <EOL> """<STR_LIT>""" <EOL> return _token_sort ( s1 , s2 , partial = True , force_ascii = force_ascii ) <EOL> @ utils . check_for_none <EOL> def _token_set ( s1 , s2 , partial = True , force_ascii = True ) : <EOL> """<STR_LIT>""" <EOL> p1 = utils . full_process ( s1 , force_ascii = force_ascii ) <EOL> p2 = utils . full_process ( s2 , force_ascii = force_ascii ) <EOL> if not utils . validate_string ( p1 ) : <EOL> return <NUM_LIT:0> <EOL> if not utils . validate_string ( p2 ) : <EOL> return <NUM_LIT:0> <EOL> tokens1 = set ( utils . full_process ( p1 ) . split ( ) ) <EOL> tokens2 = set ( utils . full_process ( p2 ) . split ( ) ) <EOL> intersection = tokens1 . intersection ( tokens2 ) <EOL> diff1to2 = tokens1 . difference ( tokens2 ) <EOL> diff2to1 = tokens2 . difference ( tokens1 ) <EOL> sorted_sect = "<STR_LIT:U+0020>" . join ( sorted ( intersection ) ) <EOL> sorted_1to2 = "<STR_LIT:U+0020>" . join ( sorted ( diff1to2 ) ) <EOL> sorted_2to1 = "<STR_LIT:U+0020>" . join ( sorted ( diff2to1 ) ) <EOL> combined_1to2 = sorted_sect + "<STR_LIT:U+0020>" + sorted_1to2 <EOL> combined_2to1 = sorted_sect + "<STR_LIT:U+0020>" + sorted_2to1 <EOL> sorted_sect = sorted_sect . strip ( ) <EOL> combined_1to2 = combined_1to2 . strip ( ) <EOL> combined_2to1 = combined_2to1 . strip ( ) <EOL> if partial : <EOL> ratio_func = partial_ratio <EOL> else : <EOL> ratio_func = ratio <EOL> pairwise = [ <EOL> ratio_func ( sorted_sect , combined_1to2 ) , <EOL> ratio_func ( sorted_sect , combined_2to1 ) , <EOL> ratio_func ( combined_1to2 , combined_2to1 ) <EOL> ] <EOL> return max ( pairwise ) <EOL> def token_set_ratio ( s1 , s2 , force_ascii = True ) : <EOL> return _token_set ( s1 , s2 , partial = False , force_ascii = force_ascii ) <EOL> def partial_token_set_ratio ( s1 , s2 , force_ascii = True ) : <EOL> return _token_set ( s1 , s2 , partial = True , force_ascii = force_ascii ) <EOL> def QRatio ( s1 , s2 , force_ascii = True ) : <EOL> p1 = utils . full_process ( s1 , force_ascii = force_ascii ) <EOL> p2 = utils . full_process ( s2 , force_ascii = force_ascii ) <EOL> if not utils . validate_string ( p1 ) : <EOL> return <NUM_LIT:0> <EOL> if not utils . validate_string ( p2 ) : <EOL> return <NUM_LIT:0> <EOL> return ratio ( p1 , p2 ) <EOL> def UQRatio ( s1 , s2 ) : <EOL> return QRatio ( s1 , s2 , force_ascii = False ) <EOL> def WRatio ( s1 , s2 , force_ascii = True ) : <EOL> """<STR_LIT>""" <EOL> p1 = utils . full_process ( s1 , force_ascii = force_ascii ) <EOL> p2 = utils . full_process ( s2 , force_ascii = force_ascii ) <EOL> if not utils . validate_string ( p1 ) : <EOL> return <NUM_LIT:0> <EOL> if not utils . validate_string ( p2 ) : <EOL> return <NUM_LIT:0> <EOL> try_partial = True <EOL> unbase_scale = <NUM_LIT> <EOL> partial_scale = <NUM_LIT> <EOL> base = ratio ( p1 , p2 ) <EOL> len_ratio = float ( max ( len ( p1 ) , len ( p2 ) ) ) / min ( len ( p1 ) , len ( p2 ) ) <EOL> if len_ratio < <NUM_LIT> : <EOL> try_partial = False <EOL> if len_ratio > <NUM_LIT:8> : <EOL> partial_scale = <NUM_LIT> <EOL> if try_partial : <EOL> partial = partial_ratio ( p1 , p2 ) * partial_scale <EOL> ptsor = partial_token_sort_ratio ( p1 , p2 , force_ascii = force_ascii ) * unbase_scale * partial_scale <EOL> ptser = partial_token_set_ratio ( p1 , p2 , force_ascii = force_ascii ) * unbase_scale * partial_scale <EOL> return utils . intr ( max ( base , partial , ptsor , ptser ) ) <EOL> else : <EOL> tsor = token_sort_ratio ( p1 , p2 , force_ascii = force_ascii ) * unbase_scale <EOL> tser = token_set_ratio ( p1 , p2 , force_ascii = force_ascii ) * unbase_scale <EOL> return utils . intr ( max ( base , tsor , tser ) ) <EOL> def UWRatio ( s1 , s2 ) : <EOL> """<STR_LIT>""" <EOL> return WRatio ( s1 , s2 , force_ascii = False ) </s>
