예제 #1
0
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
예제 #2
0
def count_file(filename):
    """ This test only works for my numeric-based test files. """
    wc = WordCount()
    wc.count_file(filename)
    assert wc._total_words == 10
    assert wc._word_counts['zero'] == 0
    assert wc._word_counts['one'] == 1
    assert wc._word_counts['two'] == 2
    assert wc._word_counts['three'] == 3
    assert wc._word_counts['four'] == 4
예제 #3
0
def count_mult_files(list_of_filenames):
    wc = WordCount()
    mult = 1
    for file in list_of_filenames:
        wc.count_file(file)
        assert wc._total_words == 10*mult
        assert wc._word_counts['zero'] == 0
        assert wc._word_counts['one'] == 1*mult
        assert wc._word_counts['two'] == 2*mult
        assert wc._word_counts['three'] == 3*mult
        assert wc._word_counts['four'] == 4*mult
        mult += 1
예제 #4
0
def words_stats(filename):
    wc = WordCount()
    wc.count_file(filename)
    return wc.word_stats()
예제 #5
0
def perc_words(filename):
    wc = WordCount()
    wc.count_file(filename)
    return wc.words_percent()
예제 #6
0
def ranked_words(filename):
    wc = WordCount()
    wc.count_file(filename)
    return wc.words_ranked()
예제 #7
0
def alpha_words(filename):
    wc = WordCount()
    wc.count_file(filename)
    return wc.words_alphabetical()