<s> import sys , re , operator , string , traceback <EOL> def extract_words ( path_to_file ) : <EOL> assert ( type ( path_to_file ) is str ) , "<STR_LIT>" <EOL> assert ( path_to_file ) , "<STR_LIT>" <EOL> try : <EOL> with open ( path_to_file ) as f : <EOL> str_data = f . read ( ) <EOL> except IOError as e : <EOL> print "<STR_LIT>" . format ( e . errno , path_to_file , e . strerror ) <EOL> raise e <EOL> pattern = re . compile ( '<STR_LIT>' ) <EOL> word_list = pattern . sub ( '<STR_LIT:U+0020>' , str_data ) . lower ( ) . split ( ) <EOL> return word_list <EOL> def remove_stop_words ( word_list ) : <EOL> assert ( type ( word_list ) is list ) , "<STR_LIT>" <EOL> try : <EOL> with open ( '<STR_LIT>' ) as f : <EOL> stop_words = f . read ( ) . split ( '<STR_LIT:U+002C>' ) <EOL> except IOError as e : <EOL> print "<STR_LIT>" . format ( e . errno , e . strerror ) <EOL> raise e <EOL> stop_words . extend ( list ( string . ascii_lowercase ) ) <EOL> return [ w for w in word_list if not w in stop_words ] <EOL> def frequencies ( word_list ) : <EOL> assert ( type ( word_list ) is list ) , "<STR_LIT>" <EOL> assert ( word_list < > [ ] ) , "<STR_LIT>" <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> assert ( type ( word_freq ) is dict ) , "<STR_LIT>" <EOL> assert ( word_freq < > { } ) , "<STR_LIT>" <EOL> try : <EOL> return sorted ( word_freq . iteritems ( ) , key = operator . itemgetter ( <NUM_LIT:1> ) , reverse = True ) <EOL> except Exception as e : <EOL> print "<STR_LIT>" . format ( e ) <EOL> raise e <EOL> try : <EOL> assert ( len ( sys . argv ) > <NUM_LIT:1> ) , "<STR_LIT>" <EOL> word_freqs = sort ( frequencies ( remove_stop_words ( extract_words ( sys . argv [ <NUM_LIT:1> ] ) ) ) ) <EOL> assert ( type ( word_freqs ) is list ) , "<STR_LIT>" <EOL> assert ( len ( word_freqs ) > <NUM_LIT> ) , "<STR_LIT>" <EOL> for ( w , c ) in word_freqs [ <NUM_LIT:0> : <NUM_LIT> ] : <EOL> print w , '<STR_LIT>' , c <EOL> except Exception as e : <EOL> print "<STR_LIT>" . format ( e ) <EOL> traceback . print_exc ( ) </s>
