<s> import sys , re , operator , string <EOL> class TFQuarantine : <EOL> def __init__ ( self , func ) : <EOL> self . _funcs = [ func ] <EOL> def bind ( self , func ) : <EOL> self . _funcs . append ( func ) <EOL> return self <EOL> def execute ( self ) : <EOL> def guard_callable ( v ) : <EOL> return v ( ) if hasattr ( v , '<STR_LIT>' ) else v <EOL> value = lambda : None <EOL> for func in self . _funcs : <EOL> value = func ( guard_callable ( value ) ) <EOL> print guard_callable ( value ) <EOL> def get_input ( arg ) : <EOL> def _f ( ) : <EOL> return sys . argv [ <NUM_LIT:1> ] <EOL> return _f <EOL> def extract_words ( path_to_file ) : <EOL> def _f ( ) : <EOL> with open ( path_to_file ) as f : <EOL> data = f . read ( ) <EOL> pattern = re . compile ( '<STR_LIT>' ) <EOL> word_list = pattern . sub ( '<STR_LIT:U+0020>' , data ) . lower ( ) . split ( ) <EOL> return word_list <EOL> return _f <EOL> def remove_stop_words ( word_list ) : <EOL> def _f ( ) : <EOL> with open ( '<STR_LIT>' ) as f : <EOL> stop_words = f . read ( ) . split ( '<STR_LIT:U+002C>' ) <EOL> stop_words . extend ( list ( string . ascii_lowercase ) ) <EOL> return [ w for w in word_list if not w in stop_words ] <EOL> return _f <EOL> def frequencies ( word_list ) : <EOL> word_freqs = { } <EOL> for w in word_list : <EOL> if w in word_freqs : <EOL> word_freqs [ w ] += <NUM_LIT:1> <EOL> else : <EOL> word_freqs [ w ] = <NUM_LIT:1> <EOL> return word_freqs <EOL> def sort ( word_freq ) : <EOL> return sorted ( word_freq . iteritems ( ) , key = operator . itemgetter ( <NUM_LIT:1> ) , reverse = True ) <EOL> def top25_freqs ( word_freqs ) : <EOL> top25 = "<STR_LIT>" <EOL> for tf in word_freqs [ <NUM_LIT:0> : <NUM_LIT> ] : <EOL> top25 += str ( tf [ <NUM_LIT:0> ] ) + '<STR_LIT>' + str ( tf [ <NUM_LIT:1> ] ) + '<STR_LIT:\n>' <EOL> return top25 <EOL> TFQuarantine ( get_input ) . bind ( extract_words ) . bind ( remove_stop_words ) . bind ( frequencies ) . bind ( sort ) . bind ( top25_freqs ) . execute ( ) </s>
