示例#1
0
def test_make_ngram():
    '''Test the make ngram method'''
    ngram_freq = NgramFrequencies(1, 'test_file.txt')
    ngram_freq_2 = NgramFrequencies(2, 'test_file.txt')
    ngram_freq.make_ngram()
    ngram_freq_2.make_ngram()
    assert list(ngram_freq.dic_ngram.keys()) == [
        'ab', "dr", 'd', 'gh', 'COMMA', "ab's"
    ]
    assert list(ngram_freq_2.dic_ngram.keys()) == [
        'ab_dr', "dr_d", 'gh_COMMA', "COMMA_ab's"
    ]
def main():

    file_name = input('enter file name: ')

    unigrams = NgramFrequencies(1, file_name)
    bigrams = NgramFrequencies(2, file_name)
    trigrams = NgramFrequencies(3, file_name)

    print("\n Top 10 unigrams:")
    unigrams.make_ngram()
    print("", *unigrams.top_n_freqs(10),
          sep="\n    ")

    print("\n Top 10 bigrams:")
    bigrams.make_ngram()
    print("", *bigrams.top_n_freqs(10),
          sep="\n    ")

    print("\n Top 10 trigrams:")
    trigrams.make_ngram()
    print("", *trigrams.top_n_freqs(10), sep="\n    ")