def writers_words(list_of_files, outputfile): counter = WordCount() for file in list_of_files: counter.count_file(file) output = open(outputfile, 'w') stats = counter.word_stats() output.write("Total words counted: " + repr(counter.word_count_total()) \ + '\n') output.write('Rank'.rjust(RANK_IND) \ + 'Word'.rjust(WORD_IND) \ + 'Count'.rjust(COUNT_IND) \ + ' '*5 \ + 'Percentage'.ljust(PERC_IND) \ + '\n') rank = 1 for (count, perc, list_of_words) in stats: for word in list_of_words: newline = repr(rank).rjust(RANK_IND) \ + word.rjust(WORD_IND) \ + repr(count).rjust(COUNT_IND) \ + ' '*5 \ + repr(perc).ljust(PERC_IND) \ + '\n' output.write(newline) rank += 1 output.close() return True
def total_word_count(list_of_words): wc = WordCount() manual_count = 0 for word in list_of_words: wc._add_to_count(word) manual_count += 1 assert wc.word_count_total() == manual_count assert wc.word_count_total() == wc._total_words