def test_make_sentence():
    """
    test making a trigrams sentence

    as it is supposed to be random, this tests for things other than
    the actual results.

    Which means that it does NOT test everything! This test could pass
    with a very broken make_sentence function. So you should probably
    add a few more things to this test.

    NOTE that this test relies on the build_trigram() function, so it
         will fail if that doesn't work.
    """

    # reset the seed, sop that we won't always get the same answer
    random.seed()

    # use the already tested build_trigram function to make the dict
    tri_dict = trigrams.build_trigram(LONGER_TEXT)

    # make a sentence of 6 words
    sentence = trigrams.make_sentence(tri_dict, 6)

    print(sentence)
    # check that it has 6 words
    assert len(sentence.split()) == 6
    # check that the first letter is a capital
    assert sentence[0] == sentence[0].upper()
    # check that it ends with a period
    assert sentence[-1] == "."
    # check that there is not a space between the period and the last word.
    assert not sentence[-2].isspace()
def test_trigrams_following_words():
    """
    test that the following words are correct
    """
    tris = trigrams.build_trigram(IWISH)

    # this will only print if the test fails
    # but if if does, you can see what's going on to try to fix it.
    print(tris)

    # a separate assert for each pair:
    assert tris[("I", "wish")] == ["I", "I"]
    assert tris[("wish", "I")] == ["may", "might"]
    assert tris[("may", "I")] == ["wish"]
    assert tris[("I", "may")] == ["I"]
예제 #3
0
def test_trigrams_pairs():
    """
    test that the build_trigram function creates the right pairs of words
    """
    tris = trigrams.build_trigram(IWISH)

    pairs = tris.keys()

    # using a set here, as the dict_keys object is a set as well
    # And keys are always unique and hashable
    # and the order does not matter, so perfect for a set
    assert pairs == {("I", "wish"),
                     ("wish", "I"),
                     ("may", "I"),
                     ("I", "may"),
                     }
예제 #4
0
def test_make_sentence():
    """
    test making a trigrams sentence

    as it is supposed to be random, this tests for things other than
    the actual results.

    NOTE that this test relies on the build_trigram() function, so it
         will fail if that doesn't work.
    """

    # reset the seed, so that we won't always get the same answer
    random.seed()

    # use the already tested build_trigram function to make the dict
    tri_dict = trigrams.build_trigram(LONGER_TEXT)

    # make a sentence of 6 words
    sentence = trigrams.make_sentence(tri_dict, 6)

    print(sentence)
    # check that it has 6 words
    assert len(sentence.split()) == 6
    # check that the first letter is a capital
    assert sentence[0] == sentence[0].upper()
    # check that it ends with a period
    assert sentence[-1] == "."
    # check that there is not a space between the period and the last word.
    assert not sentence[-2].isspace()

    # check that the second couple words are a pair
    # (the first has capitalization issues)
    pair = sentence.split()[1:3]
    # we've capitalized the sentence, so need to undo that
    # pair[0] = pair[0].lower() if pair[0] != "I" else pair[0]
    # print(sorted(tri_dict.keys()))
    assert tuple(pair) in tri_dict

    # just because it was a bug:
    words = sentence.split()
    matched = False
    for word in sentence.split():
        if word[0] == word:
            matched += 1
    print(matched)
    assert matched < len(words)
예제 #5
0
def test_build_trigram():
    local_result = trigrams.build_trigram(global_result)
    assert type(local_result) == dict
    assert local_result[('with', 'a')] == ['keen']
    assert local_result[('One', 'night')] == ['it']