<s> """<STR_LIT>""" <EOL> from math import sqrt <EOL> def get_words_from_dictionary ( lemmas ) : <EOL> '''<STR_LIT>''' <EOL> words = set ( ) <EOL> for lemma in lemmas : <EOL> words . update ( set ( lemmas [ lemma ] ) ) <EOL> return words <EOL> def _truncate ( words , cutlength ) : <EOL> '''<STR_LIT>''' <EOL> stems = { } <EOL> for word in words : <EOL> stem = word [ : cutlength ] <EOL> try : <EOL> stems [ stem ] . update ( [ word ] ) <EOL> except KeyError : <EOL> stems [ stem ] = set ( [ word ] ) <EOL> return stems <EOL> def _count_intersection ( l1 , l2 ) : <EOL> '''<STR_LIT>''' <EOL> x1 , y1 = l1 [ <NUM_LIT:0> ] <EOL> x2 , y2 = l1 [ <NUM_LIT:1> ] <EOL> x3 , y3 = l2 [ <NUM_LIT:0> ] <EOL> x4 , y4 = l2 [ <NUM_LIT:1> ] <EOL> denominator = ( x1 - x2 ) * ( y3 - y4 ) - ( y1 - y2 ) * ( x3 - x4 ) <EOL> if denominator == <NUM_LIT:0.0> : <EOL> if x1 == x2 == x3 == x4 == <NUM_LIT:0.0> : <EOL> return ( <NUM_LIT:0.0> , y4 ) <EOL> x = ( ( x1 * y2 - y1 * x2 ) * ( x3 - x4 ) - ( x1 - x2 ) * ( x3 * y4 - y3 * x4 ) ) / denominator <EOL> y = ( ( x1 * y2 - y1 * x2 ) * ( y3 - y4 ) - ( y1 - y2 ) * ( x3 * y4 - y3 * x4 ) ) / denominator <EOL> return ( x , y ) <EOL> def _get_derivative ( coordinates ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> return coordinates [ <NUM_LIT:1> ] / coordinates [ <NUM_LIT:0> ] <EOL> except ZeroDivisionError : <EOL> return float ( '<STR_LIT>' ) <EOL> def _calculate_cut ( lemmawords , stems ) : <EOL> '''<STR_LIT>''' <EOL> umt , wmt = <NUM_LIT:0.0> , <NUM_LIT:0.0> <EOL> for stem in stems : <EOL> cut = set ( lemmawords ) & set ( stems [ stem ] ) <EOL> if cut : <EOL> cutcount = len ( cut ) <EOL> stemcount = len ( stems [ stem ] ) <EOL> umt += cutcount * ( len ( lemmawords ) - cutcount ) <EOL> wmt += cutcount * ( stemcount - cutcount ) <EOL> return ( umt , wmt ) <EOL> def _calculate ( lemmas , stems ) : <EOL> '''<STR_LIT>''' <EOL> n = sum ( len ( lemmas [ word ] ) for word in lemmas ) <EOL> gdmt , gdnt , gumt , gwmt = ( <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ) <EOL> for lemma in lemmas : <EOL> lemmacount = len ( lemmas [ lemma ] ) <EOL> gdmt += lemmacount * ( lemmacount - <NUM_LIT:1> ) <EOL> gdnt += lemmacount * ( n - lemmacount ) <EOL> umt , wmt = _calculate_cut ( lemmas [ lemma ] , stems ) <EOL> gumt += umt <EOL> gwmt += wmt <EOL> return ( gumt / <NUM_LIT:2> , gdmt / <NUM_LIT:2> , gwmt / <NUM_LIT:2> , gdnt / <NUM_LIT:2> ) <EOL> def _indexes ( gumt , gdmt , gwmt , gdnt ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> ui = gumt / gdmt <EOL> except ZeroDivisionError : <EOL> ui = <NUM_LIT:0.0> <EOL> try : <EOL> oi = gwmt / gdnt <EOL> except ZeroDivisionError : <EOL> oi = <NUM_LIT:0.0> <EOL> try : <EOL> sw = oi / ui <EOL> except ZeroDivisionError : <EOL> if oi == <NUM_LIT:0.0> : <EOL> sw = float ( '<STR_LIT>' ) <EOL> else : <EOL> sw = float ( '<STR_LIT>' ) <EOL> return ( ui , oi , sw ) <EOL> class Paice ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , lemmas , stems ) : <EOL> '''<STR_LIT>''' <EOL> self . lemmas = lemmas <EOL> self . stems = stems <EOL> self . coords = [ ] <EOL> self . gumt , self . gdmt , self . gwmt , self . gdnt = ( None , None , None , None ) <EOL> self . ui , self . oi , self . sw = ( None , None , None ) <EOL> self . errt = None <EOL> self . update ( ) <EOL> def __str__ ( self ) : <EOL> text = [ '<STR_LIT>' % self . gumt ] <EOL> text . append ( '<STR_LIT>' % self . gdmt ) <EOL> text . append ( '<STR_LIT>' % self . gwmt ) <EOL> text . append ( '<STR_LIT>' % self . gdnt ) <EOL> text . append ( '<STR_LIT>' % self . ui ) <EOL> text . append ( '<STR_LIT>' % self . oi ) <EOL> text . append ( '<STR_LIT>' % self . sw ) <EOL> text . append ( '<STR_LIT>' % self . errt ) <EOL> coordinates = '<STR_LIT:U+0020>' . join ( [ '<STR_LIT>' % item for item in self . coords ] ) <EOL> text . append ( '<STR_LIT>' % coordinates ) <EOL> return '<STR_LIT>' . join ( text ) <EOL> def _get_truncation_indexes ( self , words , cutlength ) : <EOL> '''<STR_LIT>''' <EOL> truncated = _truncate ( words , cutlength ) <EOL> gumt , gdmt , gwmt , gdnt = _calculate ( self . lemmas , truncated ) <EOL> ui , oi = _indexes ( gumt , gdmt , gwmt , gdnt ) [ : <NUM_LIT:2> ] <EOL> return ( ui , oi ) <EOL> def _get_truncation_coordinates ( self , cutlength = <NUM_LIT:0> ) : <EOL> '''<STR_LIT>''' <EOL> words = get_words_from_dictionary ( self . lemmas ) <EOL> maxlength = max ( len ( word ) for word in words ) <EOL> coords = [ ] <EOL> while cutlength <= maxlength : <EOL> pair = self . _get_truncation_indexes ( words , cutlength ) <EOL> if pair not in coords : <EOL> coords . append ( pair ) <EOL> if pair == ( <NUM_LIT:0.0> , <NUM_LIT:0.0> ) : <EOL> return coords <EOL> if len ( coords ) >= <NUM_LIT:2> and pair [ <NUM_LIT:0> ] > <NUM_LIT:0.0> : <EOL> derivative1 = _get_derivative ( coords [ - <NUM_LIT:2> ] ) <EOL> derivative2 = _get_derivative ( coords [ - <NUM_LIT:1> ] ) <EOL> if derivative1 >= self . sw >= derivative2 : <EOL> return coords <EOL> cutlength += <NUM_LIT:1> <EOL> return coords <EOL> def _errt ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . coords = self . _get_truncation_coordinates ( ) <EOL> if ( <NUM_LIT:0.0> , <NUM_LIT:0.0> ) in self . coords : <EOL> if ( self . ui , self . oi ) != ( <NUM_LIT:0.0> , <NUM_LIT:0.0> ) : <EOL> return float ( '<STR_LIT>' ) <EOL> else : <EOL> return float ( '<STR_LIT>' ) <EOL> if ( self . ui , self . oi ) == ( <NUM_LIT:0.0> , <NUM_LIT:0.0> ) : <EOL> return <NUM_LIT:0.0> <EOL> intersection = _count_intersection ( ( ( <NUM_LIT:0> , <NUM_LIT:0> ) , ( self . ui , self . oi ) ) , <EOL> self . coords [ - <NUM_LIT:2> : ] <EOL> ) <EOL> op = sqrt ( self . ui ** <NUM_LIT:2> + self . oi ** <NUM_LIT:2> ) <EOL> ot = sqrt ( intersection [ <NUM_LIT:0> ] ** <NUM_LIT:2> + intersection [ <NUM_LIT:1> ] ** <NUM_LIT:2> ) <EOL> return op / ot <EOL> def update ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . gumt , self . gdmt , self . gwmt , self . gdnt = _calculate ( self . lemmas , self . stems ) <EOL> self . ui , self . oi , self . sw = _indexes ( self . gumt , self . gdmt , self . gwmt , self . gdnt ) <EOL> self . errt = self . _errt ( ) <EOL> def demo ( ) : <EOL> '''<STR_LIT>''' <EOL> lemmas = { '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> } <EOL> stems = { '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] <EOL> } <EOL> print ( '<STR_LIT>' ) <EOL> for lemma in sorted ( lemmas ) : <EOL> print ( '<STR_LIT>' % ( lemma , '<STR_LIT:U+0020>' . join ( lemmas [ lemma ] ) ) ) <EOL> print ( ) <EOL> print ( '<STR_LIT>' ) <EOL> for stem in sorted ( stems ) : <EOL> print ( '<STR_LIT>' % ( stem , '<STR_LIT:U+0020>' . join ( stems [ stem ] ) ) ) <EOL> print ( ) <EOL> p = Paice ( lemmas , stems ) <EOL> print ( p ) <EOL> print ( ) <EOL> stems = { '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] <EOL> } <EOL> print ( '<STR_LIT>' ) <EOL> for stem in sorted ( stems ) : <EOL> print ( '<STR_LIT>' % ( stem , '<STR_LIT:U+0020>' . join ( stems [ stem ] ) ) ) <EOL> print ( ) <EOL> p . stems = stems <EOL> p . update ( ) <EOL> print ( p ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> demo ( ) </s>
