Exemplo n.º 1
0
 def test_cleanup_dict(self):
     """Test that it removes single letter words from a dictionary file."""
     dict_file = os.path.abspath('tests/data/ch02/dictionary.txt')
     clean_dict = cleanup_dictionary.cleanup_dict(dict_file)
     self.assertEqual(len(clean_dict), 52)  # 78 words - 26 letters
     for element in clean_dict:
         self.assertGreater(len(element), 1)
def fill_dummy(plainlist: list,
               factors: list,
               dummy_words: list = None) -> list:
    """Fill a plainlist with dummy words.

    Adds pseudorandom dummy words to the end until the factors of the length of
    **plainlist** includes **factors**.

    Args:
        plainlist (list): List of words of plaintext message.
        factors (list): List of integers that must be factors of the length of
            **plainlist**.
        dummy_words (list): List of dummy words to use as filler. If not
            provided, defaults to :const:`~src.ch02.DICTIONARY_FILE_PATH`
            using :func:`~src.ch02.p1_cleanup_dictionary.cleanup_dict`.

    Returns:
        Same list as **plainlist**, but with dummy words added.

    """
    if dummy_words is None:
        dummy_words = cleanup_dict(DICTIONARY_FILE_PATH)
    for factor in factors:
        while factor not in get_factors(len(plainlist)):
            plainlist.append(choice(dummy_words).lower())
    return plainlist
Exemplo n.º 3
0
def main():
    """Demonstrate count_syllables with a word dictionary file."""
    word_list = cleanup_dict(DICTIONARY_FILE_PATH)
    sample_list = sample(word_list, 15)
    for word in sample_list:
        try:
            syllables = count_syllables(format_words(word))
        except KeyError:
            # Skip words in neither dictionary.
            print(f'Not found: {word}')
            continue
        print(f'{word} {syllables}')
 def test_find_anagram_phrases(self):
     """Test that it can find anagram phrases."""
     dict_file = os.path.abspath('tests/data/ch03/dictionary.txt')
     word_list = cleanup_dict(dict_file)
     word_list = cleanup_list_more(word_list)
     anagram_dict = anagram_generator.get_anagram_dict(word_list)
     # Test a word without anagrams.
     phrases, phrase = [], []
     anagram_generator.find_anagram_phrases(phrases, 'ttr', anagram_dict, phrase)
     self.assertListEqual([], phrases)
     # Test a phrase with four anagram phrases.
     anagram_generator.find_anagram_phrases(phrases, 'a cat', anagram_dict, phrase)
     self.assertListEqual(['a act', 'a cat', 'act a', 'cat a'], phrases)
def anagram_generator(word: str) -> list:
    """Generate phrase anagrams.

    Make phrase anagrams from a given word or phrase.

    Args:
        word (str): Word to get phrase anagrams of.

    Returns:
        :py:obj:`list` of phrase anagrams of **word**.

    """
    phrases, phrase = [], []
    dictionary = cleanup_list_more(cleanup_dict(DICTIONARY_FILE_PATH))
    anagram_dict = multi_get_anagram_dict(dictionary)
    # Build anagram dictionary specific to word.
    new_anagram_dict = remove_unusable_words(anagram_dict,
                                             list(word.replace(' ', '')))
    # Recursively find phrases using new_anagram_dict.
    find_anagram_phrases(phrases, word, new_anagram_dict, phrase)
    return phrases
 def test_find_anagrams(self):
     """Test that it can find anagrams with a word or phrase."""
     dict_file = os.path.abspath('tests/data/ch03/dictionary.txt')
     word_list = cleanup_dict(dict_file)
     word_list = cleanup_list_more(word_list)
     anagram_dict = anagram_generator.get_anagram_dict(word_list)
     # Test a word without anagrams.
     anagrams = []
     test_list = anagram_generator.find_anagrams('ttr', anagram_dict)
     self.assertListEqual(anagrams, test_list)
     # Test a word with anagrams.
     anagrams = ['set', 'test', 'tet']
     test_list = anagram_generator.find_anagrams('test', anagram_dict)
     self.assertListEqual(anagrams, test_list)
     # Test a phrase.
     phrase = 'tip tap'
     anagrams = ['a', 'apt', 'at', 'i', 'it', 'pap', 'pat', 'patti', 'pip',
                 'pit', 'pita', 'pitt', 'tap', 'tat', 'tia', 'tip', 'tit']
     test_list = anagram_generator.find_anagrams(phrase, anagram_dict)
     self.assertListEqual(anagrams, test_list)
     # Test that it ignores uppercase.
     anagrams = ['joe', 'jose', 'so']
     test_list = anagram_generator.find_anagrams('Jose', anagram_dict)
     self.assertListEqual(anagrams, test_list)