def test_partial_syllables(self): ''' Test for some lines having the correct number of syllables, and some not having the correct number of syllables. ''' returned = check_syllables(poem_lines, pattern, word_to_phonemes) expected = ['With a gap before the next.', 'Then the poem ends.'] self.assertEqual(returned, expected, 'Expected value not returned.')
def test_check_syllables_4(self): ''' Test check_syllables with one string that doesn't match the syllable pattern.''' word_to_phonemes = { 'NEXT': ['N', 'EH1', 'K', 'S', 'T'], 'GAP': ['G', 'AE1', 'P'], 'BEFORE': ['B', 'IH0', 'F', 'AO1', 'R'], 'LEADS': ['L', 'IY1', 'D', 'Z'], 'WITH': ['W', 'IH1', 'DH'], 'LINE': ['L', 'AY1', 'N'], 'THEN': ['DH', 'EH1', 'N'], 'THE': ['DH', 'AH0'], 'A': ['AH0'], 'FIRST': ['F', 'ER1', 'S', 'T'], 'ENDS': ['EH1', 'N', 'D', 'Z'], 'POEM': ['P', 'OW1', 'AH0', 'M'], 'OFF': ['AO1', 'F'] } poem_lines = [ 'The first line leads off,', 'With a gap before the next.', 'Then the poem ends.' ] pattern = ([5, 3, 5], ['*', '*', '*']) actual = poetry_functions.check_syllables(poem_lines, pattern, word_to_phonemes) expected = ['With a gap before the next.'] self.assertEqual(actual, expected)
def check_poem(poem_lines, pattern, word_to_phonemes, form_name): """ (list of str, poetry pattern, pronunciation dictionary, str) -> NoneType Check whether the poem in poem_lines has the right number of lines to match for form given in pattern, and print a message if it doesn't. If it does, then check whether the lines in the poem have the right number of syllables and report the lines that don't; also check whether the lines of the poem have the correct rhyming scheme and report the lines that should rhyme but don't. """ if poetry_functions.count_lines(poem_lines) != len(pattern[0]): print("\n== The poem doesn't have the right number of lines. == \n") else: problem_lines = poetry_functions.check_syllables( poem_lines, pattern, word_to_phonemes) if len(problem_lines) == 0: print( '\nThe poem has the right number of syllables on each line.\n') else: print('\n== The poem is not a {}. These lines don\'t have the ' 'right number of syllables: == '.format(form_name)) print('\n'.join(problem_lines) + '\n') problem_rhymes = poetry_functions.check_rhyme_scheme( poem_lines, pattern, word_to_phonemes) if len(problem_rhymes) == 0: print('The poem follows the rhyme scheme.\n') else: print('\n== The poem is not a {}. These lines should rhyme' " but don't: ==".format(form_name)) for lines in problem_rhymes: print('\n'.join(lines) + '\n')
def check_poem(poem_lines, pattern, word_to_phonemes, form_name): """ (list of str, poetry pattern, pronunciation dictionary, str) -> NoneType Check whether the poem in poem_lines has the right number of lines to match for form given in pattern, and print a message if it doesn't. If it does, then check whether the lines in the poem have the right number of syllables and report the lines that don't; also check whether the lines of the poem have the correct rhyming scheme and report the lines that should rhyme but don't. """ if poetry_functions.count_lines(poem_lines) != len(pattern[0]): print("\n== The poem doesn't have the right number of lines. == \n") else: problem_lines = poetry_functions.check_syllables( poem_lines, pattern, word_to_phonemes) if len(problem_lines) == 0: print('\nThe poem has the right number of syllables on each line.\n') else: print('\n== The poem is not a {}. These lines don\'t have the ' 'right number of syllables: == '.format(form_name)) print('\n'.join(problem_lines) + '\n') problem_rhymes = poetry_functions.check_rhyme_scheme( poem_lines, pattern, word_to_phonemes) if len(problem_rhymes) == 0: print('The poem follows the rhyme scheme.\n') else: print('\n== The poem is not a {}. These lines should rhyme' " but don't: ==".format(form_name)) for lines in problem_rhymes: print('\n'.join(lines) + '\n')
def test_02_correct_number_of_syllables_one_item(self): ''' Tests a poem line with correct syllables. ''' poem_line = ['Dog',] pattern = ([1], ['A']) expected = [] self.assertEqual(check_syllables(poem_line, pattern, x), expected, "Did not pass: poem_line = ['Dog'] / pattern = ([1], ['A'])")
def test_all_correct_syllables(self): ''' Test for all lines having the correct number of syllables. ''' pattern = ([5, 7, 5], ['*', '*', '*']) returned = check_syllables(poem_lines, pattern, word_to_phonemes) expected = [] self.assertEqual(returned, expected, 'Expected value not returned.')
def test_01_correct_number_of_syllables_multiple_items(self): ''' Tests multiple poem lines with correct syllables. ''' poem_line = ['Dog', 'Fog', 'Smolog'] pattern = ([1, 1, 2], ['A', 'A', 'A']) expected = [] self.assertEqual(check_syllables(poem_line, pattern, x), expected, "Did not pass: poem_line = ['Dog', 'Fog', 'Smolog'] / pattern = ([1, 1, 2], ['A', 'A', 'A'])")
def test_06_multiple_no_syllable_requirements_multiple_items(self): ''' Tests the no syllable requirments with multiple '0' with multiple lines ''' poem_line = ['mart', 'farty', 'dart MART, Bat!'] pattern = ([0, 0, 0], ['*', '*', '*']) expected = [] self.assertEqual(check_syllables(poem_line, pattern, x), expected, "Did not pass: poem_line = ['mart', 'farty', 'dart MART, Bat!'] / pattern = ([0, 0, 0], ['*', '*', '*']")
def test_03_incorrect_number_of_syllables_multiple_items(self): ''' Tests incorrect syllables on multiple poem lines. ''' poem_line = ['Dog', 'Fog', 'Smolog'] pattern = ([0, 4, 3], ['*', 'A', '*']) expected = ['Fog', 'Smolog'] self.assertEqual(check_syllables(poem_line, pattern, x), expected, "Did not pass: poem_line = ['Dog', 'Fog', 'Smolog'] / pattern = ([0, 4, 3], ['*', 'A', '*'])")
def test_05_check_with_no_syllable_requirements_one_item(self): ''' Tests the no syllable requirments (use of 0) on one line. ''' poem_line = ['Farty Mart'] pattern = ([0], ['A']) expected = [] self.assertEqual(check_syllables(poem_line, pattern, x), expected, "Did not pass: poem_line = ['Farty Mart'] / pattern = ([0], ['A'])")
def test_04_incorrect_number_of_syllables_one_item(self): ''' Tests incorrect syllables on a poem line. ''' poem_line = ['Dog'] pattern = ([3], ['*']) expected = ['Dog'] self.assertEqual(check_syllables(poem_line, pattern, x), expected, "Did not pass: poem_line = ['Dog'] / pattern = ([3], ['*'])")
def test_no_req(self): ''' Test if there is no requirement on the number of syllables in each line. ''' pattern = ([0, 0, 0], ['*', '*', '*']) returned = check_syllables(poem_lines, pattern, word_to_phonemes) expected = [] self.assertEqual(returned, expected, 'Expected value not returned.')
def test_mixed_reqs(self): ''' Test if some lines require syllables, but some do not. ''' pattern = ([4, 7, 0], ['*', '*', '*']) returned = check_syllables(poem_lines, pattern, word_to_phonemes) expected = ['The first line leads off,'] self.assertEqual(returned, expected, 'Expected value not '\ + 'returned')
def test_empty_str(self): ''' Test if there is only one line, which is empty, and does not contain the correct number of syllables. ''' poem_lines = [''] pattern = ([5], ['A']) returned = check_syllables(poem_lines, pattern, word_to_phonemes) self.assertEqual(returned, poem_lines, 'Expected value not returned.')
def test_all_incorrect_syllables(self): ''' Test for all lines having an incorrect number of syllables. ''' poem_lines = ['The first line leads a gap,', \ 'With a gap before the next.', \ 'Then the poem ends.'] pattern = ([4, 5, 4], ['*', 'A', 'B']) returned = check_syllables(poem_lines, pattern, word_to_phonemes) self.assertEqual(returned, poem_lines, 'Expected value not returned.')
def test_poem_of_mutiple_lines(self): """ Poem_lines consist of mutiple lines and the order of returned lines is beng tested """ poem_lines = ['The first line leads off,', 'With a gap before the next.', 'Then the poem ends.'] pattern = ([5, 5, 4], ['*','*','*']) expected_list = ['With a gap before the next.', 'Then the poem ends.'] self.assertEqual(check_syllables(poem_lines,pattern,word_to_phonemes), expected_list, 'Poem_lines consists of mutiple lines') self.assertFalse(check_syllables(poem_lines,pattern,word_to_phonemes) \ == expected_list[::-1],'Order of returned lines')
def test_poem_of_mutiple_lines(self): """ Poem_lines consist of mutiple lines and the order of returned lines is beng tested """ poem_lines = [ 'The first line leads off,', 'With a gap before the next.', 'Then the poem ends.' ] pattern = ([5, 5, 4], ['*', '*', '*']) expected_list = ['With a gap before the next.', 'Then the poem ends.'] self.assertEqual( check_syllables(poem_lines, pattern, word_to_phonemes), expected_list, 'Poem_lines consists of mutiple lines') self.assertFalse(check_syllables(poem_lines,pattern,word_to_phonemes) \ == expected_list[::-1],'Order of returned lines')
def test_poem_of_one_line(self): """ Poem_lines consist of one line is being tested. """ poem_lines = ['The first line leads off,'] pattern = ([5], ['*']) self.assertEqual(check_syllables(poem_lines,pattern,word_to_phonemes), [], 'Poem_lines consists of one line')
def test_poem_of_one_line(self): """ Poem_lines consist of one line is being tested. """ poem_lines = ['The first line leads off,'] pattern = ([5], ['*']) self.assertEqual( check_syllables(poem_lines, pattern, word_to_phonemes), [], 'Poem_lines consists of one line')
def test_no_syllabic_requirements(self): """ Test the pattern has no syllabic requirements ,all pattern[0] being zeros, the returned lines should be empty list. """ poem_lines = ['The first line leads off,', 'With a gap before the next.'] pattern = ([0, 0], ['*', '*']) expected_list = [] self.assertEqual(check_syllables(poem_lines,pattern,word_to_phonemes), expected_list, 'No syllabic requirements')
def test_no_syllabic_requirements(self): """ Test the pattern has no syllabic requirements ,all pattern[0] being zeros, the returned lines should be empty list. """ poem_lines = [ 'The first line leads off,', 'With a gap before the next.' ] pattern = ([0, 0], ['*', '*']) expected_list = [] self.assertEqual( check_syllables(poem_lines, pattern, word_to_phonemes), expected_list, 'No syllabic requirements')
def test_case_1(self): poem_lines = ['The first line leads off,', 'With a gap before the next.', 'Then the poem ends.'] pattern = ([5, 5, 4], ['*', '*', '*']) word_to_phonemes = {'NEXT': ['N', 'EH1', 'K', 'S', 'T'], 'GAP': ['G', 'AE1', 'P'], 'BEFORE': ['B', 'IH0', 'F', 'AO1', 'R'], 'LEADS': ['L', 'IY1', 'D', 'Z'], 'WITH': ['W', 'IH1', 'DH'], 'LINE': ['L', 'AY1', 'N'], 'THEN': ['DH', 'EH1', 'N'], 'THE': ['DH', 'AH0'], 'A': ['AH0'], 'FIRST': ['F', 'ER1', 'S', 'T'], 'ENDS': ['EH1', 'N', 'D', 'Z'], 'POEM': ['P', 'OW1', 'AH0', 'M'], 'OFF': ['AO1', 'F']} actual=check_syllables(poem_lines, pattern, word_to_phonemes) expected=['With a gap before the next.', 'Then the poem ends.'] self.assertEqual(expected, actual)
def test_check_syllables_2(self): ''' Test check_syllables with no syllable requirement per line.''' word_to_phonemes = { 'NEXT': ['N', 'EH1', 'K', 'S', 'T'], 'GAP': ['G', 'AE1', 'P'], 'BEFORE': ['B', 'IH0', 'F', 'AO1', 'R'], 'LEADS': ['L', 'IY1', 'D', 'Z'], 'WITH': ['W', 'IH1', 'DH'], 'LINE': ['L', 'AY1', 'N'], 'THEN': ['DH', 'EH1', 'N'], 'THE': ['DH', 'AH0'], 'A': ['AH0'], 'FIRST': ['F', 'ER1', 'S', 'T'], 'ENDS': ['EH1', 'N', 'D', 'Z'], 'POEM': ['P', 'OW1', 'AH0', 'M'], 'OFF': ['AO1', 'F'] } pattern = ([0, 0, 0], ['*', '*', '*']) poem_lines = ['First line,\n', 'Before line,\n', 'Ends line\n'] actual = poetry_functions.check_syllables(poem_lines, pattern, word_to_phonemes) expected = [] self.assertEqual(actual, expected)
} # Type check poetry_functions.check_rhyme_scheme result = poetry_functions.check_rhyme_scheme(poem_lines, pattern, word_to_phonemes) assert isinstance(result, list), \ '''poetry_functions.check_rhyme_scheme should return a list of list of str, but returned {0}.''' \ .format(type(result)) for item in result: assert isinstance(item, list), \ '''poetry_functions.check_rhyme_scheme should return a list of list of str, but returned a list of {0}.''' \ .format(type(item)) # Type check poetry_functions.check_syllables result = poetry_functions.check_syllables(poem_lines, pattern, word_to_phonemes) assert isinstance(result, list), \ '''poetry_functions.check_syllables should return a list, but returned {0}.''' \ .format(type(result)) for item in result: assert isinstance(item, str), \ '''poetry_functions.check_syllables should return a list of str, but returned a list of {0}.''' \ .format(type(item)) our_print(""" The type checker passed. This means that the functions in poetry_functions.py: - are named correctly,
'POEM': ['P', 'OW1', 'AH0', 'M'], 'OFF': ['AO1', 'F']} # Type check poetry_functions.check_rhyme_scheme result = poetry_functions.check_rhyme_scheme(poem_lines, pattern, word_to_phonemes) assert isinstance(result, list), \ '''poetry_functions.check_rhyme_scheme should return a list of list of str, but returned {0}.''' \ .format(type(result)) for item in result: assert isinstance(item, list), \ '''poetry_functions.check_rhyme_scheme should return a list of list of str, but returned a list of {0}.''' \ .format(type(item)) # Type check poetry_functions.check_syllables result = poetry_functions.check_syllables(poem_lines, pattern, word_to_phonemes) assert isinstance(result, list), \ '''poetry_functions.check_syllables should return a list, but returned {0}.''' \ .format(type(result)) for item in result: assert isinstance(item, str), \ '''poetry_functions.check_syllables should return a list of str, but returned a list of {0}.''' \ .format(type(item)) our_print(""" The type checker passed. This means that the functions in poetry_functions.py: